简体   繁体   中英

cmake use library target in added executable

I have started a new c++ project which has several executables and a relatively large amount of shared code.

root
    CMakeLists.txt
    Common
    Application1 
    Application2
    ....

So the multiple applications all get compiled with their unittests without issues. But since they depend on the shared code in Common I have the recompile the shared code for each project once I update something there.

So I am wondering if it would be possible to add the library target. And build that first. And then I link my code to that library.

This seems like a relatively normal thing to do but I can't find anything about it on google.

Any help is appreciated.

Yes this is possible.
You have to add a CMakeLists file to the Common folder and in it you compile it as a library

# Add required source files.
add_library(Common ...)

# Include required header files. They must be public to be recognized where they are needed.
target_include_directories(Common PUBLIC ...)

Then in the project's CMakeLists you use:

add_subdirectory(Common) // Will call the other CMakelists
...
# Link to the required libraries.
target_link_libraries(Application Common)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM