[CMake] Can two executables share one object file?

Stuart Herring cmake at stuartherring.com
Fri Jun 8 23:22:28 EDT 2007


On 6/9/07, Clark J. Wang <dearvoid at gmail.com> wrote:
>
> but it failed to cmake. So how can I let two executables share one single
> object file?
>
You don't really want to.
The cost of compiling the same source file twice is minimal, and it's
much safer to compile them separately - it means that if you later
need to add different compile flags for the two executables that you
can without worry of one contaminating the other.

However, you could always build a static library and create the
executable from that:

ADD_LIBRARY(hellomain STATIC main.c)
ADD_EXECUTABLE(hello1 hello1.c)
ADD_EXECUTABLE(hello2 hello2.c)

TARGET_LINK_LIBRARIES(hello1 hellomain)
TARGET_LINK_LIBRARIES(hello2 hellomain)

Regards,
Stuart.


More information about the CMake mailing list