3

I'm trying to compile an application that uses std::filesystem. I'm using CMake for the build system, and g++-8 as a compiler.

My CMakeLists.txt includes target_link_libraries(<target_name> PUBLIC stdc++fs) and set(CMAKE_CXX_STANDARD 17)

make VERBOSE=1 shows that -lstdc++fs is used in the linker command.

Despite this, I get undefined references to std::filesystem components everywhere they are used.

It compiles fine in Docker, so it's clearly an environment issue.

Any tips for tracking this down?

EDIT: I can't post the exact error message because of company rules, and it's also super long. I'll post a truncated, anonymized version though:

CMakeFiles/ProjectName.dir/src/ui/dir_a/file_a.cpp.o: In function `ProjectName::ClassName::update(ProjectName::Body&)':
/home/username/prog/bots/src/ui/dir_a/file_a.cpp:30: undefined reference to `std::filesystem::current_path[abi:cxx11]()'
/home/username/prog/bots/src/ui/dir_a/file_a.cpp:35: undefined reference to `std::filesystem::create_directories(std::filesystem::__cxx11::path const&)'
CMakeFiles/ProjectName.dir/src/ui/dir_a/file_a.cpp.o: In function `std::filesystem::__cxx11::path::operator+=(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
/usr/include/c++/8/bits/fs_path.h:817: undefined reference to `std::filesystem::__cxx11::path::_M_split_cmpts()'
CMakeFiles/ProjectName.dir/src/ui/dir_a/file_a.cpp.o: In function `std::filesystem::__cxx11::path::operator+=(char const*)':
/usr/include/c++/8/bits/fs_path.h:825: undefined reference to `std::filesystem::__cxx11::path::_M_split_cmpts()'
CMakeFiles/ProjectName.dir/src/ui/dir_a/file_a.cpp.o: In function `std::filesystem::exists(std::filesystem::__cxx11::path const&)':
/usr/include/c++/8/bits/fs_ops.h:121: undefined reference to `std::filesystem::status(std::filesystem::__cxx11::path const&)'
CMakeFiles/ProjectName.dir/src/ui/dir_b/file_b.cpp.o: In function `ProjectName::ClassName::update[abi:cxx11]()':
/home/username/prog/bots/src/ui/dir_b/file_b.cpp:30: undefined reference to `std::filesystem::current_path[abi:cxx11]()'
/home/username/prog/bots/src/ui/dir_b/file_b.cpp:34: undefined reference to `std::filesystem::create_directories(std::filesystem::__cxx11::path const&)'
/home/username/prog/bots/src/ui/dir_b/file_b.cpp:36: undefined reference to `std::filesystem::__cxx11::directory_iterator::operator*() const'
/home/username/prog/bots/src/ui/dir_b/file_b.cpp:40: undefined reference to `std::filesystem::__cxx11::path::replace_extension(std::filesystem::__cxx11::path const&)'
/home/username/prog/bots/src/ui/dir_b/file_b.cpp:36: undefined reference to `std::filesystem::__cxx11::directory_iterator::operator++()'
CMakeFiles/ProjectName.dir/src/ui/dir_b/file_b.cpp.o: In function `std::filesystem::__cxx11::path::path(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&&, std::filesystem::__cxx11::path::format)':
/usr/include/c++/8/bits/fs_path.h:177: undefined reference to `std::filesystem::__cxx11::path::_M_split_cmpts()'

It continues like that for a hundred or so lines.

Software versions:

Ubuntu 18.04
g++-8 (installed with `apt`)
CMake 3.14.2
Omegastick
  • 1,773
  • 1
  • 20
  • 35
  • what platform are you using? – Madden Aug 14 '19 at 13:23
  • Could you please post the exact error message? Also specific versions of software. Are you using libstdc++ that comes with the compiler or the one that comes with the system (if not the same)? – Ilya Popov Aug 14 '19 at 13:25
  • See also this: https://stackoverflow.com/questions/56615841/passing-stdfilesystempath-to-a-function-segfaults – Ilya Popov Aug 14 '19 at 13:30
  • And this: https://www.reddit.com/r/cpp/comments/c87o36/clang_800_and_stdcfs_doesnt_work_with_o3/ – Ilya Popov Aug 14 '19 at 13:31
  • @IlyaPopov Thanks for the help. I've added more details to the OP. I'm not sure which version of stdc++ is being linked by CMake, do you know how I'd check? I'll give g++-9 a go. – Omegastick Aug 14 '19 at 13:40

1 Answers1

4

Upgrading to g++ 9 fixed the problem, but if anyone knows how to fix the problem and still use g++ 8 I'll accept that answer.

Omegastick
  • 1,773
  • 1
  • 20
  • 35