felixguendling / cista

Cista is a simple, high-performance, zero-copy C++ serialization & reflection library.
https://cista.rocks
MIT License
1.78k stars 113 forks source link

error: 'fmt' has not been declared #226

Open YanzhaoW opened 1 month ago

YanzhaoW commented 1 month ago

Hi,

I'm using this library downloaded from Conan center, which use cista.h single header file.

I got an error with a hello world example:

#include "cista.h"
#include <iostream>
int main() {
   std::cout << "Hello world\n";
}

The error is:

/u/land/software/gcc/latest/bin/g++ -DASIO_STANDALONE -isystem /u/yanwang/.conan2/p/asio70e8791ea9016/p/include -isystem /u/yanwang/.conan2/p/b/fmtc97181cbd62d9/p/include -isystem /u/yanwang/.conan2/p/cista8c77ed80eecd5/p/include -m64 -O3 -DNDEBUG -std=gnu++17 -MD -MT CMakeFiles/main.dir/main.cpp.o -MF CMakeFiles/main.dir/main.cpp.o.d -o CMakeFiles/main.dir/main.cpp.o -c /u/yanwang/test/cpp/asio/main.cpp
In file included from /u/yanwang/test/cpp/asio/main.cpp:4:
/u/yanwang/.conan2/p/cista8c77ed80eecd5/p/include/cista.h:541:8: error: ‘fmt’ has not been declared
  541 | struct fmt::formatter<cista::bitset<Size>> : ostream_formatter {};
      |        ^~~
/u/yanwang/.conan2/p/cista8c77ed80eecd5/p/include/cista.h:541:22: error: expected unqualified-id before ‘<’ token
  541 | struct fmt::formatter<cista::bitset<Size>> : ostream_formatter {};
      |                      ^
/u/yanwang/.conan2/p/cista8c77ed80eecd5/p/include/cista.h:815:8: error: ‘fmt’ has not been declared
  815 | struct fmt::formatter<cista::strong<T, Tag>> : ostream_formatter {};
      |        ^~~
/u/yanwang/.conan2/p/cista8c77ed80eecd5/p/include/cista.h:815:22: error: expected unqualified-id before ‘<’ token
  815 | struct fmt::formatter<cista::strong<T, Tag>> : ostream_formatter {};
      |                      ^
/u/yanwang/.conan2/p/cista8c77ed80eecd5/p/include/cista.h:4755:8: error: ‘fmt’ has not been declared
 4755 | struct fmt::formatter<cista::basic_string<Ptr>> : ostream_formatter {};
      |        ^~~
/u/yanwang/.conan2/p/cista8c77ed80eecd5/p/include/cista.h:4755:22: error: expected unqualified-id before ‘<’ token
 4755 | struct fmt::formatter<cista::basic_string<Ptr>> : ostream_formatter {};
      |                      ^
/u/yanwang/.conan2/p/cista8c77ed80eecd5/p/include/cista.h:9080:8: error: ‘fmt’ has not been declared
 9080 | struct fmt::formatter<cista::indexed<T>> : ostream_formatter {};
      |        ^~~
/u/yanwang/.conan2/p/cista8c77ed80eecd5/p/include/cista.h:9080:22: error: expected unqualified-id before ‘<’ token
 9080 | struct fmt::formatter<cista::indexed<T>> : ostream_formatter {};

I digged into the header file and it's caused by

#if __has_include("fmt/ostream.h")

template <typename T, typename Tag>
struct fmt::formatter<cista::strong<T, Tag>> : ostream_formatter {};

#endif

I think __has_include just means whether the header file is capable to be included, instead of whether the library has been already included.

felixguendling commented 1 month ago

You're right. My mistake. Can you please create a PR that adds the #include statement?

YanzhaoW commented 1 month ago

Hi,

Sorry I'm not familiar with the program structure of this project and have no idea how cista.h is stitched together. So I think it's better to have someone who already had some experience add the #include statement. :D

zhoupengwei commented 1 month ago

@YanzhaoW It depends on the fmt ostream. Please include the fmt ostream header before using cista, as shown in the following example:

#include <fmt/ostream.h>
#include "cista.h"
felixguendling commented 1 month ago

The idea was that it does not depend on libfmt but it is compatible if libfmt is available. So it was my mistake to only check for the include but not actually include it. @zhoupengwei your work around won't be necessary anymore after this is fixed. I just didn't find the time to add the #include statements after the __has_include checks.