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
文章分享
如果这篇文章对你有帮助,欢迎分享给更多人!











