Stack Exchange
Why can't clang find a file in the directory?
I am using MSYS2/MINGW64.
I have code for the avr atmega328 microcontroller, this code is compiling using avr-gcc 12.2.0 and Makefile, without errors.
I wanted to compile this one using clang 16.0.1, I fixed the Makefile and encountered an error when compiling:
 "C:/msys64/mingw64/bin/avr-ld" -o ComponentTester --gc-sections -LC:/msys64/mingw64/bin/../lib/avr -LC:/msys64/mingw64/bin/../lib/gcc/avr/12.2.0/../../../avr/lib/avr5 -LC:/msys64/mingw64/bin/../lib/gcc/avr/12.2.0/avr5 -Tdata=0x800100 --start-group -l:crtatmega328.o -lgcc -lm -lc -latmega328 -Map=ComponentTester.map main.o user.o pause.o adjust.o ADC.o probes.o
 display.o resistor.o cap.o semi.o inductor.o tools_misc.o tools_signal.o tools_counter.o tools_LC_Meter.o SPI.o I2C.o serial.o commands.o OneWire.o IR_RX.o IR_TX.o DHTxx.o ADS7843.o MAX6675.o MAX31855.o HD44780.o ILI9163.o ILI9341.o ILI9481.o ILI9486.o ILI9488.o PCD8544.o PCF8814.o SH1106.o SSD1306.o ST7036.o ST7565R.o ST7735.o Semi_ST7735.o ST7920.o STE2007.o
 VT100.o RD_Display.o wait.o --end-group -mavr5
C:/msys64/mingw64/bin/avr-ld: cannot find -l:crtatmega328.o: No such file or directory
C:/msys64/mingw64/bin/avr-ld: cannot find -lm: No such file or directory
C:/msys64/mingw64/bin/avr-ld: cannot find -lc: No such file or directory
C:/msys64/mingw64/bin/avr-ld: cannot find -latmega328: No such file or directory
main.o: file not recognized: file format not recognized
clang: error: avr-ld command failed with exit code 1 (use -v to see invocation)
make: *** [Makefile:182: ComponentTester] Error 1

As far as I understand, the error is that when linking, it is not possible to find the crtatmega328.o file along the path C:\msys64\mingw64\avr\lib\avr5 , but if go through the explorer, then this file will be there
I tried to forcibly specify in the Makefile :
-L C:/msys64/mingw64/avr/lib/avr5
Then the following message appears from the compiler
 "C:/msys64/mingw64/bin/avr-ld" -o ComponentTester --gc-sections -LC:/msys64/mingw64/avr/lib/avr5 -LC:/msys64/mingw64/bin/../lib/avr -LC:/msys64/mingw64/bin/../lib/gcc/avr/12.2.0/../../../avr/lib/avr5 -LC:/msys64/mingw64/bin/../lib/gcc/avr/12.2.0/avr5 -Tdata=0x800100 --start-group -l:crtatmega328.o -lgcc -lm -lc -latmega328 -Map=ComponentTester.map main.o user.
o pause.o adjust.o ADC.o probes.o display.o resistor.o cap.o semi.o inductor.o tools_misc.o tools_signal.o tools_counter.o tools_LC_Meter.o SPI.o I2C.o serial.o commands.o OneWire.o IR_RX.o IR_TX.o DHTxx.o ADS7843.o MAX6675.o MAX31855.o HD44780.o ILI9163.o ILI9341.o ILI9481.o ILI9486.o ILI9488.o PCD8544.o PCF8814.o SH1106.o SSD1306.o ST7036.o ST7565R.o ST7735.o
 Semi_ST7735.o ST7920.o STE2007.o VT100.o RD_Display.o wait.o --end-group -mavr5
main.o: file not recognized: file format not recognized
clang: error: avr-ld command failed with exit code 1 (use -v to see invocation)
make: *** [Makefile:182: ComponentTester] Error 1


The code can be compiled by removing compiler flags -flto.
But the size of the compiled file is too large compared to gcc
AVR Memory Usage (CLANG)
----------------
Device: atmega328

Program:   38812 bytes (118.4% Full)
(.text + .data + .bootloader)

Data:       5925 bytes (289.3% Full)
(.data + .bss + .noinit)

EEPROM:      771 bytes (75.3% Full)
(.eeprom)


AVR Memory Usage (GCC)
----------------
Device: atmega328

Program:   34210 bytes (104.4% Full)
(.text + .data + .bootloader)

Data:        276 bytes (13.5% Full)
(.data + .bss + .noinit)

EEPROM:      771 bytes (75.3% Full)
(.eeprom)


I also have installed
avr-libc 2.1.0-1, gcc-libs 12.2.0-10

msys is a unix emulation environment. Paths starting with C: make no sense. This accounts for some of the errors.

I think the most interesting part of the diagnostic is actually this: "main.o: file not recognized: file format not recognized".  That strongly suggests that you are trying to use a native-targeted Clang (on MSYS2 / MINGW64) together with the avr-targeted linker avr-ld.  That's never going to work.  It's possible that fixing that will make everything else work, too.

@JohnBollinger, Maybe, but the linker did build the program, but this size is very large.
I think it's a good idea that because of the avr-gcc linker, the size is large.
Which clang linker can I use for AVR to generate .hex and .eep?

clang will produce larger code than avr-gcc, so no surprise here.


Why can't clang find a file in the directory?

1.  Use avr-gcc (or avr-g++) to link, not avr-ld
It's avr-ld that reports an error.
See for example this answer for more details: avr-gcc, which is a driver program, will add many options when it calls the linker like -L search paths, libraries to link with -l, options for calling LTO plugin, and many others.

I tried to forcibly specify in the Makefile: -L C:/msys64/mingw64/avr/lib/avr5

2. Don't add search paths by means of -L that point to libs provided by the toolchain by hand.
The compiler driver avr-gcc will add paths pointing to correct libc, libm, libgcc and device lib. The driver uses command line options like -mmcu= to
determine the correct paths.  If a system library like libgcc.a or libc.a is not found, then your toolchain is broken.  Get a working toolchain.

The code can be compiled by removing compiler flags -flto

3. Mixing LTO bytecode from GCC and Clang might not work.
One reason why LTO doesn't work as expected is because you used avr-ld to link, see 1.
Apart from that, LTO bytecode is not standardized.  Even different major version of GCC may use different, incompatible versions of bytecode.  You have to use the same GCC version for compilation and for LTO compilation.  GCC's LTO compiler, lto1 might not work with bytecode from LLVM. At best, the linker plugin detects that there is no (valid) LTO bytecode present, and skips LTO altogether.
