4

我有很多看起来像这样的库:

libs

     \lib1-- src
          \- include

      \lib2--src
           \- include

其中 lib2 需要 lib1。我这样做的方法是做这样的事情:

lib2/CMakeLists.txt: 
    include ../lib1/include
    target_link_libraries(lib2 lib1)

如何在 lib2 库中包含 lib1 头文件/包含文件?我目前正在尝试执行此操作,但在编译过程中出现 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 个其他图书馆,等等,这变得非常乏味。

4

1 回答 1

5

当你有一个像这样重复的文件夹结构时,创建一个自动化这种东西的宏通常很方便。

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)
于 2013-07-12T00:33:33.747 回答