Lost Password?

Go Back   CodeCall Programming Forum > Software Development > Tutorials > C Tutorials

Vote on your favorite hash algorithm here!

C Tutorials All C Tutorials and Code

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-13-2007, 04:32 AM
twitch twitch is offline
Newbie
 
Join Date: Jun 2007
Posts: 11
Credits: 0
Rep Power: 0
twitch is on a distinguished road
Smile How to make and use a library

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);
}
Now you have to create a header file that prototypes all you functions that you want to use in other source files.
yourlib.h
Code:
/*You have to put extern before your prototyped function you want to use in your library*/
extern void celconvert(float Cel);
Next you make your main source.
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;
}
Now to compile you program there are three ways to do it:

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT -5. The time now is 10:59 PM.

Contest Stats

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

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 83%

Ads