add_library(target1 funtion.c target1.c )
add_library(target2 funtion.c target2.c )
add_executable(main.out main.c)
target_link_libraries(main.out target1 target2 ${LDFLAGS})
Here is my CMakeLists.txt
above.
Both targets need to use the source file function.c
. It is able to run though. My concern is that maybe it is not a good behavior for writing CMakeList.txt
?
It's totally fine to use the same source file whatever number of times. Sometimes it's even necessary, if you want to compile the same source with different pre-processor/compiler flags.
But if you are concerned with compilation time, you could:
funtion.c
to separate static library and link target1
and target2
libraries against it.function.c
and archive output object file to target1
and target2
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With