CMake add_library包含其他库

时间:2017-07-25 13:54:17

标签: c++ cmake shared-libraries

我正在设计一个可以在我的其他C ++项目中链接的库集合。为了使集合易于使用,我希望能够链接到单个库,或链接到包含所有其他库的一个主库。如何在CMakeLists.txt文件中指定此内容?

例如:

add_library(library1 SHARED
    file1.cpp
    file2.cpp
)

add_library(library2 SHARED
    file3.cpp
    file4.cpp
)

# Define a master library that contains both of the others
add_library(master_library SHARED
    library1
    library2
)

是否有正确的方法可以通过CMake获得此功能?

编辑:

此问题与CMake: Is it possible to build an executable from only static libraries and no source?

不重复

这仅与共享库有关,与静态库或可执行文件无关。

2 个答案:

答案 0 :(得分:5)

这个解决方案似乎有效。

add_library(library1 SHARED
    file1.cpp
    file2.cpp
)

add_library(library2 SHARED
    file3.cpp
    file4.cpp
)

# dummy file is required to avoid a cmake error, but this
# "dummy" file serves no other purpose and is empty.
add_library(master_library SHARED
    dummy.cpp
)

# Link the master library with the other libraries
target_link_libraries(master_library
    library1
    library2
)

执行此操作后,我只能使用主库编译和链接代码。

答案 1 :(得分:0)

只需在项目中创建一个功能:

function(link_all_libs TARGET)
    target_link_libraries(TARGET library1 library2)
endfunction(link_all_libs)

现在你可以简单地说:

link_all_libs(myapp)