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

What is the correct way to configure target_link_libraries in CMakeList when using multiple libraries? Getting Cannot specify link libraries error

我正在开发一个使用 fast-cpp-csv-parser and 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 相关的东西打包在一起。