NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Where do I find the C code of the functions in the library.

April 14, 2014
by jmuthe
jmuthe's Avatar

I understand that when you create a library of functions, it takes three different files to do so. First you create the function in one C program, then you create an h file, and then you create the main program that accesses the function. For example, if we want to write a program that uses the LCD, we need three files which is our main program, lcd.h, and lcd.c. lcd.c contains the acual function and it could easily be viewed in the Nerdkit library. However, I am having difficulty finding the actual code written for many different AVR functions. For example let's say that I want to see the actual function for some some of the math function like "log10()" or "sqrt()". I know the h file is written in "math.h" and I know where that is located. However,the h file doesn't show the actual code for the function. Shouldn't there be a c file (like math.c for exampe) that shows the code for the actual functions? I looked all over for it and couldn't find it. Where is it located and what is the c file called?

April 14, 2014
by JimFrederickson
JimFrederickson's Avatar

That is part of the GCC AVR Toolchain Distribution. That is not code that Nerdkits has any hand in creating.

The GCC AVR Toolchain is distributed as a unit that has been tested and works.

That would be something that is never changed, or modified.

That is why you will not find them in the standard installation.

I think this location has the Source Files for the Libc Library.
(It is not something I have ever looked into so I may be incorrect.)

GCC AVR Libc Source

April 18, 2014
by jmuthe
jmuthe's Avatar

First of all, I understand that Nerdkit didn't make the "math.h" function. I merely tried to compare an h file written by Nerdkits with one that came with the AVR library. Secondly, I am confused as to why I can't see the actual code that makes a lot of these functions. Is there any place that I could actually see it or is it something that is hidden by the ATMEL corporation. Thirdly, although it would be nice to actually see the code used to make the AVR functions I don't really need it. I just want to understand how to use these functions. Obviously a lot of the functions used for "math.h" are pretty obvious. The name basically tells what they do. So the sqrt() function finds the square root of a number, and the sin() function finds the sine of a number. However, I see many of these AVR functions in code, like fdevopen() and FDEV_SETUP_STREAM(),and I am not sure what they do exactly.

I have previously viewed the AVR Website and I do see all of the AVR h files and it briefly describes each function in the library. However, I feel that it doesn't explain a lot of the functions too clearly for someone not familiar with the functions. I believe that that the Website is just used to remind people how certain functions work. However, I need something that explains those functions to me in a clearer way but I couldn't find the information. I have also read a C programming book and could not find a reference to many of those functions. Does anybody know where I could find information that will give me a better explanation of all the AVR functions.

April 19, 2014
by Noter
Noter's Avatar

The gcc compiler is open source and you can see all of it if you wish. Same goes for avr libc. Those are the places where you will find the sources to all the built-in functions. Jim's link will get you started. I haven't needed to look at it but I'm sure it's complex code and if your programming skills are less than advanced you'll have a hard time figuring out where everything is as well as what exactly it is doing. I have referenced the libc documentation quite a bit and that is where you can find information on FDEV_SETUP_STREAM(). I also search the internet whenever I need a better understanding of how something works. Usually I find many examples as well as various explanations on a topic. Occasionally I can't find the answer and post a question on AVR Freaks. Answers typically come in a couple of hours. However the AVR Freak folks may roast you if you don't exhaust the search engine results first, they know if the answer is already out there or not. Otherwise they're real nice.

If you're a windows guy then you'll probably have a rough time with open source stuff because most of it is developed on linux and sometimes difficult to follow if you're not familar with the linux environment. If that's the case you should 86 your windows box and get started with Ubuntu or one of the other linux distro's and start learning. I did that a while back and it was tough at first but now I couldn't be happier. For example, I downloaded the avrdude source and compiled it on my system so I could put debug messages in various places to figure out how it works. Did the same thing with GTKterm because it was broken so I downloaded, built, and then fixed it and it works great now. So far all the open source stuff I am interested in is written in C and that makes it easier since C is my primary language anyway. And my absolute favorite part is I don't spend money on Windows or Windows software any more which means I can buy more parts and fun stuff for my avr projects!

April 19, 2014
by JKITSON
JKITSON's Avatar

NOTER:

I have just installed Linux mint16 cinnamon. So far (my second day) have done some things with it. It looks & kinda works like my old XP. Now to try to load some of the windows programs I need to use... Jim

April 19, 2014
by Noter
Noter's Avatar

Good move! I think you'll be happy in the long run.

In my case the only windows program I really care about is for my MSO-19 logic analyzer/scope and it wouldn't work with the windows emulator (Wine) so I installed a VMware Player virtual machine and loaded XP on it and now I can run the analyzer. I keep thinking I will get a linux compatible analyzer but haven't yet.

One of the first things I did on linux was get the toolchain installed and working so I could carry on with my avr projects. As I recall it wasn't too hard but as my first task it took some time to get it going.

June 15, 2014
by jmuthe
jmuthe's Avatar

I has been a while since I wrote on this thread but I want to ask again because I didn't really understand the answers to my question. I am not sure that I was clear enough with my question so I will try to reword it. If a programmer wants to create a shared function file in c, they need to use three files. They are the header file which sets up the function parameters, the implementation file which implements the function, and a main program which calls the function. I created a simple example of a shared function file that simply adds two numbers together. First I created the header file and called it "header.h". This is the code.

    int add(int,int);

Then I created an implementation file called "implementation.c"

#include "header.h"
int add(int a, int b)
{
int c =a+b;
return c;
}

Finally, I created the main program which uses the add function file and called it main.c:

#include <stdio.h>
#include "header.h"

int main()
{
     int a=2;
    int b=5;
    int c=add(a,b);
    printf ("c = %d",c);
    return 0;
}

It is easy enough to understand the header and implementation files of functions that are created by myself, other programmers, and the Nerdkit creators. However, when I look at the standard header files that come with the compiler (math.h, stdio.h) they confuse me.

The first problem I have is that I notice that I have multiple standard header files on my computer with the same name but different code on it. I believe that this is because I have different compilers on my computer which have different versions of the standard header file. For example, stdio.h for one compiler would be different code for stdio.h of another compiler and I have both of them on my computer. Is my theory accurate. If so, it leads to more questions. Does a standard header file like stdio.h do the same thing for one compiler as it does for another compiler? If so, then why is the code written for stdio.h different for each compiler? If stdio.h does different things for different compilers then why give them the same name? Wouldn't that lead to confusion?

I still want to know where where the implementation files are to each header file. For example, I would assume if there is a header file called stdio.h and math.h then there should be a file somewhere on my computer called stdio.c and math.c which shows the implementation of these functions. I know that the implementation file doesn't have to have the same name as the header file but I assumed it should exist somewhere on my computer. I asked this question in my first post but I didn't really understand the answer that you all gave me, so I want a clear answer to my next question. Do the implementation files for the standard header files exist on my computer? Yes or no? If they do exist then what are their file names. If they don't exist then how is it possible? How could a header file do anything if it contains the function parameters to a function that doesn't exist? These standard header files have been confusing me for a while so if you provide with an answer please be as specific as possible and thanks again for all your help.

June 16, 2014
by BobaMosfet
BobaMosfet's Avatar

jmuthe,

No, the compiler maker didn't provide you with their source files-- you don't need them. Email me, I'll explain it.

BM

June 16, 2014
by jmuthe
jmuthe's Avatar

Thanks BobaMosFet. I can't E-mail you though because you have not given me you E-mail address so I will give you mine. It is jmuthe@yahoo.com. It is not my main E-mail account so it is no big deal if I give it out. I appreciate your help but I wonder why you want me to exchange the information through E-mail. Why not provide the information on this forum so that everyone can have access to the answer.

June 16, 2014
by BobaMosfet
BobaMosfet's Avatar

My email is listed elsewhere here on the site, sorry, I thought everyone had found it. I'm on a cell right now, would address the other when I got to a terminal.

BM

June 16, 2014
by BobaMosfet
BobaMosfet's Avatar

jmuthe,

There are 3 kinds of files. '.c' (which is program logic), '.h' (which is mostly for external references), and shared libraries (compiled linkable objects-- like stdio).

In many cases, for performance reasons, compiler makers don't provide c sourcecode to their libraries because how they do something may be proprietary, it may be written in assembly, or they may just not want to. What they do provide is a linkable library and a header file. I'm sure in the case of GNU, if you look on Google however, you can find sourcecode to the stdio library on GNU's site or there official repository.

So when you include a .h file for stdio and call stdio functions, the compiler gives you access to only what you need to know about within the library via the header file, and it links in just the pieces of the library you use (lower-quality or early compilers linked the entire object in).

BM

June 16, 2014
by jmuthe
jmuthe's Avatar

So you are saying that the implementation file in C doesn't exist on my computer. Are you saying that the people who created the compiler, created an implementation file in whatever programming language that they wanted? Then those files were automatically attached to the compiler when we downloaded it so that the compiler automatically reacts to the standard header files? This is how I interpret what you are saying. Is it correct or not?

June 16, 2014
by BobaMosfet
BobaMosfet's Avatar

jmuthe,

The 'implementation' file, the '.c' (source) file (more than one was used to make the library for stdio) is not included on your computer. The people who wrote the compiler wrote all the 'implementation' files in either C or assembler, or a mixture of both. Then they precompiled all of their source files for the basic functions that C supports into a '.a' file. A '.a' file is an archive of object ('.o') files that are relatively addressed so pieces can be pulled in and compiled into your program when Make is executed (by the linker and the assembler). For GCC, the file in question is: 'libgcc.a' found in the install directories where the compiler was installed - There are others for specific additions and for other reasons, but all the basic 'required' C functions are in libgcc.a.

BM

June 17, 2014
by JKITSON
JKITSON's Avatar

BM

Thanks for the info. You have helped me get a better understanding of "C" & compilers.

Jim

Post a Reply

Please log in to post a reply.

Did you know that 20 LEDs can be controlled from 11 microcontroller pins, to make a twinkling heart outline? Learn more...