Skip to content

Commit 5b3ef6d

Browse files
author
Alex Wang
committed
Hopefully fix <filesystem> check for GCC 7
The CI builds using Ubuntu 18.04 were failing to make it past the CMake configuration stage due to being unable to find <filesystem> support. This is because Ubuntu 18.04 ships GCC 7, for which <filesystem> support was still experimental. As a result, <experimental/filesystem> was present, not <filesystem>, which causes the CMake check to error out as the test program #includes <filesystem>. The fix is simple - use __has_include to include the appropriate header and add a namespace alias to account for std::experimental. This is sufficient to get the CMake configuration to succeed using GCC 7 on macOS, but configuration still fails on Ubuntu 18.04 due to the linker being unable to find the definition for current_path() [0]. This error seems a bit weird since the linker command line clearly includes -lstdc++fs, and that library definitely defines current_path(). It turns out the Ubuntu 18.04 linker is order-sensitive, and -lstdc++fs was present on the command line before the object file for the test program, so the linker doesn't pull in the current_path() definition. ld64 on macOS, on the other hand, doesn't seem to be sensitive to library order [1]. Fixing this is fairly simple - use the LINK_LIBRARIES try_compile option instead of LINK_OPTIONS, so CMake puts the flag in the right place. I should have done that in the first place, but I somehow glossed over that option when first writing the new handling. [0]: [ 50%] Linking CXX executable cmTC_6f62e /root/cmake-3.27.8-linux-x86_64/bin/cmake -E cmake_link_script CMakeFiles/cmTC_6f62e.dir/link.txt --verbose=1 /usr/bin/c++ -Wall -Wextra -Wpedantic -Werror -lstdc++fs CMakeFiles/cmTC_6f62e.dir/std_fs_support_test.cpp.o -o cmTC_6f62e CMakeFiles/cmTC_6f62e.dir/std_fs_support_test.cpp.o: In function `main': std_fs_support_test.cpp:(.text+0x1f): undefined reference to `std::experimental::filesystem::v1::current_path[abi:cxx11]()' collect2: error: ld returned 1 exit status [1]: [100%] Linking CXX executable cmTC_fb65e /usr/local/Cellar/cmake/3.27.7/bin/cmake -E cmake_link_script CMakeFiles/cmTC_fb65e.dir/link.txt --verbose=1 /usr/local/opt/gcc@7/bin/g++-7 -Wall -Wextra -Wpedantic -Werror -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX11.sdk -mmacosx-version-min=10.12 -Wl,-search_paths_first -Wl,-headerpad_max_install_names -lstdc++fs CMakeFiles/cmTC_fb65e.dir/std_fs_support_test.cpp.o -o cmTC_fb65e ld: warning: dylib (/usr/local/Cellar/gcc@7/7.5.0_4/lib/gcc/7/libstdc++.dylib) was built for newer macOS version (10.17.5) than being linked (10.12) ld: warning: object file (/usr/local/Cellar/gcc@7/7.5.0_4/lib/gcc/7/libstdc++fs.a(dir.o)) was built for newer macOS version (10.17) than being linked (10.12) ld: warning: object file (/usr/local/Cellar/gcc@7/7.5.0_4/lib/gcc/7/libstdc++fs.a(ops.o)) was built for newer macOS version (10.17) than being linked (10.12) ld: warning: object file (/usr/local/Cellar/gcc@7/7.5.0_4/lib/gcc/7/libstdc++fs.a(path.o)) was built for newer macOS version (10.17) than being linked (10.12) ld: warning: object file (/usr/local/Cellar/gcc@7/7.5.0_4/lib/gcc/7/libstdc++fs.a(cow-dir.o)) was built for newer macOS version (10.17) than being linked (10.12) ld: warning: object file (/usr/local/Cellar/gcc@7/7.5.0_4/lib/gcc/7/libstdc++fs.a(cow-ops.o)) was built for newer macOS version (10.17) than being linked (10.12) ld: warning: object file (/usr/local/Cellar/gcc@7/7.5.0_4/lib/gcc/7/libstdc++fs.a(cow-path.o)) was built for newer macOS version (10.17) than being linked (10.12) [100%] Built target cmTC_fb65e
1 parent 9795673 commit 5b3ef6d

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

cmake/std_fs_support_test.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1+
#if __has_include(<filesystem>)
12
#include <filesystem>
3+
namespace fs = std::filesystem;
4+
#else
5+
#include <experimental/filesystem>
6+
namespace fs = std::experimental::filesystem;
7+
#endif
8+
29
int main() {
3-
auto cwd = std::filesystem::current_path();
10+
auto cwd = fs::current_path();
411
}

include/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ else()
7272
STDCXXFS_SUPPORTED
7373
"${CMAKE_CURRENT_BINARY_DIR}/stdcxxfs"
7474
SOURCES "${PROJECT_SOURCE_DIR}/cmake/std_fs_support_test.cpp"
75-
LINK_OPTIONS "-lstdc++fs"
75+
LINK_LIBRARIES "stdc++fs"
7676
CXX_STANDARD 17
7777
CXX_STANDARD_REQUIRED ON
7878
CXX_EXTENSIONS OFF
@@ -84,7 +84,7 @@ else()
8484
CXXFS_SUPPORTED
8585
"${CMAKE_CURRENT_BINARY_DIR}/cxxfs"
8686
SOURCES "${PROJECT_SOURCE_DIR}/cmake/std_fs_support_test.cpp"
87-
LINK_OPTIONS "-lc++fs"
87+
LINK_LIBRARIES "c++fs"
8888
CXX_STANDARD 17
8989
CXX_STANDARD_REQUIRED ON
9090
CXX_EXTENSIONS OFF

0 commit comments

Comments
 (0)