arduino / Arduino

Arduino IDE 1.x
https://www.arduino.cc/en/software
Other
14.11k stars 7k forks source link

avr linking with precompiled library #7005

Open Corjan85 opened 6 years ago

Corjan85 commented 6 years ago

Hi, I just tried the upcoming precompiled library feature. I love it, but I noticed a possible bug.

I created a sketch with my own 3 libraries included (HWLib HWLibDriver SiBase), but I kept getting undefined reference errors while linking.

While diving into the problem I noticed the reason:

Link command:

"C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avr-gcc" -w -Os -g -flto -fuse-linker-plugin -Wl,--gc-sections,--relax -mmcu=atmega2560 "-LC:\Program Files (x86)\Arduino\libraries\HWLib\src\atmega2560" **-lHWLib -lHWLibDriver -lSiBase**  -o "C:\Users\corja\AppData\Local\Temp\arduino_build_576204/sketch_dec10a.ino.elf" "C:\Users\corja\AppData\Local\Temp\arduino_build_576204\sketch\sketch_dec10a.ino.cpp.o" "C:\Users\corja\AppData\Local\Temp\arduino_build_576204/..\arduino_cache_568285\core\core_arduino_avr_mega_cpu_atmega2560_0c812875ac70eb4a9b385d8fb077f54c.a" "-LC:\Users\corja\AppData\Local\Temp\arduino_build_576204" -lm

Error:

C:\Users\corja\AppData\Local\Temp\ccHHlJFj.ltrans0.ltrans.o: In function `setup':
C:\Users\corja\Desktop\sketch\sketch_dec10a/sketch_dec10a.ino:12: undefined reference to `si_hw_lib_tick'
collect2.exe: error: ld returned 1 exit status

Notice that my libraries are included before the object files of the sketch itself. Since ordering is very important during linking, this is causing the problem

Changing it to this fixes it:

"C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avr-gcc" -w -Os -g -flto -fuse-linker-plugin -Wl,--gc-sections,--relax -mmcu=atmega2560 "-LC:\Program Files (x86)\Arduino\libraries\HWLib\src\atmega2560" -o "C:\Users\corja\AppData\Local\Temp\arduino_build_576204/sketch_dec10a.ino.elf" "C:\Users\corja\AppData\Local\Temp\arduino_build_576204\sketch\sketch_dec10a.ino.cpp.o" "C:\Users\corja\AppData\Local\Temp\arduino_build_576204/..\arduino_cache_568285\core\core_arduino_avr_mega_cpu_atmega2560_0c812875ac70eb4a9b385d8fb077f54c.a" "-LC:\Users\corja\AppData\Local\Temp\arduino_build_576204" **-lHWLib -lHWLibDriver -lSiBase** -lm

I'm sorry if I am repeating a known bug.

Corjan

Corjan85 commented 6 years ago

My library.properties:

name=SI Custom Device
version=1.0.0
author=Sim Innovations
maintainer=Sim Innovations <corjan@siminnovations.com>
sentence=Allows to couple custom Arduino projects to Air Manager or Air Player
paragraph=TODO
category=Device Control
url=https://www.siminnovations.com
architectures=*
precompiled=true
ldflags=-lHWLib -lHWLibDriver -lSiBase
per1234 commented 6 years ago

I believe this has been reported to the SAM and SAMD cores but not yet regarding the AVR core: https://github.com/arduino/ArduinoCore-sam/issues/46 https://github.com/arduino/ArduinoCore-samd/issues/278

Corjan85 commented 6 years ago

Hi, Thanks for your quick response, and sorry about the other threads. I should have searched better.

Just checked the platform.txt in the ArduinoCore-avr github, but that platform.txt does not seem to be correct (just yet).

I believe line 68 should be replaced by this line:

recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}" {compiler.c.elf.flags} -mmcu={build.mcu} -o "{build.path}/{build.project_name}.elf" {object_files} {compiler.c.elf.extra_flags} "{build.path}/{archive_file}" "-L{build.path}" -lm

The link order would be:

That way a precompiled library can also use features from the ArduinoCore library.

Corjan