using_tools(3)

NAME
using_tools - Using the GNU tools This is a short summary of the AVR-specific aspects of using the GNU tools. Normally, the generic documentation of these tools is fairly large and maintained in texinfo files. Command-line options are explained in detail in the manual page.

Options for the C compiler avr-gcc

  Machine-specific options for the AVR
The following machine-specific options are recognized by the C compiler frontend.

•   -mmcu=architecture

Compile code for architecture. Currently known architectures are

avr1Simple CPU core, only assembler support avr2'Classic' CPU core, up to 8 KB of ROM avr3'Classic' CPU core, more than 8 KB of ROM avr4'Enhanced' CPU core, up to 8 KB of ROM avr5'Enhanced' CPU core, more than 8 KB of ROM

By default, code is generated for the avr2 architecture.

Note that when only using -mmcu=architecture but no -mmcu=MCU type, including the file <avr/io.h> cannot work since it cannot decide which device's definitions to select.

•   -mmcu=MCU type

The following MCU types are currently understood by avr-gcc. The table matches them against the corresponding avr-gcc architecture name, and shows the preprocessor symbol declared by the -mmcu option.

ArchitectureMCU nameMacro avr1at90s1200__AVR_AT90S1200__ avr1attiny11__AVR_ATtiny11__ avr1attiny12__AVR_ATtiny12__ avr1attiny15__AVR_ATtiny15__ avr1attiny28__AVR_ATtiny28__ avr2at90s2313__AVR_AT90S2313__ avr2at90s2323__AVR_AT90S2323__ avr2at90s2333__AVR_AT90S2333__ avr2at90s2343__AVR_AT90S2343__ avr2attiny22__AVR_ATtiny22__ avr2attiny26__AVR_ATtiny26__ avr2at90s4414__AVR_AT90S4414__ avr2at90s4433__AVR_AT90S4433__ avr2at90s4434__AVR_AT90S4434__ avr2at90s8515__AVR_AT90S8515__ avr2at90c8534__AVR_AT90C8534__ avr2at90s8535__AVR_AT90S8535__ avr2at86rf401__AVR_AT86RF401__ avr2attiny13__AVR_ATtiny13__ avr2attiny2313__AVR_ATtiny2313__ avr3atmega103__AVR_ATmega103__ avr3atmega603__AVR_ATmega603__ avr3at43usb320__AVR_AT43USB320__ avr3at43usb355__AVR_AT43USB355__ avr3at76c711__AVR_AT76C711__ avr4atmega48__AVR_ATmega48__ avr4atmega8__AVR_ATmega8__ avr4atmega8515__AVR_ATmega8515__ avr4atmega8535__AVR_ATmega8535__ avr4atmega88__AVR_ATmega88__ avr5at90can128__AVR_AT90CAN128__ avr5atmega128__AVR_ATmega128__ avr5atmega16__AVR_ATmega16__ avr5atmega161__AVR_ATmega161__ avr5atmega162__AVR_ATmega162__ avr5atmega163__AVR_ATmega163__ avr5atmega165__AVR_ATmega165__ avr5atmega168__AVR_ATmega168__ avr5atmega169__AVR_ATmega169__ avr5atmega32__AVR_ATmega32__ avr5atmega323__AVR_ATmega323__ avr5atmega325__AVR_ATmega325__ avr5atmega3250__AVR_ATmega3250__ avr5atmega64__AVR_ATmega64__ avr5atmega645__AVR_ATmega645__ avr5atmega6450__AVR_ATmega6450__ avr5at94k__AVR_AT94K__

•   -morder1

•   -morder2

Change the order of register assignment. The default is

r24, r25, r18, r19, r20, r21, r22, r23, r30, r31, r26, r27, r28, r29, r17, r16, r15, r14, r13, r12, r11, r10, r9, r8, r7, r6, r5, r4, r3, r2, r0, r1

Order 1 uses

r18, r19, r20, r21, r22, r23, r24, r25, r30, r31, r26, r27, r28, r29, r17, r16, r15, r14, r13, r12, r11, r10, r9, r8, r7, r6, r5, r4, r3, r2, r0, r1

Order 2 uses

r25, r24, r23, r22, r21, r20, r19, r18, r30, r31, r26, r27, r28, r29, r17, r16, r15, r14, r13, r12, r11, r10, r9, r8, r7, r6, r5, r4, r3, r2, r1, r0

•   -mint8

Assume int to be an 8-bit integer. Note that this is not really supported by avr-libc, so it should normally not be used. The default is to use 16-bit integers.

•   -mno-interrupts

Generates code that changes the stack pointer without disabling interrupts. Normally, the state of the status register SREG is saved in a temporary register, interrupts are disabled while changing the stack pointer, and SREG is restored.

•   -mcall-prologues

Use subroutines for function prologue/epilogue. For complex functions that use many registers (that needs to be saved/restored on function entry/exit), this saves some space at the cost of a slightly increased execution time.

•   -minit-stack=nnnn

Set the initial stack pointer to nnnn. By default, the stack pointer is initialized to the symbol __stack, which is set to RAMEND by the run-time initialization code.

•   -mtiny-stack

Change only the low 8 bits of the stack pointer.

•   -mno-tablejump

Do not generate tablejump instructions. By default, jump tables can be used to optimize switch statements. When turned off, sequences of compare statements are used instead. Jump tables are usually faster to execute on average, but in particular for switch statements where most of the jumps would go to the default label, they might waste a bit of flash memory.

•   -mshort-calls

Use rjmp/rcall (limited range) on >8K devices. On avr2 and avr4 architectures (less than 8 KB or flash memory), this is always the case. On avr3 and avr5 architectures, calls and jumps to targets outside the current function will by default use jmp/call instructions that can cover the entire address range, but that require more flash ROM and execution time.

•   -mrtl

Dump the internal compilation result called 'RTL' into comments in the generated assembler code. Used for debugging avr-gcc.

•   -msize

Dump the address, size, and relative cost of each statement into comments in the generated assembler code. Used for debugging avr-gcc.

•   -mdeb

Generate lots of debugging information to stderr.

  Selected general compiler options
The following general gcc options might be of some interest to AVR users.

•   -On

 Optimization level n. Increasing n is meant to optimize more, an optimization level of 0 means no optimization at all, which is the default if no -O option is present. The special option -Os is meant to turn on all -O2 optimizations that are not expected to increase code size.

Note that at -O3, gcc attempts to inline all 'simple' functions. For the AVR target, this will normally constitute a large pessimization due to the code increasement. The only other optimization turned on with -O3 is -frename-registers, which could rather be enabled manually instead.

A simple -O option is equivalent to -O1.

Note also that turning off all optimizations will prevent some warnings from being issued since the generation of those warnings depends on code analysis steps that are only performed when optimizing (unreachable code, unused variables).

See also the appropriate FAQ entry for issues regarding debugging optimized code.

•   -Wa,assembler-options

•   -Wl,linker-options

 Pass the listed options to the assembler, or linker, respectively.

•   -g

Generate debugging information that can be used by avr-gdb.

•   -ffreestanding

Assume a 'freestanding' environment as per the C standard. This turns off automatic builtin functions (though they can still be reached by prepending __builtin_ to the actual function name). It also makes the compiler not complain when main() is declared with a void return type which makes some sense in a microcontroller environment where the application cannot meaningfully provide a return value to its environment (in most cases, main() won't even return anyway). However, this also turns off all optimizations normally done by the compiler which assume that functions known by a certain name behave as described by the standard. E. g., applying the function strlen() to a literal string will normally cause the compiler to immediately replace that call by the actual length of the string, while with -ffreestanding, it will always call strlen() at run-time.

•   -funsigned-char

Make any unqualfied char type an unsigned char. Without this option, they default to a signed char.

•   -funsigned-bitfields

Make any unqualified bitfield type unsigned. By default, they are signed.

•   -fshort-enums

Allocate to an enum type only as many bytes as it needs for the declared range of possible values. Specifically, the enum type will be equivalent to the smallest integer type which has enough room.

•   -fpack-struct

Pack all structure members together without holes.

Options for the assembler avr-as

  Machine-specific assembler options

•   -mmcu=architecture

•   -mmcu=MCU name

avr-as understands the same -mmcu= options as avr-gcc. By default, avr2 is assumed, but this can be altered by using the appropriate .arch pseudo-instruction inside the assembler source file.

•   -mall-opcodes

Turns off opcode checking for the actual MCU type, and allows any possible AVR opcode to be assembled.

•   -mno-skip-bug

Don't emit a warning when trying to skip a 2-word instruction with a CPSE/SBIC/SBIS/SBRC/SBRS instruction. Early AVR devices suffered from a hardware bug where these instructions could not be properly skipped.

•   -mno-wrap

For RJMP/RCALL instructions, don't allow the target address to wrap around for devices that have more than 8 KB of memory.

•   --gstabs

Generate .stabs debugging symbols for assembler source lines. This enables avr-gdb to trace through assembler source files. This option must not be used when assembling sources that have been generated by the C compiler; these files already contain the appropriate line number information from the C source files.

•   -a[cdhlmns=file]

Turn on the assembler listing. The sub-options are:

•   c omit false conditionals
•   d omit debugging directives
•   h include high-level source
•   l include assembly
•   m include macro expansions
•   n omit forms processing
•   s include symbols
•   =file set the name of the listing file
The various sub-options can be combined into a single -a option list; =file must be the last one in that case.

  Examples for assembler options passed through the C compiler
Remember that assembler options can be passed from the C compiler frontend using -Wa (see above), so in order to include the C source code into the assembler listing in file foo.lst, when compiling foo.c, the following compiler command-line can be used:
	$ avr-gcc -c -O foo.c -o foo.o -Wa,-ahls=foo.lst
In order to pass an assembler file through the C preprocessor first, and have the assembler generate line number debugging information for it, the following command can be used:
	$ avr-gcc -c -x assembler-with-cpp -o foo.o foo.S -Wa,--gstabs
Note that on Unix systems that have case-distinguishing file systems, specifying a file name with the suffix .S (upper-case letter S) will make the compiler automatically assume -x assembler-with-cpp, while using .s would pass the file directly to the assembler (no preprocessing done).

Controlling the linker avr-ld

  Selected linker options
While there are no machine-specific options for avr-ld, a number of the standard options might be of interest to AVR users.
•   -lname
Locate the archive library named libname.a, and use it to resolve currently unresolved symbols from it. The library is searched along a path that consists of builtin pathname entries that have been specified at compile time (e. g. /usr/local/avr/lib on Unix systems), possibly extended by pathname entries as specified by -L options (that must precede the -l options on the command-line).
•   -Lpath
Additional location to look for archive libraries requested by -l options.
•   --defsym symbol=expr
Define a global symbol symbol using expr as the value.
•   -M
Print a linker map to stdout.
•   -Map mapfile
Print a linker map to mapfile.
•   --cref
Output a cross reference table to the map file (in case -Map is also present), or to stdout.
•   --section-start sectionname=org
Start section sectionname at absolute address org.
•   -Tbss org
•   -Tdata org
•   -Ttext org
Start the bss, data, or text section at org, respectively.
•   -T scriptfile
Use scriptfile as the linker script, replacing the default linker script. Default linker scripts are stored in a system-specific location (e. g. under /usr/local/avr/lib/ldscripts on Unix systems), and consist of the AVR architecture name (avr2 through avr5) with the suffix .x appended. They describe how the various memory sections will be linked together.

  Passing linker options from the C compiler
By default, all unknown non-option arguments on the avr-gcc command-line (i. e., all filename arguments that don't have a suffix that is handled by avr-gcc) are passed straight to the linker. Thus, all files ending in .o (object files) and .a (object libraries) are provided to the linker.
System libraries are usually not passed by their explicit filename but rather using the -l option which uses an abbreviated form of the archive filename (see above). avr-libc ships two system libraries, libc.a, and libm.a. While the standard library libc.a will always be searched for unresolved references when the linker is started using the C compiler frontend (i. e., there's always at least one implied -lc option), the mathematics library libm.a needs to be explicitly requested using -lm. See also the entry in the FAQ explaining this.
Conventionally, Makefiles use the make macro LDLIBS to keep track of -l (and possibly -L) options that should only be appended to the C compiler command-line when linking the final binary. In contrast, the macro LDFLAGS is used to store other command-line options to the C compiler that should be passed as options during the linking stage. The difference is that options are placed early on the command-line, while libraries are put at the end since they are to be used to resolve global symbols that are still unresolved at this point.
Specific linker flags can be passed from the C compiler command-line using the -Wl compiler option, see above. This option requires that there be no spaces in the appended linker option, while some of the linker options above (like -Map or --defsym) would require a space. In these situations, the space can be replaced by an equal sign as well. For example, the following command-line can be used to compile foo.c into an executable, and also produce a link map that contains a cross-reference list in the file foo.map:
	$ avr-gcc -O -o foo.out -Wl,-Map=foo.map -Wl,--cref foo.c
Alternatively, a comma as a placeholder will be replaced by a space before passing the option to the linker. So for a device with external SRAM, the following command-line would cause the linker to place the data segment at address 0x2000 in the SRAM:
	$ avr-gcc -mmcu=atmega128 -o foo.out -Wl,-Tdata,0x802000
See the explanation of the data section for why 0x800000 needs to be added to the actual value. Note that unless a -minit-stack option has been given when compiling the C source file that contains the function main(), the stack will still remain in internal RAM, through the symbol __stack that is provided by the run-time startup code. This is probably a good idea anyway (since internal RAM access is faster), and even required for some early devices that had hardware bugs preventing them from using a stack in external RAM. Note also that the heap for malloc() will still be placed after all the variables in the data section, so in this situation, no stack/heap collision can occur.