C语言 混合 C 和汇编源代码并使用 cmake 构建

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15132185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:29:58  来源:igfitidea点击:

Mixing C and assembly sources and build with cmake

cgccassemblycmakeavr

提问by andyinno

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.

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

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.

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

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

问题是在 eclipse 中我有 -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.

添加到 gcc 参数列表。我需要找到一种方法来选择性地将此参数添加到标准 gcc 参数列表中,仅适用于 asm 文件。我没有找到任何方法来做到这一点。

thank you in advance

先感谢您

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

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

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

第二行只是在编译 asm 文件时需要传递额外选项。我想通过所有的 CFLAGS 加上一些 ASM_OPTIONS

采纳答案by sakra

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:

CMake 2.8 应该支持开箱即用的汇编器。请务必在您的项目中启用“ASM”语言。如果汇编源文件需要预处理,还要设置源文件的编译标志:

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

回答by Uli K?hler

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

根据您的解决方案,最简单的解决方案之一是这种单行:

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