Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CMake flow not forcing c++17 and filesystem library to link #443

Open
2 of 8 tasks
bentomo opened this issue Jan 7, 2025 · 3 comments
Open
2 of 8 tasks

CMake flow not forcing c++17 and filesystem library to link #443

bentomo opened this issue Jan 7, 2025 · 3 comments

Comments

@bentomo
Copy link

bentomo commented Jan 7, 2025

Bug category

  • bug - compilation error
  • bug - compilation warning
  • bug - runtime error
  • bug - runtime warning
  • bug - logic error

Describe the bug
I'm already aware of the c++17 requirements but there seems to be a gray area where the std::filesystem library is ACTUALLY included in the c++ version. I had cmake print out the actual compiler command for me and I wasn't seeing stdc++17 or stdc++fs anywhere. Anytime I tried to build the examples or a test project using this I couldn't get it to figure out how to link in file system. I'm putting this here for alter in case anyone else has the same problem as me. I spent WAY too long trying to figure out how to do this properly in the cmake flow, and I'm too stupid to figure that out. I ended up just hacking the cmakefiles to get it to build.

From what I can tell there are a bunch of other issues related to the filesystem flow in this repo so maybe this will help answer some stuff. I'm not sure what the FindFilesystem.cmake file is for either, I couldn't tell when it's supposed to run but I don't think it was.

diff --git a/cmake/functions/link_external_libraries.cmake b/cmake/functions/link_external_libraries.cmake
index c188265..2aa45fd 100644
--- a/cmake/functions/link_external_libraries.cmake
+++ b/cmake/functions/link_external_libraries.cmake
@@ -20,9 +20,9 @@ function(target_link_libraries_system target)
       message("Warning: ${lib} doesn't set INTERFACE_INCLUDE_DIRECTORIES. No include_directories set.")
     endif()
     if(scope)
-      target_link_libraries(${target} ${scope} ${lib})
+      target_link_libraries(${target} ${scope} ${lib} stdc++fs)
     else()
-      target_link_libraries(${target} ${lib})
+      target_link_libraries(${target} ${lib} stdc++fs)
     endif()
   endforeach()
 endfunction(target_link_libraries_system)

Steps to Reproduce

mkdir build
cd build
cmake .. --install-prefix=/home/$USER/matplot_bin -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS="-O2"
make

# or any cmake args really, it seems pretty reproducible with my system, no matter what explatives I yell at my machine it won't use the correct CXX version/library
cmake .. -DCMAKE_RULE_MESSAGES:BOOL=OFF -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++

Output

../../../source/matplot/libmatplot.a(gnuplot.cpp.o): In function `matplot::backend::gnuplot::output(std::__cxx11::basic_string<char, std::char_traits<char>, std::
allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
gnuplot.cpp:(.text+0x1995): undefined reference to `std::filesystem::__cxx11::path::_M_split_cmpts()'
gnuplot.cpp:(.text+0x19a4): undefined reference to `std::filesystem::__cxx11::path::parent_path() const'
gnuplot.cpp:(.text+0x1aa1): undefined reference to `std::filesystem::__cxx11::path::parent_path() const'
gnuplot.cpp:(.text+0x1aa9): undefined reference to `std::filesystem::status(std::filesystem::__cxx11::path const&)'
gnuplot.cpp:(.text+0x1ae0): undefined reference to `std::filesystem::__cxx11::path::parent_path() const'
gnuplot.cpp:(.text+0x1ae8): undefined reference to `std::filesystem::create_directory(std::filesystem::__cxx11::path const&)'
gnuplot.cpp:(.text+0x1afd): undefined reference to `std::filesystem::__cxx11::path::parent_path() const'
gnuplot.cpp:(.text+0x1b05): undefined reference to `std::filesystem::status(std::filesystem::__cxx11::path const&)'
gnuplot.cpp:(.text+0x1b41): undefined reference to `std::filesystem::__cxx11::path::parent_path() const'
../../../source/matplot/libmatplot.a(gnuplot.cpp.o): In function `matplot::backend::gnuplot::output(std::__cxx11::basic_string<char, std::char_traits<char>, std::
allocator<char> > const&)':
gnuplot.cpp:(.text+0x3d79): undefined reference to `std::filesystem::__cxx11::path::_M_split_cmpts()'
../../../source/matplot/libmatplot.a(gnuplot.cpp.o): In function `std::filesystem::__cxx11::path::extension() const':
gnuplot.cpp:(.text._ZNKSt10filesystem7__cxx114path9extensionEv[_ZNKSt10filesystem7__cxx114path9extensionEv]+0x11): undefined reference to `std::filesystem::__cxx1
1::path::_M_find_extension() const'
gnuplot.cpp:(.text._ZNKSt10filesystem7__cxx114path9extensionEv[_ZNKSt10filesystem7__cxx114path9extensionEv]+0xae): undefined reference to `std::filesystem::__cxx1
1::path::_M_split_cmpts()'
collect2: error: ld returned 1 exit status
make[2]: *** [examples/line_plot/plot/CMakeFiles/example_plot_1.dir/build.make:105: examples/line_plot/plot/example_plot_1] Error 1
make[1]: *** [CMakeFiles/Makefile2:3174: examples/line_plot/plot/CMakeFiles/example_plot_1.dir/all] Error 2
make: *** [Makefile:156: all] Error 2

Platform

  • cross-platform issue - linux
  • cross-platform issue - windows
  • cross-platform issue - macos

Environment Details:

  • OS: Red Hat Enterprise Linux
  • OS Version: 8.10
  • Compiler: gcc/g++
  • Compiler version: g++ (GCC) 8.5.0 20210514 (Red Hat 8.5.0-21)

Additional context
I'm attempting to build and install

@alandefreitas
Copy link
Owner

When this library was written, few compilers had support for <filesystem>, so we needed lots of workarounds. Nowadays, this needs to be refactored, and all the targets need now is target_compile_features(${TARGET_NAME} PRIVATE cxx_std_17).

@bentomo
Copy link
Author

bentomo commented Jan 7, 2025

Fast response, thanks!

I forgot to include my CMakeLists.txt above. I tried the install method first, but flailed around with some other flows trying to find the right CMake parameters. In the "submodule flow" I had this but the target_compile_features parameter didn't appear to apply to project as a whole. I'm sure it's my CMake ignorance though. I pulled the line plot example code and tried to build it

This yielded the same error.

cmake_minimum_required(VERSION 3.21)
project(plot_test)

add_executable(plot matplot_test.cpp)
target_compile_features(plot PRIVATE cxx_std_17)

add_subdirectory(matplotplusplus)
target_compile_features(matplot PRIVATE cxx_std_17)
target_link_libraries(plot PUBLIC matplot)

@alandefreitas
Copy link
Owner

Sorry. I wasn't clear. I was talking about what matplot needs to do to solve the problem internally. cxx_std_17 in your project probably won't solve the problem.

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

No branches or pull requests

2 participants