8

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.

7
  • Are you sure an option -combine exists? I have a feeling that that's not what you're trying to do. Maybe you mean -fwhole-program, which is morally equivalent to concatenating all your source files together. But more likely you're just trying to make a reusable library. Use -shared to make a dynamic library, or just use the ar program to combine lots of .o files into a static library. CMake can automate both for you.
    – Kerrek SB
    Jun 14, 2011 at 19:11
  • 2
    Yes, I'm sure. This option exist in gcc version 4.4/4.5 Jun 16, 2011 at 8:29
  • Interesting, it disappeared in 4.6! :-) What's its purpose? Anything you can't achieve with -fwhole-program?
    – Kerrek SB
    Jun 16, 2011 at 10:37
  • 2
    Yes, 4.6 doesn't have -combine, but our team use gcc-4.3 and 4.4. There are the same problem with -fwhole-program as for combine, because it need to compile all source files in one compilation unit to create 1 object file. Jun 17, 2011 at 7:03
  • I would also like to know the answer to this. I'm currently using a custom Makefile system instead of CMake but I'd like to switch. But performance via inter-module optimization is more important than CMake.
    – Zan Lynx
    Sep 6, 2011 at 20:56

1 Answer 1

1

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

cmake_minimum_required(VERSION 2.6.0 FATAL_ERROR)
set(TARGET_NAME whole)
project(${TARGET_NAME} C)

file(GLOB SRC_FILES src/*.c)

add_custom_target(${TARGET_NAME} ${CMAKE_C_COMPILER} -flto -fwhole-program ${SRC_FILES} -o ${TARGET_NAME})

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.

-fwhole-program Assume that the current compilation unit represents the whole program being compiled. All public functions and variables with the exception of main and those merged by attribute externally_visible become static functions and in effect are optimized more aggressively by interprocedural optimizers.

So you mast have main defined anywhere in your source files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.