如何链接从另一个目录创建的cmake中的静态库?

时间:2019-07-06 01:37:02

标签: c++ cmake

对于我的cmake项目,我具有以下简化的目录结构

- project/
  - src/
    - network/
      - network.hpp
      - network.cpp
      - CMakeLists.txt
    - CMakeLists.txt
  - test/
    - network_test.cpp
    - CMakeLists.txt
- CMakeLists.txt

project/src/network/CMakeLists.txt中,建立了network静态库

add_library(network STATIC "")
target_sources(network
        PUBLIC
        network.hpp
        PRIVATE
        network.cpp)
target_include_directories(network PRIVATE ${Boost_INCLUDE_DIR})
target_link_libraries(network PRIVATE pthread)
target_link_libraries(network PRIVATE OpenSSL::SSL)

project/test/network_test.cpp中,我需要针对网络库进行链接,因为目标网络来自另一个目录,即使以下内容无法成功将network_cppnetwork进行链接。 / p>

顶级CMakeList.txt

cmake_minimum_required(VERSION 3.13)

project(Argon)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_subdirectory(src)

add_subdirectory(test)

src / network / CMakeList.txt

add_library(network STATIC "")
target_sources(network
        PUBLIC
        network.hpp
        PRIVATE
        network.cpp)
target_include_directories(network PRIVATE ${Boost_INCLUDE_DIR})
target_link_libraries(network PRIVATE pthread)
target_link_libraries(network PRIVATE OpenSSL::SSL)

test / network_test / CMakeList.txt

add_executable(network_test network_test.cpp)
target_link_libraries(network_test PRIVATE GTest::GTest)
target_link_libraries(network_test PRIVATE network)
enable_testing()
add_test(NAME google_test
        COMMAND $<TARGET_FILE:network_test>)

产生的错误如下:

usr/bin/ld: CMakeFiles/network_test.dir/network_test.cpp.o: in function `network_cpp_get_HEAD_does_not_throw_Test::TestBody()':
/home/bill/CLionProjects/Argon/test/network/network_test.cpp:8: undefined reference to `Argon::network::get_HEAD(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, boost::asio::io_context&)'

对此有什么解决方案?还是我的项目结构有缺陷?

0 个答案:

没有答案