#include包含C声明的程序集文件中没有错误? [英] #include header with C declarations in an assembly file without errors?
问题描述
我有一个程序集文件( asm.S
),它需要一个常量 #define
'd在C头文件( c_decls.h
)。除了 #define
我想要的之外,头文件还包含C函数声明。不幸的是,当试图编译程序集文件时, gcc
barfs。例如,
c_decls.h
#ifndef __c_decls_h__
#define __c_decls_h__
#define I_NEED_THIS 0xBEEF
int foo(int bar);
#endif
asm.S
#includec_decls.h
.globl main
main:
pushl%ebp
movl%esp,%ebp
movl $ I_NEED_THIS,%eax
leave
ret
输出
$ b
> gcc -m32 asm.S
c_decls.h:汇编程序消息:
c_decls.h:6:错误:表达式后的junk'(int bar)'
c_decls.h:6 :错误:'int'后缀或操作数无效
有没有办法 #include
包含函数声明的C头文件? (更改标题或移动/重新定义 #define
不是一个选项。)
对 cpp
使用 -dM
选项来从头文件中获得#define,并改为包含该文件。
cpp -dM c_decls.h> bare_c_decls.h
现在包含 bare_c_decls.h
在你的.S文件中。如果无法更改.S文件中的#include,请在另一个目录中生成裸露的头文件,并将其包含在您的编译器/汇编程序命令行中,而不是其他任何内容。
最后,你可以将它们全部包装到makefile中,以便自动生成裸头文件。
I have an assembly file (asm.S
) which needs a constant #define
'd in a C header file (c_decls.h
). The header file contains C function declarations in addition to the #define
I want. Unfortunately, gcc
barfs when trying to compile the assembly file. For example,
c_decls.h
#ifndef __c_decls_h__
#define __c_decls_h__
#define I_NEED_THIS 0xBEEF
int foo(int bar);
#endif
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
c_decls.h: Assembler messages:
c_decls.h:6: Error: junk '(int bar)' after expression
c_decls.h:6: Error: suffix or operands invalid for 'int'
Is there a way to #include
a C header file that contains function declarations in an assembly file? (Changing the header or moving/redefining the #define
is not an option.)
Use the -dM
option for cpp
to get just the #defines out of your header files, and include that file instead.
cpp -dM c_decls.h > bare_c_decls.h
Now include bare_c_decls.h
in your .S file. 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.
And finally, you can wrap this all up in a makefile so your "bare" header files are generated automatically.
这篇关于#include包含C声明的程序集文件中没有错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!