将本地JSONCpp安装与CMake和ExternalProject_Add链接
Linking a local JSONCpp install with CMake and ExternalProject_Add
我正在尝试使用CMake来构建一个使用JSONCpp的项目。我想下载JSONCp,在本地编译并链接到它。项目结构在一个单独的位置有第三方目录的CMake文件。我犯了两个错误。
- CMake进程试图全局安装一个文件,但我无法识别CMake变量,无法将其设置为本地安装或根本不安装
- 当我运行make时,链接器找不到libjsoncpp,尽管我试图设置所有可能的变量,使其指向正确的方向
有人知道如何关闭全局安装的尝试吗?如何以可移植的方式将链接器指向正确的位置?
我使用的项目结构是:
./jsoncpp_build_test.cc
./third_party/CMakeLists.txt.jsoncpp
./third_party/CMakeLists.txt
./CMakeLists.txt
源文件jsoncpp_build_test
为:
#include <iostream>
#include "json/json.h"
int main(int argc, char **argv) {
Json::Value json_doc;
std::cout << json_doc.asString() << std::endl;
return 0;
}
主./CMakeLists.txt
文件。
project(jsontest CXX C)
cmake_minimum_required(VERSION 2.8.12)
add_subdirectory(third_party)
set(jsoncpp_lib_dir ${CMAKE_BINARY_DIR}/jsoncpp-build/src/lib_json)
set(jsoncpp_src_dir ${CMAKE_BINARY_DIR}/jsoncpp-src/include)
add_executable(jsoncpp_build_test "jsoncpp_build_test.cc")
target_include_directories(jsoncpp_build_test PRIVATE ${jsoncpp_src_dir})
target_include_directories(jsoncpp_build_test PRIVATE ${jsoncpp_lib_dir})
target_link_libraries(jsoncpp_build_test jsoncpp)
third_party/CMakeLists.txt
文件。我试图下载JSONCp并安装它,以避免用户必须运行多个与构建相关的命令。
configure_file(CMakeLists.txt.jsoncpp
jsoncpp/CMakeLists.txt)
set(jsoncpp_work_dir "${CMAKE_BINARY_DIR}/third_party/jsoncpp")
set(jsoncpp_build_dir "${CMAKE_BINARY_DIR}/jsoncpp-build")
set(jsoncpp_lib_dir "${CMAKE_BINARY_DIR}/jsoncpp-lib")
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
WORKING_DIRECTORY ${jsoncpp_work_dir} )
message(STATUS "INSTALL DIR is ${CMAKE_INSTALL_PREFIX}")
set(CMAKE_INSTALL_COMPONENT "${CMAKE_BINARY_DIR}/jsoncpp-lib")
message(STATUS "INSTALL COMPONENT is ${CMAKE_INSTALL_COMPONENT}")
execute_process(COMMAND ${CMAKE_COMMAND} --build .
WORKING_DIRECTORY ${jsoncpp_work_dir} )
最后是使用ExternalProject_Add模块的third_party/CMakeLists.txt.json
文件。
cmake_minimum_required(VERSION 2.8.12)
project(jsoncpp-download NONE)
set(jsoncpp_install_prefix "${CMAKE_BINARY_DIR}/jsoncpp-lib")
set(CMAKE_INSTALL_COMPONENT "${jsoncpp_install_prefix}")
include(ExternalProject)
ExternalProject_Add(jsoncpp-parser
GIT_REPOSITORY https://github.com/open-source-parsers/jsoncpp.git
GIT_TAG master
SOURCE_DIR "${CMAKE_BINARY_DIR}/jsoncpp-src"
BINARY_DIR "${CMAKE_BINARY_DIR}/jsoncpp-build"
INSTALL_DIR "${CMAKE_BINARY_DIR}/jsoncpp-lib"
CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX=${jsoncpp_install_prefix}"
)
cd build && cmake ../
的结果如下。不过,我不想在全局范围内安装jsoncpp。
-- The CXX compiler identification is GNU 4.8.4
-- The C compiler identification is GNU 4.8.4
...
[100%] Built target jsoncpp_test
[ 87%] Performing install step for 'jsoncpp-parser'
[ 50%] Built target jsoncpp_lib_static
[ 66%] Built target jsontestrunner_exe
[100%] Built target jsoncpp_test
Install the project...
-- Install configuration: "Release"
CMake Error at cmake_install.cmake:44 (FILE):
file cannot create directory: /lib/pkgconfig. Maybe need administrative privileges.
make[3]: *** [install] Error 1
make[2]: *** [jsoncpp-parser-prefix/src/jsoncpp-parser-stamp/jsoncpp-parser-install] Error 2
make[1]: *** [CMakeFiles/jsoncpp-parser.dir/all] Error 2
make: *** [all] Error 2
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/cmake_test/build
make
的结果是:
Scanning dependencies of target jsoncpp_build_test
[100%] Building CXX object
CMakeFiles/jsoncpp_build_test.dir/jsoncpp_build_test.cc.o
Linking CXX executable jsoncpp_build_test
/usr/bin/ld: cannot find -ljsoncpp
collect2: error: ld returned 1 exit status
make[2]: *** [jsoncpp_build_test] Error 1
make[1]: *** [CMakeFiles/jsoncpp_build_test.dir/all] Error 2
make: *** [all] Error 2
默认情况下,命令configure_file
将${var_name}
的所有出现替换为相应变量的值。
因此,它将${jsoncpp_install_prefix}
替换为空字符串,因为在调用方的上下文中没有jsoncpp_install_prefix变量。
如果您只想替换一些变量的引用,可以使用命令的@ONLY
选项,因此只有@var_name@
出现将被替换,但${var_name}
将保持不变:
第三方/CMakeLists.txt.json:
cmake_minimum_required(VERSION 2.8.12)
project(jsoncpp-download NONE)
# This reference will be replaced when configured
set(jsoncpp_install_prefix "@CMAKE_BINARY_DIR@/jsoncpp-lib")
# But this reference remains unchanged, until resulted CMakeLists.txt will be interpreted.
set(CMAKE_INSTALL_COMPONENT "${jsoncpp_install_prefix}")
...
第三方/CMakeLists.txt:
configure_file(CMakeLists.txt.json
jsoncpp/CMakeLists.txt
@ONLY)
相关文章:
- Visual Studio - Cmake Project - Add NetCDF
- CMakeLists:使用 ExternalProject 从 Github 添加源文件
- 在 atomic() 中 ++、add operation 和 fetch_add() 有什么区别
- wxImageList::Add()始终返回0
- 函数 C::add() 中不允许将'C *'隐式转换为'A *'
- C++有"not equal compare and exchange"或"fetch add on not equal"吗?
- gRPC trace.cc 的方法 TraceFlagList::Add(TraceFlag* flag) 使其链接列表
- 如何将用户定义的格式标志"add" basic_ostream?
- 'Cannot add two pointers'添加带有 WCHAR 的 LPCWSTR
- 在创建完整对象之前编写 if-can-add check 方法的正确方法?
- CMake add library libpq (postgreSQL) mac c++ clion
- 收到错误:C3867 '<int>Template_class::add':非标准语法;将指针指向带有模板的函数时
- 呼叫'php :: Extension :: add()没有匹配函数
- 使用intel内联汇编程序对bigint-add进行进位编码
- 如何在配置主项目的同时构建cmake ExternalProject
- C++ 中头文件中的 Add 方法出错
- 二进制'+=':未找到采用类型 'Add' C++ 的全局运算符
- 如果我们使用链表数组来实现哈希表,则可以以不需要遍历的方式实现"add"。这是真的还是假的?
- 为什么 cmake 在定义 CXX 时忽略 ADD(SYSTEM) 头文件?
- "QFileSystemWatcher: failed to add paths"但仍然有效