混合C和汇编源代码并使用cmake进行构建 [英] Mixing C and assembly sources and build with cmake

查看:1711
本文介绍了混合C和汇编源代码并使用cmake进行构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用eclipse构建一个将汇编代码和C源文件混合在一起的avr-gcc项目. 我想摆脱自动生成eclipse的原因,因为我需要出于某些原因而将一些过程自动化到makefile中.

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.

我之前使用过cmake,对此我感到很满意,所以我想尝试使用它来编译我的源文件.一切都按预期运行,使用C源代码.问题是,最后我需要编译一些程序集文件(实际上是2个)并将它们添加到目标中.

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.

我在Google上四处搜寻,但没有找到一种方法来做到这一点. 有人对如何执行此操作有想法吗?

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

问题是在月食中 -x带有cpp的汇编器

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

已添加到gcc参数列表.我需要找到一种仅针对asm文件有选择地将此参数添加到标准gcc参数列表的方法.我没有找到实现此目的的任何方法.

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.

先谢谢您

解决方案: 在CMakeLists.txt中设置每个文件,以在同一列表中进行编译

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}" )

第二行只是在编译asm文件时需要传递额外的选项.我想通过所有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

推荐答案

CMake 2.8应该开箱即用地支持汇编程序.只要确保在您的项目中启用"ASM"语言即可.如果汇编器源文件需要预处理,则还要设置源文件的编译标志:

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)

这篇关于混合C和汇编源代码并使用cmake进行构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆