This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How to create and link a static library for an ARM project using arm-none-eabi-gcc? or get the knoledge to do it

I want to create a static library libmylib.a from mylib.c/.h and link it to a project to use this library in bootloader code using the arm-none-eabi-gcc cross compiler in ubuntu 20.04 LTS.

I have an electronic engineering background, so I'm kind of new in this compiler and linker stuff.

What I know: I've been searching for knoledge about it, and findout that '.a' are just a pack for '.o' files, and that's it. You can do it using ar in linux. I don't know how to manage the dependencies for this '.a' file for example or how to link it to the project.

What I want to know: I really want to understand how it works, to compile and generate the bin, elf or hex files using these static libraries for arm using the arm-none-eabi-gcc cross compiler(found some for linux), but don't know how to search for this knoledge properly, how to lean it in a linear way. If you guys could help me on this I would be really gratefull.

Parents
  • Pity, no one answered. Had this problem quite a few years ago.

    Assuming ARM7TDMI in ARM moode, you compile to objects with

    arm-none-eabi-gcc -ffreestanding -mcpu=arm7tdmi -marm -c main.c

    Use your processor or arch (e.g. arch=armv8-m) instead.

    You have to create an index in the .a file with ar option -s. I use:

    arm-none-eabi-ar rcs libmylib.a *.o
    
    

    You can use that libmylib.a when linking with ld or better gcc like shown below. Conversion into .bin or .hex is done with objcopy:

    arm-none-eabi-gcc -o program.elf main.o -Lpath/to/lib -lmylib -ffreestanding -nostdlib -lgcc
    arm-none-eabi-objcopy --only-section=.text --only-section=.rodata --only-section=.data -O binary program.elf program.bin
    arm-none-eabi-objcopy --only-section=.text --only-section=.rodata --only-section=.data -O ihex program.elf program.hex

    Very important and very complex to me was creating a suitable LD-script, which is provided with -T<linkerScript.x> when linking.

    And of course a startup-code (crt0.o) you use from the linker script. But that needs MUCH MORE details of your processor, architecture, etc....

    I'm currently re-programming my build system for bare-metal LPC8/LPC17/LPC21/LPC43/ADSP-CM403 for Linux/RaspberryPi.

    I stopped using make, because I can't do the things I need with it. If you're interested in it, let me know.

    Marc

Reply
  • Pity, no one answered. Had this problem quite a few years ago.

    Assuming ARM7TDMI in ARM moode, you compile to objects with

    arm-none-eabi-gcc -ffreestanding -mcpu=arm7tdmi -marm -c main.c

    Use your processor or arch (e.g. arch=armv8-m) instead.

    You have to create an index in the .a file with ar option -s. I use:

    arm-none-eabi-ar rcs libmylib.a *.o
    
    

    You can use that libmylib.a when linking with ld or better gcc like shown below. Conversion into .bin or .hex is done with objcopy:

    arm-none-eabi-gcc -o program.elf main.o -Lpath/to/lib -lmylib -ffreestanding -nostdlib -lgcc
    arm-none-eabi-objcopy --only-section=.text --only-section=.rodata --only-section=.data -O binary program.elf program.bin
    arm-none-eabi-objcopy --only-section=.text --only-section=.rodata --only-section=.data -O ihex program.elf program.hex

    Very important and very complex to me was creating a suitable LD-script, which is provided with -T<linkerScript.x> when linking.

    And of course a startup-code (crt0.o) you use from the linker script. But that needs MUCH MORE details of your processor, architecture, etc....

    I'm currently re-programming my build system for bare-metal LPC8/LPC17/LPC21/LPC43/ADSP-CM403 for Linux/RaspberryPi.

    I stopped using make, because I can't do the things I need with it. If you're interested in it, let me know.

    Marc

Children