我有很多库看起来像这样:
libs
\lib1-- src
\- include
\lib2--src
\- include
lib2需要lib1。我这样做的方法是做这样的事情:
lib2/CMakeLists.txt:
include ../lib1/include
target_link_libraries(lib2 lib1)
如何在lib2库中包含lib1 header / include文件? 我目前正在尝试这样做,但在编译过程中我遇到了lib2无法找到lib1头文件的错误。
libs/CMakeLists.txt:
file(GLOB lib1_src
"lib1/src/*.cc"
)
#header files
file (GLOB lib1_h
"lib1/include/*.h"
)
file(GLOB lib2_src
"lib2/src/*.cc"
)
#header files
file (GLOB lib2_h
"lib2/include/*.h"
)
add_library(lib1 ${lib1_src} ${lib1_h})
add_library(lib2 ${lib2_src} ${lib2_h})
target_link_libraries(lib2 lib1)
我可以通过将include_directories(lib1/include)
添加到libs/CMakeLists.txt
来使其工作,但我已经达到了一个图书馆需要3个其他图书馆,其中每个图书馆需要3个其他图书等等,并且它变得非常漂亮乏味的。
答案 0 :(得分:6)
当你有一个像这样的重复文件夹结构时,创建一个自动化这种东西的宏通常很方便。
MACRO(ADD_SUBLIB libname)
#Compute required sources
file(GLOB sublib_sources "${libname}/src/*.cc")
#Create library
ADD_LIBRARY(${libname} SHARED ${sublib_sources})
#add this library's header folder to the global include set
INCLUDE_DIRECTORIES("${libname}/include")
ENDMACRO(ADD_SUBLIB)
#Call macro once per library
ADD_SUBLIB(lib1)
ADD_SUBLIB(lib2)
ADD_SUBLIB(lib3)
#Sample executable, who as a side effect of the macro calls will
# be able to include.
ADD_EXECUTABLE(myprog main.cpp)
#Link any of the created libraries
TARGET_LINK_LIBRARIES(myprog lib1 lib2 lib3)