I'm going to show you how to make and use a library in C.
Library's are no different than regular source files.All they do is break the code up into smaller parts that can be maintained better and easier to read.
It is like your main source file except it has no main function because your program isn't going to start here.
yourlib.c
Now you have to create a header file that prototypes all you functions that you want to use in other source files.Code:/* You must always include your own library.*/ #include<stdio.h> #include"yourlib.h" void celconvert(float Cel) { float Fah; Fah = Cel * 9/5 + 32; printf("%f",Fah); }
yourlib.h
Next you make your main source.Code:/*You have to put extern before your prototyped function you want to use in your library*/ extern void celconvert(float Cel);
You need to include your new library to use it.
main.c
Now to compile you program there are three ways to do it:Code:#include <stdio.h> #include"yourlib.h" int main() { float UserInput; printf("Enter a temperature in celsius to convert to fahrenheit:"); scanf("%f",&UserInput); celconvert(UserInput); return 0; }
If you have a IDE it will handle all the compiling for you.
If you compile from the command line then you have to do it all yourself.
Or use make.
For the tutorial I'll teach you how to do it yourself on the command line but for big projects I strongly suggest you learn how to use make because doing it yourself would take a very long time.
first get to were you have all the source code
The use your you compiler
For this tutorial I'm using MinGW which is a version of GCC If your compiler is different please read the documents that came with it on compiling your code.
First compile you library
gcc -c yourlib.c
Then compile your main
gcc -c main.c
And to make your program
gcc -o Yourprogram main.o yourlib.o
Last edited by twitch; 06-14-2007 at 12:44 AM.
which type of library is this?
is this a dynamic library formation example.
rasp.
desh
It is an object file of which can be linked with other objects in to an executable, it is not a library in the .dll or .so or .lib sense although can become one.
Be sure to read the updated FAQ || Health is achieved through 10,000 different steps.
A textual description can be only part of your question, be sure to provide sample results, errors and your platform in the appropriate forums while asking.
Thanks for sharing this information. It will ery helpful while programming .We can put all function in one library and afterwards we can use it all.
There are currently 4 users browsing this thread. (0 members and 4 guests)
Bookmarks