In cmake, how to specify dependencies of subdirectories in a scalable way?

ε祈祈猫儿з 提交于 2019-12-01 20:59:30

For the 1 question:

In lib2/CMakeLists.txt you should put this:

target_link_libraries(lib2 lib1)

And in app/CMakeLists.txt:

target_link_libraries(app lib2)

Now if you try to build app, CMake will check if lib2 is up to date and if not - rebuild lib1 and lib2.

For the 2 question:

You can guard add_subdirectory(lib3) invocation with if() block based on option() variable.

Another way - in lib3/CMakeLists.txt:

add_library(lib3 ${SRCS} EXCLUDE_FROM_ALL)

This would make CMake to not adding lib3 target into all target. This target will still be built if you are trying to build something depending on it, or issue make lib3 manually.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!