Friday, December 31, 2010

'Hello world' application in C with static library

I will show how to create 'Hello world' application in C, which will use static library.

Firstly, you must create header file for the library. In this file all functions will be declared.

mylib.h
extern void printhw();

Next, you must create source file and implement functions declared in the header file.

mylib.c
#include<stdio.h>
#include"mylib.h"

void printhw()
{
 printf("Hello world!\n");
}

Then compile your library.

gcc -c mylib.c

After compiling the library you must create an application which would use it.

main.c
#include<stdio.h>
#include"mylib.h"

int main()
{
 printhw();

 return 0;
}

Compile your application.

gcc -c main.c

Finally, make and run application.

gcc -o app main.o mylib.o
./app

You must get this result:

Hello world!

No comments:

Post a Comment