Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build object files only once with cmake?

Tags:

I have a CMakeLists.txt file that looks like this:

add_executable(exec1 exec1.c source1.c source2.c source3.c) add_executable(exec2 exec2.c source1.c source2.c source3.c) add_executable(exec3 exec3.c source1.c source2.c source3.c) 

The source1.o source2.o source3.o files take a really long time to build, and since they are common to all the executables, I want each of them to be built only once. The current behavior for cmake, however, is to rebuild them for each exec target separately, which is an unnecessary duplication of effort.

Is there a way to tell cmake to build object files only once?

like image 909
D R Avatar asked Sep 07 '09 11:09

D R


People also ask

Where does CMake store object files?

Directory Structure The source directory is where the source code for the project is located. This is also where the CMakeLists files will be found. The binary directory is sometimes referred to as the build directory and is where CMake will put the resulting object files, libraries, and executables.

Does CMake create Makefiles?

CMake generates a Unix makefile by default when run from the command line in a Unix-like environment. Of course, you can generate makefiles explicitly using the -G option. When generating a makefile, you should also define the CMAKE_BUILD_TYPE variable.


1 Answers

No. This would be difficult to achieve since the source files could be compiled with different compiler options, post-build steps, etc.

What you can do is to put the object files into a static library and link with that instead:

add_library(mylib STATIC source1.c source2.c) add_executable(myexe source3.c) target_link_libraries(myexe mylib) 

EDIT: of course, you can put it in a shared library as well.

like image 78
JesperE Avatar answered Sep 22 '22 17:09

JesperE