ARM Technical Support Knowledge Articles

Sharing header files between C and assembler

Applies to: ARM Developer Suite (ADS), DS-5, RealView Developer Kit (RVDK) for OKI, RealView Developer Kit (RVDK) for ST, RealView Developer Kit for XScale (RVXDK), RealView Development Suite (RVDS)


To define, for example, constants in code, it is normal to use #define for C code and EQU for assembler code. You may have a project containing a mix of C and assembler code, where there are some constant definitions which are common to both the C and assembler code. In this case, it is useful to be able to create a list of common definitions that can be included into both C files and assembler files, to avoid maintaining two parallel lists.

Solutions for ADS 1.2, RVDS 2.x, RVDS 3.x, RVDS 4.x and DS-5

In ADS 1.2 and later, you can include C style #include and #define directives directly in your assembler source. The assembler source can then be passed through the C compiler preprocessor, which outputs a "preprocessed" version of your assembler code, which can then be assembled in the normal way. Consider the following example:
----- ex.s -----
#include "my_header.h"

ADD r0, r0, r1
MOV pc,lr

--- end ex.s ---

where my_header.h contains

#define ONE_CONSTANT 1

When ex.s is passed through the C preprocessor using the following command.

armcc -E ex.s > p_ex.s

The output file p_ex.s looks like this:

---- p_ex.s ----
#line 1 "ex.s"
#line 1 "my_header.h"
#line 2 "ex.s"

MOV r0, # 1
ADD r0, r0, r1
MOV pc,lr

-- end p_ex.s --

p_ex.s can then be assembled in the normal way with armasm.

Unfortunately, the above cannot be done with ADS 1.1 or earlier because

Additional Information

In RVDS 4.x and DS-5, you no longer need to invoke armcc to use C preprocessor macros in an assembly language source file or C preprocessor command-line options. To achieve this you can use the --cpreproc command-line option when invoking the assembler. The --cpreproc_opts command-line option can be used to pass additional options to the compiler.  For example: armasm --cpreproc source.s. For more information on these options, please see the corresponding assembler documentation in RVDS 4.x or DS-5.

Solutions for ADS 1.1 (and earlier)

Two solutions for ADS 1.1 and earlier are given below:


Article last edited on: 2011-08-30 15:44:37

Rate this article

Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential