I hope this is the right part of the forum for this.
I want to create a C library for use in my programs using atmel studio 7
I first chose static C library at the intro screen. I wrote a very simple library containing one simple function. I also wrote a .h file to go with it. I built this with no problems.
I next wrote a simple c program who's only purpose was to call the function in the library.
I now get errors. I was able to get Atmel studio to find the .h file, but i can't figure out how to get it to find my library itself. I'm not even certain which file I want it to locate. Does it use the .a file? That's not clear to me. There is also a .d file (presumably i built a debug version. I can't see that that matters though).
How libraries work is only vaguely clear to me. Tell me if this is correct: I write many .c files containing my functions, and one .h file for the whole shebang. When I build, it creates the .a file which somehow tells the linker which .o files to grab. Is that about right?
I don't use Studio but your idea is the correct one: once the compiler generates the .a file, you should put it in the Studio/GCC library directory (unless Studio does it for you). You will need to specify your new library in the Studio/GCC linker option as well so that the compiler knows you want to link with that library with a particular project.
Do I need the .o files any more at that point? In other words, does the .a file contain all the object code? I'm not clear if the .a is a gathering of all the .o files under one roof, or if it's just a directory of them for the linker to use. If the .a is everything, does that mean the linker can separate out the stuff I use, or does this mean if I need just one part of a library, I still wind up having to load the whole thing?
.o (object) and .d (dependency) files are intermediate output files from before linker stage. You can open a lot of those file with notepad.
The .a (or .lib/.hex..bin) is the output of the linker. Which is your library.
Which you'll need to feed to the next project as dependency.
I'd suggest you find the manual of the compiler. Since these are kind of toolchain basics you'll need to know when compiling and using libs.
Otherwise you'll be asking every step on the internet. Which does not make one very self sufficient.
"How libraries work is only vaguely clear to me. "
What a "library" is depends on the person using that term.
Some use it to mean a collection of header and source files that can be reused. For those, you simply copy them to your project folder and include them in your project. Nothing else.
Others will consider special precompiled binary files and associated header files that you link into your project. You will have to set up the preprocessor to use the header files and the linker to include the binary files. Didn't sound like you did the later in your project.
Jeron3, have you read the Atmel Studio documentation? It's pretty useless in many cases. Alot of "what is the X button? The X button turns on X. Moving on..."
@Danny. I know I need to show the linker how to find my binary (the .a file if I'm understanding correctly). It's how I do that in atmel studio that's stumping me. I my library in the solution manager, and added it's file path in the linker settings. I also added the correct path to the include settings.
I get an error saying my library function is on referenced. I don't get an error about my include statement, so it's finding the .h file.
Jeron3, have you read the Atmel Studio documentation? It's pretty useless in many cases. Alot of "what is the X button? The X button turns on X. Moving on..."
Yes. But you're looking at the wrong place. You're not going to find compiler documentation in Eclipse either, it's the IDE.
You need to look at
gnu gcc, which is the compiler used. At least, last time I checked it didn't contain a proprietary compiler.
Lack of documentation is often something you have to deal with when using "free" IDE's.
To the above linked references I would add that you really need to know how to specify a search path for the linker. Read up on the GCC linker and Makefiles.
Those links are just what I need.
I am hoping to do this through the IDE though. I would rather not have to edit makefiles. I feel like I should have left those behind with my DOS days. The links you posted tell me what the makefile should look like though, so I can at least tell if I'm doing it right with the IDE. I'll edit makefiles by hand if I must. I think I have the info I need to do what I want now.
Thanks to all who helped!
Update: its solved.
As is often the case, my problem was completely different than I thought it was.
Turns out I'd set up my library and paths properly.
What had happened is when I created the Atmel Studio solution for the library, I then created a new .c file for my function . Silly me didn't realize that I had to specifically add this file to the solution. What confused matters was that the empty template library1.c file that the solution created had just enough code in it to compile and make me think all was well, when actually my code wasn't even being linked in.
In the process, I did learn alot about how libraries work in Atmel Studio, so that's good.
One thing still isn't quite clear to me. Say I have a library with ten functions in it, five of which are prototyped in one .h file, and five in another. Then say I only need 1 of the ten functions, and as such, only #include the one relevant .h file. Which of the three possibilities below happens:
1:All object code in the library, regardless of whether its referenced in the application, or in any any included . h file, is linked in.
2:All the functions referenced in the included .h file are linked in, but nothing else.
3:Only the functions actually used are linked in.
I think it's number 1, but still wonder if it's number two. I'm pretty certain its not number 3.
Can someone help?
It depends on the compiler, but most compilers, including GCC, actually use #3 ;-)
We compiler writers are kind of crafty in that way.