gpt4 book ai didi

c++ - 包含其他库的 CMake add_library

转载 作者:太空宇宙 更新时间:2023-11-03 10:39:43 24 4
gpt4 key购买 nike

我正在设计一组可以在我的其他 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?

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

最佳答案

这个解决方案似乎有效。

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
)

完成此操作后,我能够仅使用 master 库编译和链接代码。

关于c++ - 包含其他库的 CMake add_library,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45305446/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com