pra*_*ale 5 c++ cmake nlohmann-json
我正在使用以下 JSON 解析器: https: //github.com/nlohmann/json
以下是我构建的步骤:
2074 git clone https://github.com/nlohmann/json.git
2075 git branch
2076 ls
2077 cd json/
2078 git branch
2079 git pull
2080 ls
2081 vi CODE_OF_CONDUCT.md
2082 mkdir build
2083 cd build/
2084 ls
2085 cmake ..
2086 cmake --build .
2087 ctest --output-on-failure
Run Code Online (Sandbox Code Playgroud)
单元测试通过。正如文档所述,我没有看到正在构建的库。
我正在尝试为解析器构建一个简单的 hello world 程序。这是代码:
#include <nlohmann/json.hpp>
#include<string.h>
// for convenience
using json = nlohmann::json;
int
main(int argc, char *argv[])
{
std::ifstream ifs("test.json");
json jf = json::parse(ifs);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
和 CMake 文件:
# CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
# set the project name
project(my_json CXX)
find_package(nlohmann_json 3.2.0 REQUIRED)
Run Code Online (Sandbox Code Playgroud)
但是,CMake 找不到包 nlohmann_json。
请建议如何构建这个示例。我打算使用外部库方法来构建此代码。
默认行为find_package是期望找到系统上安装的软件包。
如果您有一个简单的本地克隆,那么您应该Find***.cmake在模块路径中提供一个与模块名称匹配的文件。
例如,创建一个包含Findnlohmann_json.cmake如下内容的文件:
if( TARGET nlohmann_json )
return()
endif()
if(NOT NLOHMANNJSON_ROOT)
set(NLOHMANNJSON_ROOT "${PROJECT_SOURCE_DIR}/lib/json")
endif()
add_library( nlohmann_json INTERFACE )
target_include_directories(
nlohmann_json
INTERFACE
${NLOHMANNJSON_ROOT}/include
)
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请参阅https://cmake.org/cmake/help/latest/command/find_package.html#search-procedure。
也就是说,如果您将库保留在源代码树中,您可能会发现add_subdirectory从项目的调用更容易CMakeLists.txt:
project(my_json CXX)
add_subdirectory( lib/nlohmann_json )
//...
target_link_libraries( myApp PRIVATE nlohmann_json )
Run Code Online (Sandbox Code Playgroud)
请注意,并非所有库都准备好以这种方式包含在内
| 归档时间: |
|
| 查看次数: |
7782 次 |
| 最近记录: |