John Greene
John Greene

Reputation: 2596

Creating multiple objects from a single source file using cmake

Using cmake, is there a way to generate n-unique object files from a single source file?

I saw one solution for autotool (of which I know how to do) but nothing for cmake ... yet.

#  Make three unique object files 
#  from a single source file

set_source_files_properties(source.c
    TARGET source_1
    OBJECT_OUTPUT source_1.o
    COMPILER_FLAGS -DCODE_LOGIC1
)
set_source_files_properties(source.c
    TARGET source_2
    OBJECT_OUTPUT source_2.o
    COMPILER_FLAGS -DCODE_LOGIC2
)
set_source_files_properties(source.c
    TARGET source_3
    OBJECT_OUTPUT source_3.o
    COMPILER_FLAGS -DCODE_LOGIC3
)
add_executable(myexec source_1 source_2 source_3)

The above doesn't work.

Upvotes: 2

Views: 457

Answers (1)

serkan.tuerker
serkan.tuerker

Reputation: 1821

You are looking for add_library(<name> OBJECT <source>). With that in mind, you can do:

add_library(source_1 OBJECT source.c)
target_compile_options(source_1 PUBLIC -DLOGIC1)

add_library(source_2 OBJECT source.c)
target_compile_options(source_2 PUBLIC -DLOGIC2)

add_library(source_3 OBJECT source.c)
target_compile_options(source_3 PUBLIC -DLOGIC3)

add_executable(myexec $<TARGET_OBJECTS:source_1> $<TARGET_OBJECTS:source_2> $<TARGET_OBJECTS:source_3>)

Upvotes: 5

Related Questions