How to make a library?

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

Hi All,

I have been using Peter Fleury's lcd library for quite some time now. And I am doing my 3rd app with his library. But until this time, I still include lcd.h and lcd.c in my projects.

I was wondering if I could just include the header file, kind of like how we use _delay_ms(). I mean we just include right?

I have been doing some reading and I "think" this has something to do with .o files? And with the "linker" but I am quite confused so please help me?

Also, I am using AS4 and I have been familiarizing myself with AS6(as they say it is already bug-free?).

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

Quote:
I mean we just include right?
But all of the code is in that file or other .h files that that file includes. With the lcd files, all of the code is in the .c file. So if you don't include lcd.c, you won't have any of the actual code.
Quote:
I have been doing some reading and I "think" this has something to do with .o files?
The .o files are built from the .c files. You could create .a files, but you would have to have a separate .a file for each AVR that you use. And you would still have to link to those files, so it really wouldn't gain you anything except a second or two of compilation time.

Regards,
Steve A.

The Board helps those that help themselves.

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

To answer the technical question as such:

Building an app is done in these steps:

App-1) Source code (.c) is compiled into object code (.o). Each source file is compiled separately (unless you apply special options, which we will not cover here).

App-2) All object files are combined together into one file (.elf). (Had this been on e.g. a U*nix system, this would be the executable.) This is done by the linker, and apart from taking object files, it can also get it's input from libraries (.a). Such libraries are in essence just a glued-together collection of object files and a TOC.

App-3) For AVRs, there is an additional step, taking the .elf and extracting/converting the contents into separate files with data destined for the different AVR memories. The obvious example is creating a .hex file with the program code - destined for flash. But there can also be produced files with EEPROM contents, or values for programming the fuses. The program that does this is called "avr-objcopy".

Now on to libraries:

This starts out just as above..

Lib-1) Source code (.c) is compiled into object code (.o).

Then...

Lib-2) These object files are combined into an "archive" (thus the .a file extension). The program that does this is "as" (in e.g. WinAVR and AVR Toolchain it goes under the name "avr-as"). Be sure to make the name of the library start with "lib" (see below) and it's file extension should be .a .

Your library is done!

You can now use it by specifying it to the linker in an App-2 stage. On a command line you would use the -l switch to name the library to use. When you specify the library name do not incorporate the initial "lib", nor the .a extension. So, to link with libmyutil.a you would simply say -lmyutil.

This is one of my most non-favourite things with the GCC/binutils tool chain. I am totally comfortable with the extension defaulting to .a, but I hate the default "lib" prefix.

If you want to get all the nitty-gritty detail of the process, I would recommend:
1) A study of the MFile utility's template makefile
2) The GNU documentation on GCC and "ar" close by

You could also set up a library project in e.g. Atmel Studio 6 (it has a template for that), and then look at the makefile it generates. I would suspect it to be less fitting for reading by a human than e.g. the MFile template malefile.

HTH!

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]

Last Edited: Tue. Jul 17, 2012 - 10:02 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Even the "second or two of compilation time" really means one or two seconds the first time lcd.c is compiled to lcd.o because after that it's unlikely to change so won't be compiled again and there's even an argument that the linker can link a .o fractionally quicker than a .a!

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

And if I recall correctly, the Fleury LCD code is configured by modifying the .h file, so every time you change the .h file because IO pins are differently, you have to recompile the .c file into library again before the changes take effect.

It is not worth it to make it into a "real" library, keep it as a source file.

What you could do is keep the .c source file in some common place, and have customized .h file for each project. Remember to add the .c source into makefile (or project if you use some GUI) so it gets compiled into object file before linking the binary, preferably in the project folder, not in the library folder.

C:\Projects\MyProject1
C:\Projects\MyProject2
C:\Projects\FleuryLib
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i am not after compilation speed or anything like that, i was just wondering if there was some other (better?) way of doing things.

since i am using Fleury's "library" but i am not actually using it as a "library" per se.

@johan,
thank you for the explanation, i have not digested all of it yet. i will do a bit more reading.

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

Quote:

i was just wondering if there was some other (better?) way of doing things.

Pre compiled libs work for things like sin/cos/tan or printf/strcpy/itoa/etc. because there's nothing particularly machine specific about them (though even for those there are about 10 different versions of .a as some processors have MUL, some don't, some have JMP/CALL while some only have RJMP/RCALL and so on). But for anything that uses the hardware and needs configuration (like which pins an LCD lib uses) there is no advantage in a pre-compiled lib and in fact it's would be very tricky to have it dynamically adjust to different pin layouts at run time - so those decisions are better made at compile time.

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

@clawson
ah.. i understand. thank you.
i have just been doing simple apps and i made it a point to use the same pins for the lcd.

@johan
i have tried as6 library project, works like a charm!
(i am not knowledgable enough to try to invoke gcc on the command line yet, its still the gui for me - thank you very much)

@jepael
i see your point, thanks. i think this is the way to go for me.

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

Quote:
i am not knowledgable enough to try to invoke gcc on the command line yet, its still the gui for me - thank you very much

That's OK, I didn't write it as an instruction on how to do it by hand. But you should understand what is involved and running when you press that build button in your IDE - THAT is why I wrote it.

And you did say

Quote:
I "think" this has something to do with .o files? And with the "linker" but I am quite confused so please help me?

and the best help I could think of at the moment was to straighten out the processes.

You're most welcome! :D

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]