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 d开发者_如何学Goeclarations 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.
That's simle: In your .S file use
#define __ASSEMBLY__
In your .C file use
#undef __ASSEMBLY__
Then in .h file place condition
#ifdef __ASSEMBLY__
// here declarations only for assembler
#else
// here only for C
#endif
// and here - defines suitable for both
精彩评论