Skip to content

Conversation

na-trium-144
Copy link
Contributor

close #14249

I tested with gcc 5-10 and clang 7-10 here: https://github.com/na-trium-144/meson-fs-dep-test/actions/runs/17265187744

filesystem_dep = dependency('cpp-filesystem')
  1. Checks if <filesystem> header is available.
  2. Checks if test code compiles and runs without crash with <filesystem>.
    • For instance when both gcc 8 and 9 are installed, gcc 9 does not need stdc++fs, while gcc 8 without stdc++fs causes segfault. So we need to actually run the test.
    • We cannot rely on compiler versions, because clang can use libstdc++.
  3. If it fails, it tries to find libstdc++fs (for gcc 8) or libc++fs (for clang 7-8) and run test again with those libraries.
  4. If it is cross compilation and cannot run host binaries, it just search libstdc++fs or libc++fs and use if it's found.
filesystem_dep = dependency('cpp-experimental-filesystem')

does the same with <experimental/filesystem>, using libstdc++fs (for gcc 6-7) and libc++experimental (for clang 5-6).

Example with fallback to experimental/filesystem:

filesystem_dep = dependency('cpp-filesystem', 'cpp-experimental-filesystem')
filesystem_is_experimental = filesystem_dep.name() == 'cpp-experimental-filesystem'

executable('main', 'main.cc',
  dependencies: [filesystem_dep],
  cpp_args: filesystem_is_experimental ? ['-DEXPERIMENTAL_FS'] : []
)
#include <iostream>
#ifdef EXPERIMENTAL_FS
#include <experimental/filesystem>
namespace std_fs = std::experimental::filesystem;
#else
#include <filesystem>
namespace std_fs = std::filesystem;
#endif

int main(){
    std::cout << std_fs::exists("foo") << std::endl;
}

@jwakely
Copy link

jwakely commented Aug 28, 2025

  • For instance when both gcc 8 and 9 are installed, gcc 9 does not need stdc++fs, while gcc 8 without stdc++fs causes segfault.

There should not be a segfault. If you compile with gcc-8 and don't use -lstdc++fs then you should get a linker error:

/home/jwakely/gcc/8.5.0/include/c++/8.5.0/bits/fs_path.h:185:(.text._ZNSt10filesystem7__cxx114pathC2IA4_cS1_EERKT_NS1_6formatE[_ZNSt10filesystem7__cxx114pathC5IA4_cS1_EERKT_NS1_6formatE]+0x5e): undefined reference to `std::filesystem::__cxx11::path::_M_split_cmpts()'

Unfortunately because of the way Ubuntu packages libstdc++.so that doesn't necessarily happen. The linker finds the libstdc++.so from GCC 9 and thinks it's OK to use, and then it links to the incompatible libstdc++.so and crashes at runtime. Ubuntu probably could have done something to prevent this problem, but it's too late to fix now.

For other distros, you should get a linker error. But sadly that means for the general case, you can't rely on the linker error, and so you do need to execute the program to be sure :-(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Wrapping C++ std::filesystem implementation libraries in a custom dependency
2 participants