sample Makefile that create a library out of *.c/*.ccp files

Go To Last Post
15 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

It's been awhile since I created a library. I vaguely remember that one uses the AR (archive) command to do so. But, I would like a nice/simple sample or template (even portable between WINDOWS/Linux for AVR GCC) of Makefile to create a library out of *.c/*.cpp files. I can later add rules to do proper export of *.h and *.lib files to a user directory.

Thanks.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I thought the Mfile template makefile has rules etc for building a library, but when I checked it turns out it does not.

We have a tutorial (or rather a starting point with links) here: https://www.avrfreaks.net/index.p... .

This falls apart into two distinct problems:

1. The commands needed to build a library (i.e. what would you type on a command prompt if you did this by hand). Mainly about the ar utility (known as avr-ar in e.g. WinAVR).

2. The rules, variables and stuff needed in a makefile.

Are you having troubles with 1, 2 or both? Answers to 1 should be found following the links in the tutorial. Let us know if you need help with 2.

I'd go for a modification of the Mfile template, adding the rule for creating the library. Other rules (e.g. for compiling) are already there. Should not need much more than adding one rule..

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks, all, for the responses. I should be able to carry on with your responses. I'll post if there is any problem.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I never bothered figuring that out. I just put the source files in a separate folder. It works for me. What am I missing by not using linker libraries?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

steve17 wrote:
I never bothered figuring that out. I just put the source files in a separate folder. It works for me. What am I missing by not using linker libraries?

The whole point of the Make utility to make sure all dependencies are built accordingly. If you change a line of code of a library that say 1000 other libraries and binaries depend on, when you do a build of those 1000 things, Make knows that it needs to build that one library with the one line change and export that library out so that the other 999 things to build need to only use the latest library that you changed. Can imagine doing that manually? :-)

And, then, of course, you want your stuff to run WINDOWS then Linux, etc.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

unebonnevie wrote:
Thanks, all, for the responses. I should be able to carry on with your responses. I'll post if there is any problem.

Ok, a tiny problem.

Compiling is fine. Linking is the problem of not finding the libspi.a the directory above c:\projects\test, which is c:\projects\lib and it does have the libspi.a in it.

COMPILE
-------

avr-gcc.exe  -mmcu=atmega8 -DF_CPU=16000000U -I../include -Wall -gdwarf-2
-std=gnu99 -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
-MD -MP -MT my_prog.o -MF dep/my_prog.o.d  -c  ../my_prog.c

LINKING
-------

avr-gcc.exe -mmcu=atmega8 -L../lib -lspi -Wl,-Map=my_prog.map my_prog.o -o my_prog
my_prog.o: In function `main':
c:\project\test\my_prog.c:534: undefined reference to `SPIInit'
make: *** [my_prog] Error 1

c:\project\lib has:
 libspi.a

c:\project\include has:
 spi.h

Even putting

-L. -lspi

with libspi.a in the current directory does not help.

Here is how I built my libspi.a:

ar -r libspi.a spi.o
ranlib libspi.a 

And running nm on libspi.a:

c:\project\lib>nm libspi.a

spi.o:
0000002c T SPIChipSelect
00000000 T SPIInit
00000054 T SPIReadByte
00000038 T SPIWriteByte
00000040 T SPIWriteStr
00000034 a __CCP__
0000003e a __SP_H__
0000003d a __SP_L__
0000003f a __SREG__
00000000 a __tmp_reg__
00000001 a __zero_reg__
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The order of object files and libraries on the linking command line is important. The "-lspi" must be behind the object files.

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

unebonnevie wrote:

The whole point of the Make utility to make sure all dependencies are built accordingly. If you change a line of code of a library that say 1000 other libraries and binaries depend on, when you do a build of those 1000 things, Make knows that it needs to build that one library...

Actually, that's not a problem. I include the source in all the projects that need the code. If the source changes, then it would get recompiled.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Does that mean that you have several physical opies of the same source ode - one copy in every project?

Or does it mean that all projects "reference" one and the same copy of the library routines?

Either way, I smell dragons... (Though the former case might be less severe if a competent source control system, e.g. Subversion, is in place.)

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

JohanEkdahl wrote:
I thought the Mfile template makefile has rules etc for building a library, but when I checked it turns out it does not.

WinAVR comes with a sample Makefile for building a library.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

WinAVR comes with a sample Makefile for building a library.

Oh, thank you. Nice to hear that it wan't an indicator of insanity, but that I've actually had seen this somewhere. (My WinAVR-equipped machine crashed Windoze again - or some such. Will take a few days before I get it up and running again. Or just buy a new one.)

If Jörg sees this: Any chance of "backporting" that into the Windows "distribution" of Mfile (the ZIP file) on your site?

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

steve17 wrote:
I never bothered figuring that out. I just put the source files in a separate folder. It works for me. What am I missing by not using linker libraries?

Libraries are collections of pre-compiled code, i.e. object code. Usually libraries are built in such a way that there is 1 function per object module. The linker works by pulling in the object module required for the reference in the main application code. This means that only that one function will be linked in.

Here's the advantage of using a library over other methods:

1. If you add source code modules from one project to another project, you need to go through those modules to determine which functions you will need to use on that project, and discard the rest. With libraries, this will be done automatically when you link, as it will link only the items you need and not link functions that are never referenced.

2. If you compile all of your functions in one single object code module, then that entire object code module will be linked into your application, whether or not you are using all of those functions. This can cause code bloat. With a properly designed library, you generally have only 1 function per object module (there can be exceptions, e.g. ISRs) which makes for efficient granularity, and then the linker automatically takes care of it for you.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

JohanEkdahl wrote:
Quote:

WinAVR comes with a sample Makefile for building a library.

Oh, thank you. Nice to hear that it wan't an indicator of insanity, but that I've actually had seen this somewhere. (My WinAVR-equipped machine crashed Windoze again - or some such. Will take a few days before I get it up and running again. Or just buy a new one.)

It's actually been in the WinAVR distribution for quite some time.

JohanEkdahl wrote:

If Jörg sees this: Any chance of "backporting" that into the Windows "distribution" of Mfile (the ZIP file) on your site?

My guess is, probably not. They're different kinds of Makefiles, due to separate kinds of targets and commands and I don't see how they can be easily integrated into a single Makefile. Hence the reason why it is a separate sample Makefile in WinAVR.

Perhaps some clever person can figure out how to integrate the two.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sternst wrote:
The order of object files and libraries on the linking command line is important. The "-lspi" must be behind the object files.

The above tip works. So, the correct way is NOT to modify the LDFLAGS, but to do the below.

1. LIBDIRS += -L$(YOUR_LIB_DIR)
2. LIBS += -lspi

"-lspi" is for my case, but you can put any of your libraries.

This works because both LIBDIRS and LIBS are built-in macros or pre-defined for Make utility.