Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mixing C and assembly sources and build with cmake

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

like image 633
andyinno Avatar asked Feb 28 '13 09:02

andyinno


People also ask

Can you use CMake with C?

In the C/C++ ecosystem, the best tool for project configuration is CMake. CMake allows you to specify the build of a project, in files named CMakeLists. txt, with a simple syntax (much simpler than writing Makefiles).

Can CMake cross compile?

Cross-compiling is fully supported by CMake, ranging from cross-compiling from Linux to Windows; cross-compiling for supercomputers, through to cross-compiling for small embedded devices without an operating system (OS).

Does CMake include GCC?

a. This "cmake and make" stuffs actually use g++ / gcc in its implementation.

Does CMake include a compiler?

When CMake executes the project() call, it looks for a default compiler executable and determines the way for use it: default compiler flags, default linker flags, compile features, etc. And CMake stores path to that default compiler executable in the CMAKE_C_COMPILER variable.


2 Answers

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)
like image 172
sakra Avatar answered Oct 03 '22 06:10

sakra


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

SET(CMAKE_ASM_FLAGS "${CFLAGS} -x assembler-with-cpp")
like image 29
Uli Köhler Avatar answered Oct 03 '22 07:10

Uli Köhler