Libs

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Euler

Libs

Post 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.
Tim

Re:Libs

Post 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
surya4friends

Re:Libs

Post 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.
Euler

Re:Libs

Post 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.
_mark

Re:Libs

Post 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()
Euler

Re:Libs

Post 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.
Adi

Re:Libs

Post by Adi »

Hey Mark,

Thanks for explaining it !

Regards,
surya4friends
Post Reply