[CMake] Sharing object files among multiple executables

Braddock Gaskill braddock at braddock.com
Fri Mar 9 17:19:21 EST 2007


How can I share a common object file among multiple executable targets,
without CMake re-compiling the common file for every target?

In a simple example (under Linux), I have:
hello_main.cpp
world_main.cpp
and a common object that needs to be linked to both
common.cpp

cat >CMakeLists.txt <<EOF
ADD_EXECUTABLE("hello" hello_main.cpp common.cpp)

ADD_EXECUTABLE("world" world_main.cpp common.cpp)

EOF

cmake .
make
Scanning dependencies of target hello

[ 25%] Building CXX object CMakeFiles/hello.dir/hello_main.o

[ 50%] Building CXX object CMakeFiles/hello.dir/common.o

Linking CXX executable hello

[ 50%] Built target hello

Scanning dependencies of target world

[ 75%] Building CXX object CMakeFiles/world.dir/world_main.o

[100%] Building CXX object CMakeFiles/world.dir/common.o

Linking CXX executable world

[100%] Built target world


ie, it compiled common.o twice, once in world.dir, once in hello.dir.

In my real-life case I have several dozen unit test executables with a
common object, and rebuilding that common object is adding significantly
to the build time.  I'd prefer not to build a separate library.

Any assistance is appreciated.

Thanks,
Braddock Gaskill
Dockside Vision Inc





More information about the CMake mailing list