Full Version: Creating shared libraries
Dream.In.Code > Programming Tutorials > C++ Tutorials
GWatt
This tutorial covers the basics of compiling a shared library on UNIX like systems using The GNU C Compiler. The server is blocking "g c c" so I am using the cc command instead. cc is simply a smbolic link for the other one.
Having shared libraries allows you to create programs that can break down functions into several different binary files. If there were no shared libraries all the programs would be a single, massive binary file. Updating programs would be pain because the entire project would have to be recompiled.
Also, shared libraries allow programmers to reuse existing binary files so that standard functions don't have to be recompiled again and again. Imagine having to wait for the stdio or stdlib to compile every single time you had a simple program.

Alright, now for the meat of the tutorial.
Create a simple function in a file. I'm going create an add function.
CODE
#include "add.h"

int add(int a, int b)
{
    return a + b;
}


Now, make a header file called add.h and put this line in it:
CODE

int add(int,int);


You should create another file to use the add function. I'm going to call it testadd.c
CODE
#include <stdio.h>
#include "add.h"

int main(int argc, char** argv)
{
    int sum = add(3, 6);
    printf("3 + 6 = %d\n", sum);
    return 0;
}


The compilation process is slightly platform dependent. Linux and Mac have a different way of doing this, and I can't speak for Windows, which is why this is for UNIX like systems
Linux:
cc -o libadd.so add.c -shared
Mac:
cc -o libadd.dylib add.c -dynamiclib

To compile the driver code, type:
cc -o add testadd.c -ladd -L.
The “-l” option tells the compiler to look for a file called libsomething.ext The something is specified by the argument immediately following the “-l”. The extension depends on your system. On linux, it looks for a file ending in “.so” but on Mac it looks for a file ending in “.dylib”. The “-L” option tells the compiler where to find the library. If no “-L” is specified, the compiler will search the usual locations. You can specify as many “-l” and “-L” options as you like. In this case I only needed one of each. The compiler will look for a file called “libadd.so” or “libadd.dylib” in the current directory.
You can see what libraries programs need by typing
ldd add
This does not work on Mac, because ldd does not exist.

This is the output on my ldd command:
QUOTE
linux-gate.so.1 => (0xffffe000)
libadd.so => not found
libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0xb7e02000)
/lib/ld-linux.so.2 (0xb7f5f000)


Here you can see that the linker didn't find libadd.so. You have two choices. You can move the libadd.so to your "/usr/lib" directory, or you can set "LD_LIBRARY_PATH" to be the current directory.
export LD_LIBRARY_PATH=.
Then recompile the binary with:
cc -o add add.c -ladd
You don't need the "-L" option because the linker searches the directories in LD_LIBRARY_PATH.
QUOTE
linux-gate.so.1 => (0xffffe000)
libadd.so => ./libadd.so (0xb7f4b000)
libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0xb7df0000)
/lib/ld-linux.so.2 (0xb7f4f000)

OR if you put the library in your /usr/lib directory, the command would be:
cc -o add add.c -ladd
You don't need the "-L" option because "/usr/lib" is automatically searched by the linker.
Then, the ouptut of ldd add
is
QUOTE
linux-gate.so.1 => (0xffffe000)
libadd.so => /usr/lib/libadd.so (0xb7f75000)
libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0xb7e2b000)
/lib/ld-linux.so.2 (0xb7f8a000)


You should know that there are two types of shared libraries. There are dynamic and static libraries. A separate instance of a static library is loaded into memory every time a program needs it. A dynamic library is loaded into memory just once no matter how many programs need it. In linux, you must use the “-fPIC” option to compile dynamic libraries. The “PIC” stands for position independent code. That means that no matter where the library is in memory, every program that needs it can access it. In mac, the “-dynamiclib” option takes care of that for you.

I hope that helps people who are wondering how to compile shared libraries, and I'm sorry about not being able to work in how to do it under Windows, but I don't have access to a Windows computer, so I can't test everything to make sure it's working properly.

nitin_c_c++
good tutorial.

Congrats
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.