用“实验/文件系统”构建项目。使用cmake [英] Build project with "experimental/filesystem" using cmake

查看:113
本文介绍了用“实验/文件系统”构建项目。使用cmake的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在项目中添加一个实验/文件系统标头

I need to add a "experimental/filesystem" header to my project

#include <experimental/filesystem>
int main() {
    auto path = std::experimental::filesystem::current_path();
    return 0;
}

所以我使用了-lstdc ++ fs标志并与libstdc ++ fs链接.a

So I used -lstdc++fs flag and linked with libstdc++fs.a

cmake_minimum_required(VERSION 3.7)
project(testcpp)
set(CMAKE_CXX_FLAGS "-std=c++14 -lstdc++fs" )
set(SOURCE_FILES main.cpp)
target_link_libraries(${PROJECT_NAME} /usr/lib/gcc/x86_64-linux-gnu/7/libstdc++fs.a)
add_executable(testcpp ${SOURCE_FILES})

但是,我下一个错误:


CMake CMakeLists.txt:9(target_link_libraries)错误:
无法为目标 testcpp指定链接库不是由

这个项目构建的。

CMake Error at CMakeLists.txt:9 (target_link_libraries): Cannot specify link libraries for target "testcpp" which is not built by
this project.

但是如果我直接编译,那就可以了:

But if I compile directly, it`s OK:

g++-7 -std=c++14 -lstdc++fs  -c main.cpp -o main.o
g++-7 -o main main.o /usr/lib/gcc/x86_64-linux-gnu/7/libstdc++fs.a

我的错误在哪里?

推荐答案

就是这样 target_link_libraries()调用必须在 add_executable()调用之后进行。否则, testcpp 目标尚不清楚。 CMake依次解析所有内容。

It's just that the target_link_libraries() call has to come after the add_executable() call. Otherwise the testcpp target is not known yet. CMake parses everything sequential.

因此,为了完整起见,这是我测试过的示例的有效版本:

So just for completeness, here is a working version of your example I've tested:

cmake_minimum_required(VERSION 3.7)

project(testcpp)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# NOTE: The following would add library with absolute path
#       Which is bad for your projects cross-platform capabilities
#       Just let the linker search for it
#add_library(stdc++fs UNKNOWN IMPORTED)
#set_property(TARGET stdc++fs PROPERTY IMPORTED_LOCATION "/usr/lib/gcc/x86_64-linux-gnu/7/libstdc++fs.a")

set(SOURCE_FILES main.cpp)
add_executable(testcpp ${SOURCE_FILES})
target_link_libraries(${PROJECT_NAME} stdc++fs)

这篇关于用“实验/文件系统”构建项目。使用cmake的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆