C++17 filesystem example and caveats

Some compilers will compile and link C++17 filesystem code, but fail on run. In general, although it’s a little slower to configure, consider using run tests to more completely ensure a feature is properly implemented.

CMake

see filesystem/ folder

Meson

In meson.build test for C++17 filesystem functionality by:

if add_languages('cpp', required: false)
  cpp = meson.get_compiler('cpp')
  code = '''
#include <filesystem>
namespace fs = std::filesystem;

int main(void) {
fs::path p = fs::path(".");
return 0;
}
'''
  # some compilers will compile and link, but only fail on run when accessing
  # the actual filesystem paths
  # e.g. MinGW G++ 9.2.0
  f17filesystem_ok = cpp.run(code, name: 'C++ filesystem').returncode() == 0
endif

Then use if f17filesystem_ok to decide if to build executables or libraries