Gcc

gcc
http://en.wikipedia.org/wiki/GNU_Compiler_Collection GNU Compiler Collection - Wikipedia]
 * "The GNU Compiler Collection (usually shortened to GCC) is a compiler system produced by the GNU Project supporting various programming languages. GCC is a key component of the GNU toolchain. As well as being the official compiler of the GNU system, GCC has been adopted as the standard compiler by most other modern Unix-like computer operating systems, including GNU/Linux, the BSD family and Mac OS X. GCC has been ported to a wide variety of processor architectures, and is widely deployed as a tool in commercial, proprietary and closed source software development environments. GCC is also available for most embedded platforms, for example Symbian, AMCC and Freescale Power Architecture-based chips. The compiler can target a wide variety of platforms, including videogame consoles such as the Playstation 2 and Sega Dreamcast. Several companies make a business out of supplying and supporting gcc ports to various platforms, and chip manufacturers today consider a gcc port almost essential to the success of an architecture.


 * Originally named the GNU C Compiler, because it only handled the C programming language, GCC 1.0 was released in 1987, and the compiler was extended to compile C++ in December of that year. Front ends were later developed for Fortran, Pascal, Objective C, Java, and Ada, among others.


 * The Free Software Foundation (FSF) distributes GCC under the GNU General Public License (GNU GPL) and the GNU Lesser General Public License (GNU LGPL). GCC is free software."

GCC, the GNU Compiler Collection - GNU Project - Free Software Foundation (FSF)

Hello World
hello.c:
 * 1) include 

int main { printf("Hello World\n"); return 0; }

Compile: gcc hello.c -o hello ./hello

Library
mylib.c:
 * 1) include 

void toast(char * msg) { printf("Message: %s\n", msg); }

mylib.h: void toast(char * msg);

Compile shared library: gcc -c -fpic mylib.c  # output: mylib.o, can also explicitly use '-o mylib.o' gcc -fPIC -Wall -g -c mylib.c  # PIC for larger libraries gcc -shared -o libmylib.so mylib.o # or ld -shared -soname libmylib.so.1 -o libmylib.so.1.0 mylib.o

Or you can specify Soname: (otherwise none will be included) gcc -shared -Wl,-soname,libmylib.so.1 -o libmylib.so.1.0.1 mylib.o objdump -p libmylib.so.1.0.1 | grep SONAME SONAME     libmylib.so.1
 * 1) find with:

Note: For the record, pic stands for "position-independent code", an object-code format required for shared libraries. You might need to use the option -fPIC instead of -fpic if your library is very large.

Test: mkdir -p test test/lib test/include cp mylib.h test/include cp libmylib.so test/lib cd test

test/test.c:
 * 1) include 

int main { toast("This is a test"); }

Compile: gcc -I./include -L./lib -lmylib test.c -o test

Note: If you try running as is, you will get this error:

./test: error while loading shared libraries: libmylib.so: cannot open shared object file: No such file or directory
 * 1) ./test

Test: LD_LIBRARY_PATH=lib ./test

To install in the system copy to /usr/lib and run ldconfig (not sure ldconfig is required, maybe creates the top .so.1 link?) cp libmylib.so.1.0 /usr/lib ldconfig -n /usr/lib/ ln -sf /usr/lib/libmylib.so.1 /usr/lib/libmylib.so

For static library: ar rcs libmylib.a mylib.o gcc -static main.c -L. -lmylib -o static_linked_app

Good References:
 * Creating a shared and static library with the gnu compiler [gcc] - http://www.adp-gmbh.ch/cpp/gcc/create_lib.html
 * The GNU C Programming Tutorial - http://crasseux.com/books/ctutorial/Building-a-library.html
 * Dissecting shared libraries - http://www.ibm.com/developerworks/linux/library/l-shlibs/index.html

Shared Libraries
Also known as Dynamic Libraries, as they are dynamically loaded when an application is executed.

-

Shared Libraries - http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html
 * Shared libraries are libraries that are loaded by programs when they start. When a shared library is installed properly, all programs that start afterwards automatically use the new shared library. It's actually much more flexible and sophisticated than this, because the approach used by Linux permits you to:


 * update libraries and still support programs that want to use older, non-backward-compatible versions of those libraries;
 * override specific libraries or even specific functions in a library when executing a particular program.
 * do all this while programs are running using existing libraries.

-

The Linux GCC HOWTO: Linking - http://netwinder.osuosl.org/pub/netwinder/docs/misc/GCC-HOWTO-html/GCC-HOWTO-6.html
 * Between the two incompatible binary formats, the static vs shared library distinction, and the overloading of the verb `link' to mean both `what happens after compilation' and `what happens when a compiled program is invoked' (and, actually, the overloading of the word `load' in a comparable but opposite sense), this section is complicated. Little of it is much more complicated than that sentence, though, so don't worry too much about it.
 * To alleviate the confusion somewhat, we refer to what happens at runtime as `dynamic loading' and cover it in the next section. You will also see it described as `dynamic linking', but not here. This section, then, is exclusively concerned with the kind of linking that happens at the end of a compilation.

-

Short Course on Linux Libraries - http://jrv.oddones.org/lib.html

nm - can list the symbols defined in a library

-

segfault.in » How to create shared libraries with gcc? - http://segfault.in/2008/05/how-to-create-shared-libraries-with-gcc/
 * Shared libraries are libraries that are loaded by programs when they start. All programs that start afterwards automatically use the new shared library.

-

Library - LQWiki - http://wiki.linuxquestions.org/wiki/Library
 * If you are a programmer, you will want to re-use code written by others and design your program to be modular. You build up libraries containing re-usable code

Dissecting shared libraries
Summary: Shared libraries use version numbers to allow for upgrades to the libraries used by applications while preserving compatibility for older applications. This article reviews what's really going on under the book jacket and why there are so many symbolic links in /usr/lib on a normal Linux™ system.

Compatibility's not just for relationships

This means that the library you end up being linked to had better be compatible with the code that's calling it. With a statically linked executable, there is some guarantee that nothing will change on you. With dynamic linking, you don't have that guarantee.

What happens if a new version of the library comes out? Especially, what happens if the new version changes the calling sequence for a given function?

Version numbers to the rescue -- a shared library will have a version. When a program is linked against a library, it has the version number it's designed for stored in it. The dynamic linker can check for a matching version number. If the library has changed, the version number won't match, and the program won't be linked to the newer version of library.

One of the potential advantages of dynamic linking, however, is in fixing bugs. It'd be nice if you could fix a bug in the library and not have to recompile a thousand programs to take advantage of that fix. So sometimes, you want to link to a newer version.

Unfortunately, that creates some cases where you want to link to the newer version and some cases where you'd rather stick with an older version. There is a solution, though -- two kinds of version numbers:


 * A major number indicates a potential incompatibility between library versions.
 * A minor number indicates only bug fixes.

So under most circumstances, it is safe to load a library with the same major number and a higher minor number; consider it an unsafe practice to load a library with a higher major number.

To prevent users (and programmers) from needing to track library numbers and updates, the system comes with a large number of symbolic links. In general, the pattern is that

libexample.so

will be a link to

libexample.so.N

in which N is the highest major version number found on the system.

For every major version number supported,

libexample.so.N

will be a link in turn to

libexample.so.N.M

in which M is the largest minor version number.

Thus, if you specify -lexample to the linker, it looks for libexample.so which is a symbolic link to a symbolic link to the most recent version. On the other hand, when an existing program is loaded, it will try to load libexample.so.N in which N is the version to which it was originally linked. Everyone wins!

reference:
 * Dissecting shared libraries - http://www.ibm.com/developerworks/linux/library/l-shlibs/index.html