Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake: multiple targets use the same source file

Tags:

c

cmake

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?

like image 705
Samuel Avatar asked Dec 27 '17 07:12

Samuel


1 Answers

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:

  • move funtion.c to separate static library and link target1 and target2 libraries against it.
  • Use object library for function.c and archive output object file to target1 and target2.
like image 65
ivaigult Avatar answered Oct 06 '22 01:10

ivaigult