What is the correct way to link C++17 filesystem with CMake?

帅比萌擦擦* 提交于 2021-01-28 01:49:10

问题


I noticed that linking my CMake project with gcc 8.3 fails to link functions from std::filesystem. This is not the case with gcc 9, clang 7 or clang 8.

I found solutions like this and this, but these hard-code the linking of stdc++fs, which is normally not what you want to do.

  • So what is the correct way to link such libraries?
  • Do I have to do a find_package? What would be the package I am looking for?

回答1:


It looks like there is no proper solution to this as of now. There still is an open issue on this subject on the CMake tracker.

Some seem to be using find modules like this one, which would allow you to use code like the following:

find_package(Filesystem REQUIRED)

add_executable(myapp myapp.cpp)
target_link_libraries(myapp PRIVATE std::filesystem)

In my opinion this is preferable to changing CMAKE_CXX_FLAGS or linking against stdc++fs directly.



来源:https://stackoverflow.com/questions/58545562/what-is-the-correct-way-to-link-c17-filesystem-with-cmake

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!