简体   繁体   中英

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 #define s 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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM