This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Creating a "system" static lib

Hi,

I'm working on a project where I need for some reason to create a "system" static lib.

What I mean by "system" is a lib where all nrf_driver, startup file, etc are grouped in a single .a file.

This will be later used by arm-none-eabi-gcc to compile a single main.c file and linked with the static lib.

To test this I used the 'uart' example :

cd $(UART_EXAMPLE_ARMGCC)
make VERBOSE=1
mv _build/nrf52840_xxaa/main.c.o ..     // So that main.c.o is not integrated in the static lib
arm-none-eabi-ar rcs _build/libnrf52.a _build/nrf52840_xxaa/*.o
arm-none-eabi-gcc -O3 -g3 -mthumb -mabi=aapcs -L ../../../../../../components/toolchain/gcc -Tuart_gcc_nrf52.ld -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -Wl,--gc-sections --specs=nano.specs _build/main.c.o _build/libnrf52.a -o _build/nrf52840_xxaa.out
arm-none-eabi-objcopy -O ihex _build/nrf52840_xxaa.out _build/nrf52840_xxaa.hex
nrfjprog -f NRF52 --program nrf52840_xxaa.hex --chiperase -r
The 'arm-none-eabi-gcc' line is juste the same line prompted when the 'make VERBOSE=1' but i replaced the .in file (listing all objects) by the main.c.o and libnrf52.a
The problem  I have is that there is no output on the uart.
I tried the same process with the blinky example and it works great.
I also tried to replace libnrf52.a with all the .o objects files and it works great.
So the problem comes from the library creation step but I don't know what's wrong with it.
So my question is do you have any idea why it is not working for the uart example ?
Any advice would be appreciated :)
Thanks
  • can you debug into a GCC static library?

    you'll need to do some googling to find out ...

  • Hi and thanks for your answer.

    I don't think that's a problem because a static lib is just a bunch of object files right ?

  • Yes, I'd have thought it should be possible - you're just going to have to search out how to actually do it.

  • In order to get more debug information, I added the -Wl,--verbose flag to the linking step for both Makefile and manual linking with static lib.

    Then I compared the outputs and I found that when I use the static lib, not all the object files in the archive are used for linking.

    Here are the files loaded during linking with the Makefile :

    _build/nrf52840_xxaa/nrf_log_frontend.c.o
    _build/nrf52840_xxaa/boards.c.o
    _build/nrf52840_xxaa/app_error.c.o
    _build/nrf52840_xxaa/app_error_weak.c.o
    _build/nrf52840_xxaa/app_fifo.c.o
    _build/nrf52840_xxaa/app_uart_fifo.c.o
    _build/nrf52840_xxaa/app_util_platform.c.o
    _build/nrf52840_xxaa/nrf_assert.c.o
    _build/nrf52840_xxaa/nrf_balloc.c.o
    _build/nrf52840_xxaa/nrf_memobj.c.o
    _build/nrf52840_xxaa/nrf_strerror.c.o
    _build/nrf52840_xxaa/retarget.c.o
    _build/nrf52840_xxaa/nrf_drv_common.c.o
    _build/nrf52840_xxaa/nrf_drv_uart.c.o
    _build/nrf52840_xxaa/main.c.o
    _build/nrf52840_xxaa/gcc_startup_nrf52840.S.o
    _build/nrf52840_xxaa/system_nrf52840.c.o

    And here are the files loaded during manual linking with the static lib :

    attempt to open main.c.o succeeded
    main.c.o
    attempt to open libnrf52.a succeeded
    (libnrf52.a)app_error.c.o
    (libnrf52.a)app_error_weak.c.o
    (libnrf52.a)app_uart_fifo.c.o
    (libnrf52.a)boards.c.o
    (libnrf52.a)gcc_startup_nrf52840.S.o
    (libnrf52.a)nrf_drv_uart.c.o
    (libnrf52.a)system_nrf52840.c.o
    (libnrf52.a)app_fifo.c.o
    (libnrf52.a)nrf_drv_common.c.o

    I don't understand why not every files are loaded.

    Does someone has any idea ?

    Thanks

  • Ok so I finally succeed to get the output using the static lib I generated.

    In my last comment I said that all object files in the static lib were not linked.

    To force gcc to link every file in the lib I added the flag -Wl,--whole-archive before libnrf52.a. Now we can see that the linker links all objects from the static lib :

    attempt to open libnrf52.a succeeded
    (libnrf52.a)app_error.c.o
    (libnrf52.a)app_error_weak.c.o
    (libnrf52.a)app_fifo.c.o
    (libnrf52.a)app_uart_fifo.c.o
    (libnrf52.a)app_util_platform.c.o
    (libnrf52.a)boards.c.o
    (libnrf52.a)gcc_startup_nrf52840.S.o
    (libnrf52.a)nrf_assert.c.o
    (libnrf52.a)nrf_balloc.c.o
    (libnrf52.a)nrf_drv_common.c.o
    (libnrf52.a)nrf_drv_uart.c.o
    (libnrf52.a)nrf_log_frontend.c.o
    (libnrf52.a)nrf_memobj.c.o
    (libnrf52.a)nrf_strerror.c.o
    (libnrf52.a)retarget.c.o
    (libnrf52.a)system_nrf52840.c.o

    I also added the -Wl,--no-whole-archive after the library that other libraries won't be affected. The final gcc command is the following :

    arm-none-eabi-gcc -O3 -mthumb -mabi=aapcs -L ../../../../../../components/toolchain/gcc -Tuart_gcc_nrf52.ld -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 --specs=nano.specs main.c.o -Wl,--whole-archive libnrf52.a -Wl,--no-whole-archive -o nrf52840_xxaa.out

Related