gpt4 book ai didi

c++ - 使用多个库时,在 CMakeList 中配置 target_link_libraries 的正确方法是什么?获取无法指定链接库错误

转载 作者:行者123 更新时间:2023-11-30 03:28:18 26 4
gpt4 key购买 nike

我正在做一个使用 fast-cpp-csv-parser 的项目和 date库并想添加 zmq (0mq) 但是无法让 CMakeList 工作。

以下是一个有效的 CMakeList.txt:

cmake_minimum_required(VERSION 3.7)
project(sample_project)

set(CMAKE_CXX_STANDARD 14)
set(SOURCE_FILES source/main.cpp include/csv.h include/date.h)


find_package (Threads)
add_executable(sample_project ${SOURCE_FILES})
target_link_libraries (sample_project ${CMAKE_THREAD_LIBS_INIT})

根据 zmq instructions必须将以下内容添加到 CMakeLists.txt(ZMQ 和 CPPZMQ 已安装)。

find_package(cppzmq)
if(cppzmq_FOUND)
include_directories(${cppzmq_INCLUDE_DIR})
target_link_libraries(sample_project ${cppzmq_LIBRARY})
endif()

当我将上面的代码添加到 CMakeLists.txt 时,它看起来像这样:

cmake_minimum_required(VERSION 3.7)
project(sample_project)

set(CMAKE_CXX_STANDARD 14)
set(SOURCE_FILES source/main.cpp include/csv.h include/date.h)

find_package(cppzmq)
if(cppzmq_FOUND)
include_directories(${cppzmq_INCLUDE_DIR})
target_link_libraries(sample_project ${cppzmq_LIBRARY})
endif()

find_package (Threads)
add_executable(sample_project ${SOURCE_FILES})
target_link_libraries (sample_project ${CMAKE_THREAD_LIBS_INIT})

并导致以下错误:

CMake Warning at /usr/local/share/cmake/cppzmq/cppzmqConfig.cmake:44 (find_package):
By not providing "FindZeroMQ.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "ZeroMQ", but
CMake did not find one.

Could not find a package configuration file provided by "ZeroMQ" with any
of the following names:

ZeroMQConfig.cmake
zeromq-config.cmake

Add the installation prefix of "ZeroMQ" to CMAKE_PREFIX_PATH or set
"ZeroMQ_DIR" to a directory containing one of the above files. If "ZeroMQ"
provides a separate development package or SDK, be sure it has been
installed.
Call Stack (most recent call first):
CMakeLists.txt:7 (find_package)


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


-- Configuring incomplete, errors occurred!
See also "/home/greg/CLionProjects/sample_project/cmake-build-debug/CMakeFiles/CMakeOutput.log".
See also "/home/greg/CLionProjects/sample_project/cmake-build-debug/CMakeFiles/CMakeError.log".

[Finished]

如何使用 CMakeLists.txt 正确添加额外的库?

最佳答案

您必须重新排序您的 CMakeLists.txt,以便 target_link_libraries 位于 add_executable 之后。

例如:

cmake_minimum_required(VERSION 3.7)
project(sample_project)

set(CMAKE_CXX_STANDARD 14)
set(SOURCE_FILES source/main.cpp include/csv.h include/date.h)

find_package(cppzmq)
if(cppzmq_FOUND)
include_directories(${cppzmq_INCLUDE_DIR})
endif()

find_package (Threads)
add_executable(sample_project ${SOURCE_FILES})
target_link_libraries (sample_project ${CMAKE_THREAD_LIBS_INIT})

if(cppzmq_FOUND)
target_link_libraries(sample_project ${cppzmq_LIBRARY})
endif()

顺便说一句,我建议使用 target_include_directories而不是 include_directories。这也允许将所有 cppzmq 相关的东西打包在一起。

关于c++ - 使用多个库时,在 CMakeList 中配置 target_link_libraries 的正确方法是什么?获取无法指定链接库错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46702224/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com