Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

#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). 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.)

like image 358
Andrew Keeton Avatar asked Feb 08 '11 00:02

Andrew Keeton


1 Answers

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.

like image 59
payne Avatar answered Oct 27 '22 08:10

payne