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

时间:2017-10-12 05:44:32

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

我正在开发一个使用fast-cpp-csv-parserdate库的项目,并希望添加zmq(0mq),但无法使CMakeList工作。

以下是有效的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})

CMakeLists.txt

根据zmq instructions,必须将以下内容添加到find_package(cppzmq) if(cppzmq_FOUND) include_directories(${cppzmq_INCLUDE_DIR}) target_link_libraries(sample_project ${cppzmq_LIBRARY}) endif() (已安装ZMQ和CPPZMQ)。

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

如何使用populateSpinner (int bgCount, int Endcount)正确添加其他库?

1 个答案:

答案 0 :(得分:2)

您必须重新排序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相关的内容打包在一起。