0

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.

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.