← 返回命令列表

Linux command

avr-gcc 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

Compile C code

avr-gcc -mmcu=[atmega328p] -o [output.elf] [source.c]

Compile with optimization

avr-gcc -mmcu=[atmega328p] -Os -o [output.elf] [source.c]

Compile with debugging

avr-gcc -mmcu=[atmega328p] -g -O0 -o [output.elf] [source.c]

Generate hex file

avr-gcc -mmcu=[atmega328p] -Os -o [output.elf] [source.c] && avr-objcopy -O ihex [output.elf] [output.hex]

Compile with warnings

avr-gcc -mmcu=[atmega328p] -Wall -Os -Wl,--relax -o [output.elf] [source.c]

Compile for freestanding

avr-gcc -mmcu=[atmega328p] -ffreestanding -Os -o [output.elf] [source.c]

说明

avr-gcc is the GNU Compiler Collection configured for AVR 8-bit microcontrollers. It compiles C and C++ code into machine code for Atmel/Microchip AVR chips used in Arduino and embedded systems. The -mmcu option is essential as it configures code generation, memory layout, and available instructions for the specific target MCU. Common targets include atmega328p (Arduino Uno), atmega2560 (Arduino Mega), and attiny85. Output is typically an ELF file, which is converted to Intel HEX format using avr-objcopy for flashing to hardware via tools like avrdude. The -Os optimization is preferred for embedded development as it minimizes code size while maintaining reasonable performance. The -ffreestanding flag is appropriate since AVR programs run without an operating system. AVR-GCC is typically installed as part of the avr-libc toolchain, which includes the C library, header files, and additional tools.

参数

-mmcu=_mcu_
Target MCU (atmega328p, attiny85, atmega2560, etc.); required for correct code generation
-Os
Optimize for size (recommended for embedded)
-O0, -O1, -O2, -O3
Optimization levels (0=none, 3=maximum)
-g
Include debugging information
-Wall
Enable all common warnings
-ffreestanding
Assume freestanding environment (no standard library assumptions)
-fno-jump-tables
Disable jump tables (required for bootloaders on >64KB devices)
-mrelax
Enable linker relaxation to use shorter instructions when possible
-Wl,--relax
Pass relaxation option to linker
-Wl,-gc-sections
Remove unused code sections to reduce binary size
-mcall-prologues
Use subroutines for function prologues/epilogues (saves space)
-DF_CPU=_freq_
Define CPU frequency in Hz (e.g., -DF_CPU=16000000)
-I_path_
Add include directory
-L_path_
Add library search directory
-l_library_
Link with library

FAQ

What is the avr-gcc command used for?

avr-gcc is the GNU Compiler Collection configured for AVR 8-bit microcontrollers. It compiles C and C++ code into machine code for Atmel/Microchip AVR chips used in Arduino and embedded systems. The -mmcu option is essential as it configures code generation, memory layout, and available instructions for the specific target MCU. Common targets include atmega328p (Arduino Uno), atmega2560 (Arduino Mega), and attiny85. Output is typically an ELF file, which is converted to Intel HEX format using avr-objcopy for flashing to hardware via tools like avrdude. The -Os optimization is preferred for embedded development as it minimizes code size while maintaining reasonable performance. The -ffreestanding flag is appropriate since AVR programs run without an operating system. AVR-GCC is typically installed as part of the avr-libc toolchain, which includes the C library, header files, and additional tools.

How do I run a basic avr-gcc example?

Run `avr-gcc -mmcu=[atmega328p] -o [output.elf] [source.c]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -mmcu=_mcu_ do in avr-gcc?

Target MCU (atmega328p, attiny85, atmega2560, etc.); required for correct code generation