[英]#include header with C declarations in an assembly file without errors?
I have an assembly file ( asm.S
) which needs a constant #define
'd in a C header file ( c_decls.h
). 我有一个组件文件(
asm.S
这需要一个常数) #define
“d在C头文件( c_decls.h
)。 The header file contains C function declarations in addition to the #define
I want. 除了我想要的
#define
之外,头文件还包含C函数声明。 Unfortunately, gcc
barfs when trying to compile the assembly file. 不幸的是,
gcc
barfs在尝试编译汇编文件时。 For example, 例如,
c_decls.h c_decls.h
#ifndef __c_decls_h__
#define __c_decls_h__
#define I_NEED_THIS 0xBEEF
int foo(int bar);
#endif
asm.S asm.S
#include "c_decls.h"
.globl main
main:
pushl %ebp
movl %esp, %ebp
movl $I_NEED_THIS, %eax
leave
ret
Output 产量
> gcc -m32 asm.S
> gcc -m32 asm.S
c_decls.h: Assembler messages:c_decls.h:汇编程序消息:
c_decls.h:6: Error: junk '(int bar)' after expressionc_decls.h:6:错误:表达式后的垃圾'(int bar)'
c_decls.h:6: Error: suffix or operands invalid for 'int'c_decls.h:6:错误:后缀或操作数对'int'无效
Is there a way to #include
a C header file that contains function declarations in an assembly file? 有没有办法
#include
包含程序集文件中的函数声明的C头文件? (Changing the header or moving/redefining the #define
is not an option.) (更改标题或移动/重新定义
#define
不是一个选项。)
Use the -dM
option for cpp
to get just the #defines out of your header files, and include that file instead. 对
cpp
使用-dM
选项-dM
文件中获取#defines,而是包含该文件。
cpp -dM c_decls.h > bare_c_decls.h
Now include bare_c_decls.h
in your .S file. 现在在.S文件中包含
bare_c_decls.h
。 And if you can't change the #include in the .S file, generate the bare header files in another directory, and put that include path on your compiler/assembler command line, ahead of everything else. 如果您无法更改.S文件中的#include,请在另一个目录中生成裸头文件,并将该包含路径放在编译器/汇编器命令行上,而不是其他任何内容。
And finally, you can wrap this all up in a makefile so your "bare" header files are generated automatically. 最后,您可以将其全部包装在makefile中,以便自动生成“裸”头文件。
That's simle: In your .S file use 这就是问题:在.S文件中使用
#define __ASSEMBLY__
In your .C file use 在.C文件中使用
#undef __ASSEMBLY__
Then in .h file place condition 然后在.h文件中放置条件
#ifdef __ASSEMBLY__
// here declarations only for assembler
#else
// here only for C
#endif
// and here - defines suitable for both
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.