Topic is called writing libraries.
I will be covering creating and using static and shared libraries. (Yes using too, because that is the main part)
So here is simplest you can do
I will write two files containg a function each, write one header file and use those function in a prog.
Go into some directory
create a dirctory called "incl" this is the place where your manual created header files will be searched.
go to "../incl" create a header file called "myheader.h" and write folloing lines there
#include"stdio.h"
extern void one();
extern void second();
save it... Thats it, your header file is ready.
Now the second part is to create library......Ok there are 1001 documents on net which explains you what a lib does and all that blah blah.. I will just focus here how to create a library.
First I am going to create a static library.
come to base dir and create a dir called "mylib"
Go to mylib dir and create two .c files
first.c
return type one()
{
print("\nThis is from the first libraray\n");
}
second.c
return type second()
{
print("\this is from the second library\n");
}
now just compile these files ( you can not create a executalbe as there is no main funciton here)
gcc -c first.c -o first.o
gcc -c second.c -o second.o
Now you have two objects file with you.
You want to create the static lib.
Ok to be very frank static lib is nothing but puting the all the concerned object files in a single file.
Nothing more. This is done by
1) ar rcs libfirstlib.a first.o second.o
This command is just like ar (archiving the many object files into one )
Now few points to be noted here, your lib must start with "lib" prefix and extension for static is .a.
2) To just make indexing fast in the lib, you CAN (that is it may or may not be needed lib will work even without it)
ranlib libfirstlib.a
Thats it your static lib is ready.
Now comes the part of using this lib and header file into your program.
Come to the base lib again
//use.c
include file "mylib.h"
include file
main function come here()
{
one();
second();
prin("\nBoht of the lib funcitons called .....\n");
}
Write one program here using the lib function and include the header file you have crated.
Ok the thing is simple your compiler and linker don't know anything about where is your .h file which you have included in your program.. nor linker will know anything about the fucntion defination.
So how to do it.
Here is the logic
when compling and linking ( as you know " gcc file.c" is equivalent to compling and linking if you do not want to link you use "gcc -c")..you need to give the header file and lib path where preprocessor and liker will look for.
By default preprocessor will look in some directory (defined differnetlly form sys to sys)
Gen it is /use/include
and lib in /use/lib
Now suppose you do not have premission to write there and location of you header file and lib is different then use this.
gcc -I ./inclu/ -L ./mylib/ -static use.c -lfirstlib
Thic command will do.
-I tells where to look for a header file
and -L about the lib file.
Note you can also copy .h to /usr/include and .a to /usr/lib dir it will work.
So I explained all the synario here about static lib.
Now its turn of shared library. And guess what Shared library I have created is not working. Will post this some time later.
Very Useful links to read are mentioned below
http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html
http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html
http://www.gpres.org/tutorials/shared.xhtml
http://gcc.gnu.org/ml/gcc/1999-12/msg00092.html
http://www.iram.fr/~roche/code/c++/c++_AddNumbers.html
http://www.linux.org/docs/ldp/howto/Program-Library-HOWTO/shared-libraries.html
http://randu.org/tutorials/c/libraries.php
http://randu.org/tutorials/c/
I will be covering creating and using static and shared libraries. (Yes using too, because that is the main part)
So here is simplest you can do
I will write two files containg a function each, write one header file and use those function in a prog.
Go into some directory
create a dirctory called "incl" this is the place where your manual created header files will be searched.
go to "../incl" create a header file called "myheader.h" and write folloing lines there
#include"stdio.h"
extern void one();
extern void second();
save it... Thats it, your header file is ready.
Now the second part is to create library......Ok there are 1001 documents on net which explains you what a lib does and all that blah blah.. I will just focus here how to create a library.
First I am going to create a static library.
come to base dir and create a dir called "mylib"
Go to mylib dir and create two .c files
first.c
return type one()
{
print("\nThis is from the first libraray\n");
}
second.c
return type second()
{
print("\this is from the second library\n");
}
now just compile these files ( you can not create a executalbe as there is no main funciton here)
gcc -c first.c -o first.o
gcc -c second.c -o second.o
Now you have two objects file with you.
You want to create the static lib.
Ok to be very frank static lib is nothing but puting the all the concerned object files in a single file.
Nothing more. This is done by
1) ar rcs libfirstlib.a first.o second.o
This command is just like ar (archiving the many object files into one )
Now few points to be noted here, your lib must start with "lib" prefix and extension for static is .a.
2) To just make indexing fast in the lib, you CAN (that is it may or may not be needed lib will work even without it)
ranlib libfirstlib.a
Thats it your static lib is ready.
Now comes the part of using this lib and header file into your program.
Come to the base lib again
//use.c
include file "mylib.h"
include file
main function come here()
{
one();
second();
prin("\nBoht of the lib funcitons called .....\n");
}
Write one program here using the lib function and include the header file you have crated.
Ok the thing is simple your compiler and linker don't know anything about where is your .h file which you have included in your program.. nor linker will know anything about the fucntion defination.
So how to do it.
Here is the logic
when compling and linking ( as you know " gcc file.c" is equivalent to compling and linking if you do not want to link you use "gcc -c")..you need to give the header file and lib path where preprocessor and liker will look for.
By default preprocessor will look in some directory (defined differnetlly form sys to sys)
Gen it is /use/include
and lib in /use/lib
Now suppose you do not have premission to write there and location of you header file and lib is different then use this.
gcc -I ./inclu/ -L ./mylib/ -static use.c -lfirstlib
Thic command will do.
-I tells where to look for a header file
and -L about the lib file.
Note you can also copy .h to /usr/include and .a to /usr/lib dir it will work.
So I explained all the synario here about static lib.
Now its turn of shared library. And guess what Shared library I have created is not working. Will post this some time later.
Very Useful links to read are mentioned below
http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html
http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html
http://www.gpres.org/tutorials/shared.xhtml
http://gcc.gnu.org/ml/gcc/1999-12/msg00092.html
http://www.iram.fr/~roche/code/c++/c++_AddNumbers.html
http://www.linux.org/docs/ldp/howto/Program-Library-HOWTO/shared-libraries.html
http://randu.org/tutorials/c/libraries.php
http://randu.org/tutorials/c/
No comments:
Post a Comment