Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cmake 链接不使用“添加”子目录`_Cmake - Fatal编程技术网

Cmake 链接不使用“添加”子目录`

Cmake 链接不使用“添加”子目录`,cmake,Cmake,我是CMake的新手,有以下问题(简化为MWE): 给出了项目结构 nlohmann_json/ CMakeLists.txt Example.hpp Example.cpp main.cpp 文件夹nlohmann_json只是一个克隆 以下是文件的内容: //Example.hpp #ifndef示例\u水电站 #定义示例\u水电站 #包括 #包括 void json_test(); #endif//水电站示例 //Example.cpp #包括“Example.hpp” 使用nlohm

我是CMake的新手,有以下问题(简化为MWE):

给出了项目结构

nlohmann_json/
CMakeLists.txt
Example.hpp
Example.cpp
main.cpp
文件夹nlohmann_json只是一个克隆

以下是文件的内容:

//Example.hpp
#ifndef示例\u水电站
#定义示例\u水电站
#包括
#包括
void json_test();
#endif//水电站示例
//Example.cpp
#包括“Example.hpp”
使用nlohmann::json;
#包括
void json_test(){
json-jsonfile;
jsonfile[“foo”]=“bar”;
std::of流文件(“key.json”);

文件您必须添加'nlohmann_json'作为静态库的链接目标:

cmake_minimum_required(VERSION 3.0.0)
project(MWE VERSION 0.1.0)

# Use C++17
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_subdirectory(nlohmann_json)

add_library(Example STATIC Example.cpp Example.hpp)
target_link_libraries(Example PRIVATE nlohmann_json)

add_executable(main main.cpp)
target_link_libraries(main PRIVATE Example)

还考虑在导出目标的符号或标头时将链接目标公开。


在您的示例中,您将nlohmann/json.hpp包含在example.hpp中。因此,根据此标头构建的每个人都需要知道nlohmann/json.hpp存储在何处。

您必须添加“nlohmann_json”作为静态库的链接目标:

cmake_minimum_required(VERSION 3.0.0)
project(MWE VERSION 0.1.0)

# Use C++17
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_subdirectory(nlohmann_json)

add_library(Example STATIC Example.cpp Example.hpp)
target_link_libraries(Example PRIVATE nlohmann_json)

add_executable(main main.cpp)
target_link_libraries(main PRIVATE Example)

还考虑在导出目标的符号或标头时将链接目标公开。


在您的示例中,您将nlohmann/json.hpp包含在example.hpp中。因此,根据此标头进行构建的每个人都需要知道nlohmann/json.hpp存储在哪里。

错误似乎来自编译
example
库。您的代码没有将该库与
nlohmann\u json
链接。t禁用编译任何内容从依赖库中,除了您真正需要的之外,我建议
add_子目录(nlohmann_json EXCLUDE_from_ALL)
错误似乎来自编译
示例库。您的代码没有将该库链接到
nlohmann\u json
。若要禁用从依赖库编译除您确切需要之外的任何内容,我建议
添加子目录(nlohmann\u json从\u ALL中排除\u)
谢谢!它在链接目标公开后起作用。谢谢!它在链接目标公开后起作用。
cmake_minimum_required(VERSION 3.0.0)
project(MWE VERSION 0.1.0)

# Use C++17
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_subdirectory(nlohmann_json)

add_library(Example STATIC Example.cpp Example.hpp)
target_link_libraries(Example PRIVATE nlohmann_json)

add_executable(main main.cpp)
target_link_libraries(main PRIVATE Example)