9

When I search I found 7yr old results talking about a fork of clang instead of clang itself.

Using avr-gcc I can compile and upload my code with

avr-gcc a.cpp -DF_CPU=16000000 -mmcu=atmega2560 -Wall -Werror -Wextra -Os
avr-objcopy -j .text -j .data -O ihex a.out my.hex
sudo avrdude -patmega2560 -cwiring -P/dev/ttyACM0 -b115200 -D -Uflash:w:my.hex:i

I'd like to replace the first step with clang++. The changes I made here

  • avr-gcc to clang++
  • Added --target=avr
  • Added -nostdlib because I'll include it myself
  • Added -I/usr/avr/include/ because path wasn't implicit
  • Added -L/usr/avr/lib/avr6 -lc -latmega2560 so it has enough info to build an elf

I found device-specs at /usr/lib/gcc/avr/10.2.0/device-specs/specs-atmega2560 which mentions crtatmega2560.o and -latmega2560 which appears to be located at /usr/avr/lib/avr6/. So I came up with the following and got these errors. How should I be compiling so I can get a hex to upload using avrdude?

$ clang++ a.cpp -DF_CPU=16000000 -mmcu=atmega2560 -Wall -Werror -Wextra -Os --target=avr -I/usr/avr/include/ -nostdlib -L/usr/avr/lib/avr6 -lc -latmega2560
/usr/bin/avr-ld: skipping incompatible /usr/avr/lib/avr6/libc.a when searching for -lc
/usr/bin/avr-ld: cannot find -lc
/usr/bin/avr-ld: skipping incompatible /usr/avr/lib/avr6/libatmega2560.a when searching for -latmega2560
/usr/bin/avr-ld: cannot find -latmega2560
8
  • As we see in errors, linking is not going fine. But when you link manually, it links just fine. Are you sure that libc and libatmega2560 are the same when you link manually? I am a bit confused. Commented Jan 7, 2021 at 3:03
  • Also I noticed that when I install gcc-avr and avr-libc from apt, they work just fine without directly specifying them, but clang freaks out to atmega2560. Commented Jan 7, 2021 at 3:11
  • @CPPCPPCPPCPPCPPCPPCPPCPPCPPCPP I'm not sure of anything tbh. I just know if I do -c and link with arv-gcc it doesn't complain but I dont know if the code is completely incorrect. I want to use a non C language on arduino who emits LLVM IR so I'm hoping I can get the code running later. Commented Jan 7, 2021 at 16:26
  • Understood. Speaking about correctness, I am not sure if I can help :( I like playing with LLVM too, but probably you'll have to run the code to guess if it works correctly :( Does your version of Clang freaks out to atmega2560 too? Commented Jan 7, 2021 at 20:40
  • Generally, if it will be able to run correctly - problem is kinda solved? Commented Jan 7, 2021 at 20:50

2 Answers 2

2

AVR target is experimental in LLVM compiler for which clang is the C and C++ front-end. To enable experimental targets you must compile LLVM from source. This Stack Overflow answer describes how to do it.

Looking at the bug tracker I see there are good reasons why it is experimental.

Sign up to request clarification or add additional context in comments.

2 Comments

~~Is there no work arounds? I figured out if I do -c in llvm and use avr-gcc to link them it seems to work (didn't execute the code but it compiled/linked without error).~~ From the bug tracker it seems like it just doesn't produce correct code?
@EricStotch, workaround what? do you thing it generated machine code for the AVR without support for it?
0

I am not sure what to answer finally.

Probably it is not the worst idea to compile .o file with clang, and link everything manually just like you wish.

I am not sure, if it is needed to enable any experiment features, due I tried to compile something to AVR, and it works fine with clang-12 when I use llvm repository apt.llvm.org.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.