|
||||||
| C Tutorials All C Tutorials and Code |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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 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 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 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 02:44 AM. |
| Sponsored Links |
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
| Xav | ........ | 1357.94 |
| MeTh0Dz|Reb0rn | ........ | 1077.71 |
| WingedPanther | ........ | 919.18 |
| marwex89 | ........ | 906.86 |
| morefood2001 | ........ | 900.18 |
| John | ........ | 890.77 |
| Brandon W | ........ | 770.65 |
| chili5 | ........ | 312.39 |
| Steve.L | ........ | 264.99 |
| dcs | ........ | 234.34 |
Goal: 100,000 Posts
Complete: 83%