I have many source files in source directory. For example a.c, b.c, c.c and want to compile it by gcc with -combine option.
set(CMAKE_C_FLAGS "-combine")
set(SRC a.c b.c c.c)
add_executable(a.out ${SRC})
Cmake compiles each *.c file in object file, but I want to compile all sources to one object. How can I get this.
In terms of gcc:
gcc -combine a.c b.c c.c -o module.o
ar rcs library.a module.o
But cmake uses per-module translation of sources.
gcc a.c -o a.o
gcc b.c -o b.o
gcc c.c -o c.o
ar rcs library.a a.o b.o c.o
Translation with combine could speed up performance of program. I couldn’t use LTO which could resolve the same problem in cmake due to old version of gcc.
Thanks.
Use add_custom_target/add_custom_command.
In any way it is non-portable construction, so here simple example
[project root] with two folders in it [src] – here N files, [build] for binary, and CMakeLists.txt
In build folder run cmake ..; make VERBOSE=1 whole
This will make the work for you.
But, -fwhole-program work only with executable, as per documentation.
So you mast have main defined anywhere in your source files.