I have a c++ project build with cmake.
add_executable(app foo.cpp bar.cpp main.cpp)
And I would like to use foo.cpp
and bar.cpp
in a test project.
I know I could build a library and then link it in both projects: like:
add_library(foobar foo.cpp bar.cpp)
add_executable(app main.cpp)
target_link_library(app foobar)
add_executable(test test.cpp)
target_link_library(test foobar)
But I would rather not change the app build system and link test
against the objects foo.o
and bar.o
.
Do you know how I could do that with cmake?
Preamble: you cannot link to an executable, so you have to change something in the way you build things.
Some solutions proposed in the comments are risky. Don't go for linking directly to object files, you'll end up with so much pain in trying to find where your object files end up when you change anything, cmake is meant to simplify this kind of things, the general suggestion is to use the right architecture for your project.
Proposed solutions:
add_library(foobar STATIC foo.cpp bar.cpp)
and keep the code you proposed in the beginning.