Building a static library

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, utzig, tfAteba, barthess

User avatar
Tectu
Posts: 1226
Joined: Thu May 10, 2012 9:50 am
Location: Switzerland
Contact:

Building a static library

Postby Tectu » Fri Apr 18, 2014 2:54 am

I'm trying to create a static library (*.a file) containing an entire project for the STM32F407-Discovery board. The goal is it to have a static library which can be compiled/linked with only a main.c to create a working project. However, I horribly fail at it.

What I did is taking the /demos/ARMCM4-STM32F407-DISCOVERY project and modify the Makefile. I removed main.c from the CSRC variable. Then I added the following to the very bottom of the Makefile:

Code: Select all

chibios.a: $(OBJS)
   arm-none-eabi-ar r $@ $^

This way I could create the chibios.a library by executing

Code: Select all

make chibios.a

To create the actual project, I added only the main.c to the CSRC variable and I also added the chibios.a library to the ULIBS variable. Then I ran the Makefile normally just by executing

Code: Select all

make
Everything compiled and linked successfully. However, the created binary does not work at all (nothing gets executed at all).

I sadly don't know where the problem is but I am quite sure that it has something to do with how I create the static library. I don't really see through all the Makefile magic inside of /os/ports/GCC/ARMCMx/rules.mk to find out what I'm doing wrong.

I attached the .map file of the working project and the one with the library. The .map file of the library project does show some difference to the other at the start address:

Code: Select all

startup
 *(vectors)

constructors    0x08000000        0x0
                0x08000000                PROVIDE (__init_array_start, .)

whilst the working one is

Code: Select all

startup         0x08000000      0x188
 *(vectors)
 vectors        0x08000000      0x188 build/obj/vectors.o
                0x08000000                _vectors
However, I don't really know how to fix this.
Can anybody help?


~ Tectu
Attachments
map_files.zip
(34.81 KiB) Downloaded 286 times

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: Building a static library

Postby Giovanni » Fri Apr 18, 2014 7:16 am

I have not tried this yet but I imagine it could be useful.

I would look into adding one more target "lib" to rules.mk, then, by giving the "make lib" command a library would be built.

Giovanni

User avatar
Tectu
Posts: 1226
Joined: Thu May 10, 2012 9:50 am
Location: Switzerland
Contact:

Re: Building a static library

Postby Tectu » Fri Apr 18, 2014 11:07 am

This would indeed be useful as I'm in the process of writing a tutorial on how to use Em::Blocks with ChibiOS/RT (and optionally uGFX). It looks like you'd like to move away from Eclipse as well and I'm very satisfied with Em::Blocks so far.

I agree that it would be the best to add everything into the rules.mk Makefile. However, as long as I don't know what I have to put there, it is pretty hard for me. As everything links without any problems, I'm sure that I don't miss any object files. When looking at the resulting *.map file, it clearly looks like some startup issue.
Giovanni, is there some ChibiOS/RT black magic hidden inside which you never told anybody about it? :P


~ Tectu

Abhishek
Posts: 266
Joined: Wed May 23, 2012 3:15 pm
Location: India

Re: Building a static library

Postby Abhishek » Fri Apr 18, 2014 8:32 pm

From what I understand ChibiOS as a whole will perhaps not be able to be linked as is because of the interrupt tables that are getting linked in as well, the application entry point is actually in vectors.c of the particular ChibiOS port (in this case under /ports/GCC/... ) and indeed if it could be done, it would also require relocation of interrupt tables as the HAL also uses ISRs and statically linking them in without the compiler being aware of it would be akin to their absence, which explains why your code doesn't execute.

Possible solutions:
  • Make the compiler aware of the ISRs and other stuff in the libchibios.a so that it can place it accordingly
  • Put the port, or ISR specific stuff with the application but statically link the other code [the kernel code that really doesn't need to be hardware specific].
  • Compile and put ChibiOS separately and expose a function pointer based API interface for the application to call ChibiOS functions and vice versa. Something like CircleOS App loader - visit stm32circle.com
  • Some more GCC and linker magic that I am not aware of.
  • Compile ChibiOS together with the application (which we are trying to avoid in the first place)

The best solution would be perhaps to build a plugin for any IDE of choice to do the hard work of copying the files and setting appropriate dependencies. Any other compiler magic might bloat the code. How much, I cannot say.

Regards

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: Building a static library

Postby Giovanni » Fri Apr 18, 2014 8:40 pm

The solution is to put in the library only the generic modules. Startup files and vectors table belong to the application.

Giovanni

nad
Posts: 30
Joined: Fri Dec 16, 2011 9:20 am

Re: Building a static library

Postby nad » Thu Jul 31, 2014 10:23 pm

Is there some simple Makefile change to build a ChiBios library?

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: Building a static library

Postby Giovanni » Fri Aug 01, 2014 7:07 am

Hi,

I think it would require a new ad-hoc makefile because all sources are assigned to the same variable in current makefiles.

Giovanni

nad
Posts: 30
Joined: Fri Dec 16, 2011 9:20 am

Re: Building a static library

Postby nad » Fri Aug 01, 2014 7:32 am

Unfortunately I am not a Makefile guru.
So I used what the previous user had mentioned:
modified a Makfile in one of the demos directory
chibios.a: $(OBJS)
arm-none-eabi-ar r $@ $^

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: Building a static library

Postby Giovanni » Fri Aug 01, 2014 7:41 am

I will give it a try, will post here.

Giovanni

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: Building a static library

Postby Giovanni » Fri Aug 01, 2014 8:12 am

It works, I will add the change to 3.0 but not to 2.6.x, way too many makefiles to update.

If you want to add this to 2.6.x then you can add this to rules.mk just before the clean target (note you have to use TABS in targets, not spaces):

Code: Select all

lib: $(OBJS) $(BUILDDIR)/lib$(PROJECT).a

$(BUILDDIR)/lib$(PROJECT).a: $(OBJS)
   @$(AR) -r $@ $^
   @echo
   @echo Done


Then add to the makefile:

Code: Select all

AR   = $(TRGT)ar


Use with: make lib

Note that probably you will have to make 2 makefiles, one for building the lib only the second with everything else (your application).

Giovanni


Return to “General Support”

Who is online

Users browsing this forum: No registered users and 13 guests