Mixing C and assembly sources and build with cmake

34,534

Solution 1

CMake supports 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 options:

project(assembler C ASM)
set_property(SOURCE foo.s APPEND PROPERTY COMPILE_OPTIONS "-x" "assembler-with-cpp")
add_executable(hello foo.s bar.c)

Solution 2

Based on your solution, one of the simplest solutions is this one-liner:

SET(CMAKE_ASM_FLAGS "${CFLAGS} -x assembler-with-cpp")
Share:
34,534
andyinno
Author by

andyinno

Updated on July 09, 2022

Comments

  • andyinno
    andyinno almost 2 years

    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