Using of arm-none-eabi-ar leads to broken binary


I use arm-none-eabi to compile my STM32 project. I want to link my project files to an archive, e.g. libexample.a, to allow link with other objects, e.g. example.o. The libexample.a contains weak symbol replaced by example.o and example.o uses functions from libexample.a.

Now I face the following problem: When I create libexample.a and then link libexample.a with example.o, the resulting elf/bin is malfunction.

When I take example.o and link it directly with .o files used to crate libexample.a, the resulting elf/bin is OK.

Compile flags are equal.

The further investigation showed, that:

  • the bin/elf produced by example.a+example.o link process is smaller than when I take example.o+.o files used to create example.a file
  • the example.a contains all .o files

The Makefile snippet is below:

libexample.a: $(OBJS)
        @echo Creating static library $@
        $(AR) rcs $@ $(OBJS)

        @echo Compiling
        @arm-none-eabi-gcc -c $(CFLAGS) $(INCLUDE) example.c -o example.o
        @echo "Linking (creating .elf)"
        @arm-none-eabi-gcc $(OBJS) example.o $(LDFLAGS) -o example.elf
        @echo "Creating .hex file"
        @arm-none-eabi-objcopy -O ihex example.elf example.hex
        @echo "Creating .bin file"
        @arm-none-eabi-objcopy -O binary -S example.elf example.bin

afile: libexample.a
        @echo Compiling
        @arm-none-eabi-gcc -c $(CFLAGS) $(INCLUDE) example.c -o example.o
        @echo "Linking (creating .elf)"
        @arm-none-eabi-gcc -L. -lexample example.o $(LDFLAGS) -o example2.elf
        @echo "Creating .hex file"
        @arm-none-eabi-objcopy -O ihex example2.elf example2.hex
        @echo "Creating .bin file"
        @arm-none-eabi-objcopy -O binary -S example2.elf example2.bin

example.bin size is 172K
example2.bin  size is 144K

More questions in this forum