Page 1 of 1
Libs
Posted: Mon Apr 07, 2003 3:44 pm
by Euler
Hi,
Well, i'd like to know how does the libs work. I mean, when in linux for ex, i make a program and include the stdio.h ( in stdio.h i only hace the heads ), how do the OS to include the function and not only the header? I want to do this libc, but don't know where to start.
bye.
Re:Libs
Posted: Mon Apr 07, 2003 4:29 pm
by Tim
Compile your C library sources to .o files as normal. Combine these into one .a using the ar program:
Code: Select all
ar rcs libc.lib strcpy.o strlen.o fopen.o
Re:Libs
Posted: Mon Apr 07, 2003 11:54 pm
by surya4friends
Tim,
I was not able to understand (right (?)) what's asked and what was answered! Sorry about that!
You gave example: ar rcs libc.lib strcpy.o strlen.o fopen.o
Now, Is this ar utility by any means replacing existing functionality of strcpy, strlen, fopen in libc.lib gets replaced with custom strcpy, strlen, fopen.
Is this correct?
Thanks in advance!
Regards,
Aditya.
Re:Libs
Posted: Tue Apr 08, 2003 1:50 am
by Euler
Hi Tim,
Once i've got the .a file compiled, i only need to link it with my executable? ( as elf link or whatever will be the format )
Thanks.
Re:Libs
Posted: Tue Apr 08, 2003 3:55 pm
by _mark
Libraries are just collections of object files (*.o). The format of what is in the library is pretty un-important, but you can look at it under windows with things like DUMPBIN and LIB /L. Under Linux most of these tools will be combined into 'ar', but basically at the heart of it all, just think of it as like a ZIP file (or a closer relative would be a TAR file).
So in anycase you create the library (be it a .a or .lib) with an external utility. Now when you want to compile against that library you just need to tell the compiler it is a library and not a normal object file. This is done with compiler switches and under unix is usually "-l".
So for example I want to create a library with:
strcmp.c and strlen.c
I first compile them to obj files:
cc -c strcmp.c
cc -c strlen.c
Now I create the library (as Tim showed)
ar rcs stdlib.a strcmp.o strlen.o
And to compile the program and link against the library:
cc main.c -l stdlib.a
There are other things to think about when using libraries such as static data, circular dependancies and stuff, but all in all this is really pretty simple and is also pretty basic programming stuff. You might do well by getting some programming books.
_mark()
Re:Libs
Posted: Wed Apr 09, 2003 1:36 am
by Euler
Thanks _mark()
I really want to know how compile and create libs. Now will begin to create my libc for may os, and also try to load gcc from it.
Thanks for all once again.
Re:Libs
Posted: Wed Apr 09, 2003 6:30 am
by Adi
Hey Mark,
Thanks for explaining it !
Regards,
surya4friends