简体   繁体   English

如何使用cmake确保C ++ 14编译器与实验性文件系统TS库链接?

[英]How do I use cmake to ensure a C++14 compiler links with the experimental filesystem TS library?

I'm (switching to) using <std::experimental::filesystem> in my C++14 code, and I need to be able to build it using GCC 5.x on Linux (and any C++14-enabled compiler really, or at least - relevant versions of clang, msvc and perhaps icc) . 我(切换到)在我的C ++ 14代码中使用<std::experimental::filesystem> ,并且我需要能够在Linux(以及任何启用了C ++ 14的版本)上使用GCC 5.x进行构建编译器,或者至少是相关版本的clang,msvc以及icc)。

Now, when I link, I'm getting errors about missing all of the filesystem-related symbols, eg: 现在,当我链接时,我收到有关丢失所有与文件系统相关的符号的错误,例如:

foo.cpp:(.text+0x9cd): undefined reference to `std::experimental::filesystem::v1::__cxx11::path::_M_split_cmpts()'

Now, if I "manually" add a dependency on libstdc++fs : 现在,如果我“手动”添加对libstdc++fs的依赖关系:

target_link_libraries(foo other_lib stdc++fs)

then everything links fine - but how do I generalize this to any C++14-capable compiler? 然后一切都很好-但是如何将其推广到任何具有C ++ 14功能的编译器?

Note: The relevant parts of my CMakeLists.txt look something like this: 注意:我的CMakeLists.txt的相关部分看起来像这样:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall" )
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

add_library(bar src/bar.cpp)
add_executable(foo src/foo.cpp)
target_link_libraries(foo bar other_lib)

* (When I was using boost::filesystem it was paradoxically easier - I just need to use whatever FindBoost.cmake produced.) *(当我使用boost::filesystem反而更容易-我只需要使用产生的任何FindBoost.cmake 。)
** Using -Wall is a problem for other compilers as well, I guess. **对于其他编译器,使用-Wall也是一个问题,我想。

Unfortunately, this is exactly how it is intended to be used. 不幸的是,这恰恰是打算使用的方式。 See the relevant part of libstdc++ documentation : 请参阅libstdc ++文档的相关部分

GCC 5.3 includes an implementation of the Filesystem library defined by the technical specification ISO/IEC TS 18822:2015. GCC 5.3包含由技术规范ISO / IEC TS 18822:2015定义的文件系统库的实现。 Because this is an experimental library extension, not part of the C++ standard, it is implemented in a separate library, libstdc++fs.a, and there is no shared library for it. 由于这是实验性库扩展,不是C ++标准的一部分, 因此在单独的库libstdc ++ fs.a中实现,并且没有共享库。 To use the library you should include and link with -lstdc++fs . 要使用该库,您应该包括-lstdc ++ fs并与之链接 The library implementation is incomplete on non-POSIX platforms, specifically Windows support is rudimentary. 在非POSIX平台上,库的实现不完整,特别是Windows支持是基本的。

Due to the experimental nature of the Filesystem library the usual guarantees about ABI stability and backwards compatibility do not apply to it. 由于文件系统库的实验性质,有关ABI稳定性和向后兼容性的通常保证不适用于它。 There is no guarantee that the components in any header will remain compatible between different GCC releases. 不保证任何标头中的组件在不同的GCC版本之间都将保持兼容。

(emphasis mine) (强调我的)

To link libstdc++fs explicitly only under GCC, you can use the relevant CMake variable : 要仅在GCC下显式链接libstdc++fs ,可以使用相关的CMake变量

target_link_libraries(foo other_lib)
if (CMAKE_CXX_COMPILER_ID STREQUAL GNU)
    target_link_libraries(foo stdc++fs)
endif()

Or, in a bit fancier manner, 或者,以更奇特的方式

if (CMAKE_CXX_COMPILER_ID STREQUAL GNU)
    set(CXX_FILESYSTEM_LIBRARIES stdc++fs)
else()
    set(CXX_FILESYSTEM_LIBRARIES)
endif()
target_link_libraries(foo other_lib ${CXX_FILESYSTEM_LIBRARIES})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM