1. What I'm trying to achieve
My ultimate goal is to convert Arduino projects (sketches) into makefile-based self-contained C/C++ projects.
I already made some progress (thanks @Juraj for your helpful comments). I copy all the relevant Arduino *.cpp
and *.h
files to a folder (which I call my "project folder") as well as the preprocessed sketch file (the Arduino preprocessor turns the sketch file into a *.cpp
file).
I added an in-house makefile and the build works very well. However, in the last step - the linking - it's still a mystery to me what linkerscript is being used exactly.
2. The problem explained
Although the build - including the linking step - works very well, I'd like to know what linkerscript is being used. Right now, the avr-gcc
toolchain chooses a linkerscript from its installation folder:
<arduino ide installation>/hardware/tools/avr/avr/lib/ldscripts
As @Juraj explains in the comments, the toolchain bases its choice on the -mmcu
flag.
I don't want the compiler to choose a fixed linkerscript from the avr-gcc
toolchain. Instead, I want to copy this linkerscript right into my project folder and be able to tweak it. But to do that - I must first know which linkerscript it is. How can I know?
3. Background info
Below you can find more information about my setup and system.
3.1. My setup
I've got an Arduino UNO R3
which is based on the ATmega328P
microcontroller. I'm working in Ubuntu 20.04.1 LTS
.
I installed the Arduino IDE and created a new project from:
File > Examples > 01.Basics > Blink
I saved the new project at ~/Arduino/sketch_uno_blinky/sketch_uno_blinky.ino
Finally I ticked the checkmark at:
File > Preferences > Show verbose output
such that I can see the compilation output properly. Based on that, I could track all the *.cpp
and *.h
files that take part in the build. I copied them to a folder to make my own self-contained makefile-based project.
3.2 Linking
I run the linker like this:
avr-gcc -Wl,-Map=output.map
-Wl,--gc-sections
-mmcu=atmega328p
-DF_CPU=16000000L
-DARDUINO=10813
-DARDUINO_AVR_UNO
-DARDUINO_ARCH_AVR
-Og
-g3
-MMD
-fmessage-length=0
-ffunction-sections
-fdata-sections
-Wno-comment
-Wno-unused-function
-Werror-implicit-function-declaration
-w
-lm
-flto
-fuse-linker-plugin
-L ../config/
-o application.elf
WInterrupts.o
main.o
hooks.o
wiring.o
wiring_analog.o
wiring_digital.o
wiring_pulse.o
wiring_shift.o
[...]
sketch_uno_blinky.ino.o
which results in the application.elf
firmware.
packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino5/avr/lib/ldscripts
. but let avr-gcc do this – Juraj