Mixing C and assembly sources and build with cmake

andyinno picture andyinno · Feb 28, 2013 · Viewed 26.8k times · Source

I'm using eclipse for building a avr-gcc project that mixes assembly code and C source files. I want to get rid of the automatic makefile generation of eclipse because I need to automate some process into the makefiles and for other reasons.

I used cmake some times ago and I was happy with it so I want to try to compile my source files using it. Everything run as expected with C sources. The problem is that at the end I need to compile some assembly files (actually 2) and add them to the target.

I googled around but I didn't found a way for doing this. someone have an idea on how to do this?

The problem is that in eclipse I have -x assembler-with-cpp

added to gcc argument list. I need to find a way for selectively add this param to the standard gcc argument list only for the asm files. I didn't find around any way for doing this.

thank you in advance

SOLUTION: set in CMakeLists.txt every file to compile in the same list

enable_language(C ASM)

set ( SOURCES 
    foo.c
    bar.c
    foobar.s
)

add_executable(program  ${SOURCES} ) 

in the Toolchain file you should place:

SET(ASM_OPTIONS "-x assembler-with-cpp")
SET(CMAKE_ASM_FLAGS "${CFLAGS} ${ASM_OPTIONS}" )

the second line is just if you need to pass extra options while compiling asm files. I wanted to pass all the CFLAGS plus some ASM_OPTIONS

Answer

sakra picture sakra · Feb 28, 2013

CMake 2.8 should support assembler out of the box. Just be sure to enable the "ASM" language in your project. If an assembler source file needs preprocessing, also set the source file's compilation flag:

project(assembler C ASM)
set_source_files_properties(foo.s PROPERTIES COMPILE_FLAGS "-x assembler-with-cpp")
add_executable(hello foo.s bar.c)