Mixing C and assembly sources and build with cmake

让人想犯罪 __ 提交于 2019-12-03 11:25:09

问题


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


回答1:


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)



回答2:


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

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


来源:https://stackoverflow.com/questions/15132185/mixing-c-and-assembly-sources-and-build-with-cmake

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!