954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C libraries

Please help me for creating C libraries

sagarnarasgonde
Newbie Poster
1 post since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

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

L7Sqr
Practically a Master Poster
657 posts since Feb 2011
Reputation Points: 201
Solved Threads: 124
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Write function.
Compile it.

You now have a library.

Moschops
Practically a Master Poster
620 posts since Sep 2008
Reputation Points: 258
Solved Threads: 117
 

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.

Moschops
Practically a Master Poster
620 posts since Sep 2008
Reputation Points: 258
Solved Threads: 117
 

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'ttechnically a library even though it can be compiled into an executable in certain contexts.

L7Sqr
Practically a Master Poster
657 posts since Feb 2011
Reputation Points: 201
Solved Threads: 124
 

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
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

@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?

L7Sqr
Practically a Master Poster
657 posts since Feb 2011
Reputation Points: 201
Solved Threads: 124
 
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 linksthem.

nezachem
Posting Shark
903 posts since Dec 2009
Reputation Points: 719
Solved Threads: 194
 
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.cand 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.

Moschops
Practically a Master Poster
620 posts since Sep 2008
Reputation Points: 258
Solved Threads: 117
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You