视频加载失败

CMake:add_subdirectory 与 add_library 的区别

171 字
1 分钟
CMake:add_subdirectory 与 add_library 的区别

If you want to compile the code into your project directly, that’s not a library, that’s just code.

A library is a distinct compiled and linked object. A static library is a .a/.lib file, a shared library is a .so/.dll file.

If you just want to add source code files to your current target, you do so with target_sources() and that’s it. No need to create a library at all.

So with that in mind, we can now talk about the commands:

add_subdirectory(): This has nothing to do with libraries, this just runs the CML of a subdirectory. The code in that CML can do anything, maybe it creates a library, maybe it adds source code files to your current project, maybe it plays a little song and prints messages to your build log

add_library(): This creates a new library target, a new .lib or .dll file to be compiled and linked. It is the sister command to add_executable(), which creates a new binary executable target (.exe on Windows)

target_link_libraries(): This links a library, maybe created in your current project or maybe an imported library from find_package()/find_library(), into a target

文章分享

如果这篇文章对你有帮助,欢迎分享给更多人!

CMake:add_subdirectory 与 add_library 的区别
https://blog.81vm3.xyz/posts/cmake-add-subdirectory-vs-add-library/
作者
Blume
发布于
2025-07-15
许可协议
CC BY-NC-SA 4.0
Profile Image of the Author
Blume
I build interesting things.
公告
欢迎来到我的博客!
分类
标签
最新动态
站点统计
文章
38
分类
5
标签
98
总字数
23,078
运行时长
0 天
最后活动
0 天前
站点信息
构建平台
Local
博客版本
Firefly v6.16.8
文章许可
CC BY-NC-SA 4.0

当前页面没有目录

文章目录

当前页面没有目录