Please help me for creating C libraries

Have you tried google? What progress have you made so far?

How to do that all depends on the compiler and/or IDE you are using. Basically, a library is nothing more than a collection of compiled functions that are in a file with *.lib or *.a file name. *nix computers also have something called shared libraries, which has a *.so file name.

Write function.
Compile it.

You now have a library.

commented: wanna bet? -4

WaltP has challenged me on this, with his reputation comment.

I stand by my original line. I can take a C source file, compile it into an object, and I'm done - that object file is then a library. A standard *.a file is no more than a bunch of these object files stuck together; a single object file is perfectly usable as a library.

commented: I agree +17

So there is some interpretation there. Sure, an object file can be compiled into an executable but it cant be 'linked' to in the traditional sense without converting it to a .a archive. (I'm talking purely Linux here as I am less familiar with how Windows handles this)

So an object file isn't technically a library even though it can be compiled into an executable in certain contexts.

Of course object files *.o or *.obj can be linked just the same as *.a and *.lib files, every programmer does it all the time.

@Ancient Dragon: I'm curious how you do this.

On Linux I get the following:

# Compiling object file directly into binary
$ gcc -c t1.c
$ gcc t2.c t1.o

# Trying to link to .o file
$ gcc -c t1.c -L. -lt1.o
/usr/bin/ld: cannot find -lt1.o
collect2: ld returned 1 exit status

$ cp t1.o libt1.o
$ gcc -c t1.c -L. -lt1
/usr/bin/ld: cannot find -lt1
collect2: ld returned 1 exit status

# Create the static library
$ ar -cvq libt1.a t1.o
$ gcc -c t1.c -L. -lt1

Is there another way I am not considering?

Is there another way I am not considering?

> gcc -c foo.c
> gcc -c bar.c
> gcc -o baz foo.o bar.o
>

Of course an object file can be linked. In fact, only the object files are linked.

A library is a convenient way to package a collection of objects. They are not linked directly. The linker extracts required objects from the library and links them.

Is there another way I am not considering?

The gcc call does an awful lot for you when you ask it to take care of the linking. Here's the longhand linker call:

gcc -c func.c
gcc -c main.c

and now calling the linker...
/usr/bin/ld --build-id --eh-frame-hdr -m elf_x86_64 --hash-style=both -dynamic-linker /lib64/ld-linux-x86-64.so.2 -z relro /usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib/crt1.o /usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.4.3/crtbegin.o -L/usr/lib/gcc/x86_64-linux-gnu/4.4.3 -L/usr/lib/gcc/x86_64-linux-gnu/4.4.3 -L/usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib -L/lib/../lib -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../.. -L/usr/lib/x86_64-linux-gnu -v main.o func.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-linux-gnu/4.4.3/crtend.o /usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib/crtn.o


The -l option for the linker tells the linker that the file specified is an ar format.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.