仅标头的C ++接口库在第二个相关的CMake项目中显示链接器错误

时间:2019-05-20 13:44:17

标签: c++ cmake linker

myapp -可执行文件

mylib1 -具有源和标题的库

mylib2 -仅限标头的库

这是我的依赖链,

myapp -> mylib1 -> mylib2

mylib2编译正常(已通过测试检查)

mylib1编译正常

myapp编译失败并显示错误

Target "myapp" links to target "mylib2::mylib2" but the target
  was not found.  Perhaps a find_package() call is missing for an IMPORTED
  target, or an ALIAS target is missing?

当我使用target_link_libraries(myapp PRIVATE mylib2)时。我没有错误! 但我想在mylib1中包含mylib2

mylib2的CMakeLists.txt

add_library(mylib2 INTERFACE)
target_include_directories(mylib2 INTERFACE
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
    $<INSTALL_INTERFACE:include>)

mylib1的CMakeLists.txt

find_package(mylib2)
add_library(mylib1 ${mylib1_SOURCES})
target_include_directories(mylib1 PUBLIC
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
    $<INSTALL_INTERFACE:include>)
target_link_libraries(mylib1 PUBLIC mylib2) #lib2 holds some important structs that i need to use in myapp

最后是myapp的CMakeLists.txt

find_package(mylib1)
add_executable(myapp app.cpp)
target_link_libraries(myapp PRIVATE mylib1)

在myapp的CMakeLists.txt中,当我添加以下行时。 CMake运行没有任何错误

find_package(mylib2)
# note there is no target_link_libraries(myapp mylib2)

但是如果没有上述内容,我会在开始时遇到CMake错误。我继续在错误配置的cmake构建文件夹上使用make。我收到以下错误。

/usr/bin/ld: cannot find -lmylib2::mylib2

但是链接器不应在mylib2上运行,它是仅标头的库。 为什么要在myapp中加入find_package(mylib2)。有人可以帮助我了解这个问题。

0 个答案:

没有答案