creating and using my own libraries with atmel studio 7 - Page 1
Forum section: electronics / general

author: Dan Moos
author_rank: Frequent Contributor
timestamp: July 05, 2016, 02:16:42 am
content: 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 7I 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?

author: richardman
author_rank: Frequent Contributor
timestamp: July 05, 2016, 05:14:38 am
content: 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.

author: Dan Moos
author_rank: Frequent Contributor
timestamp: July 05, 2016, 05:32:03 am
content: 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?

author: Jeroen3
author_rank: Super Contributor
timestamp: July 05, 2016, 07:13:17 am
content: .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.

author: dannyf
author_rank: Super Contributor
timestamp: July 05, 2016, 10:10:29 am
content: "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.

author: Dan Moos
author_rank: Frequent Contributor
timestamp: July 05, 2016, 12:56:27 pm
content: 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..."

author: Dan Moos
author_rank: Frequent Contributor
timestamp: July 05, 2016, 01:08:21 pm
content: @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.

author: Jeroen3
author_rank: Super Contributor
timestamp: July 05, 2016, 01:56:32 pm
content: Quote from: Dan Moos on July 05, 2016, 12:56:27 pmYes. 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 atgnu 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.

author: bingo600
author_rank: Super Contributor
timestamp: July 05, 2016, 06:23:03 pm
content: Maybehttp://www.nongnu.org/avr-libc/user-manual/library.htmlAndhttp://www.avrfreaks.net/comment/511512#comment-511512/Bino

author: rstofer
author_rank: Super Contributor
timestamp: July 05, 2016, 11:08:45 pm
content: 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.

author: Dan Moos
author_rank: Frequent Contributor
timestamp: July 06, 2016, 12:45:17 am
content: 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!

author: Dan Moos
author_rank: Frequent Contributor
timestamp: July 06, 2016, 03:01:34 am
content: 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?

author: Jeroen3
author_rank: Super Contributor
timestamp: July 06, 2016, 05:33:07 am
content: You can choose between 1 and 3 using compiler/linker flags. (of tickboxes if the IDE does the makefile)Most compilers use a default setting that only used sections are included in the final build.Take a look at the map file if you want to know what is included.http://www.atmel.com/webdoc/AVRLibcReferenceManual/group__demo__project_1demo_project_map.html

author: richardman
author_rank: Frequent Contributor
timestamp: July 06, 2016, 05:33:24 am
content: It depends on the compiler, but most compilers, including GCC, actually use #3 ;-)We compiler writers are kind of crafty in that way.