Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to include ASM header file in C without losing preprocessor

Short version:

I want to be able to define assembler macros in a macros.S and use them from inside asm() statements in GNU C.

I can do this with asm(".include \"macros.S\""); near the top of my C source, but I want macros.S to go through the C preprocessor.


Long version:

In GCC asm, *.S files are preprocessed by the C preprocessor, allowing use of C style #define, etc.

In GCC C, you can include an asm header file (which may include asm macro definitions, .set declarations, etc), by writing asm(".include \"myasmheader.S\""); near the top of a file.

Including an ASM header file in this manner allows you to use asm macros inside asm blocks.

Unfortunately, doing so does not invoke the C preprocessor on the .S file being included (as the .include is done later in the compilation process), and so #defines are no longer substituted.

So is there any way to properly include a .S file inside of a C file?

Some other compilers support:

#asm
#include "myasmheader.S"
#endasm

Which would not exhibit such a problem. But alas, GCC seems to require that all asm inside of a C file is in the form of strings.

Short of not using asm (not an option, embedded DSP project that heavily mixes asm and c), or removing use of the C preprocessor in ASM files, what can be done?

like image 570
Mania Avatar asked Nov 28 '17 13:11

Mania


1 Answers

From the comments:

Add preprocessing of the ASM file (via cpp) as a distinct build step into whatever build system you're using.

Credits to arrowd and Ped7g.

like image 109
ARaspiK Avatar answered Nov 07 '22 10:11

ARaspiK