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

标签 c++ cmake shared-libraries static-libraries zeromq

我正在做一个使用 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/

相关文章:

C++: 'Foo' 中成员 'f' 的错误请求,它是非类类型 'Foo*'

C++ std::set 排序不适用于客户类*

c++ - ARM 汇编和 C++

python - 编译Boost.Numpy时CMake错误 "NumPy import failure"

c++ - 在 C 中声明结构的新实例

c++ - Travis 和 Coverity Scan - 一切都在构建,但 coverity 似乎没有运行

c++ - crt1.o : In function `_start' : (.text+0x20): undefined reference to `main'

java - 为 Raspberry Pi 编译 JD2XX

linux - ldd 是否也显示依赖关系?

gcc - 为什么 GCC 编译的应用程序总是包含 _mcount 符号?