Reputation:
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.
Upvotes: 0
Views: 50
Reputation: 169
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)
Upvotes: 2