How to generate a shared object: (Dynamically linked object library file.) Note that this is a two step process.
1. Create object code
2. Create library
3. Optional: create default version using a symbolic link.
gcc -Wall -L/home/hanyh/workspace/cplusplus/linux prog.c -lctest -o prog
export LD_LIBRARY_PATH=/home/hanyh/workspace/cplusplus/linux/so:$LD_LIBRARY_PATH
Library creation example:
gcc -Wall -fPIC -c *.c
gcc -shared -Wl,-soname,libctest.so.1 -o libctest.so.1.0 *.o
mv libctest.so.1.0 /opt/lib
ln -sf /opt/lib/libctest.so.1.0 /opt/lib/libctest.so
ln -sf /opt/lib/libctest.so.1.0 /opt/lib/libctest.so.1
This creates the library libctest.so.1.0 and symbolic links to it.
Compiler options:
* -Wall: include warnings. See man page for warnings specified.
* -fPIC: Compiler directive to output position independent code, a characteristic required by shared libraries. Also see "-fpic".
* -shared: Produce a shared object which can then be linked with other objects to form an executable.
* -W1: Pass options to linker.
In this example the options to be passed on to the linker are: "-soname libctest.so.1". The name passed with the "-o" option is passed to gcc.
* Option -o: Output of operation. In this case the name of the shared object to be output will be "libctest.so.1.0"
Library Links:
* The link to /opt/lib/libctest.so allows the naming convention for the compile flag -lctest to work.
* The link to /opt/lib/libctest.so.1 allows the run time binding to work. See dependency below.
Compile main program and link with shared object library:
Compiling for runtime linking with a dynamically linked libctest.so.1.0:
gcc -Wall -I/path/to/include-files -L/path/to/libraries prog.c -lctest -o prog
Use:
gcc -Wall -L/opt/lib prog.c -lctest -o prog
Where the name of the library is libctest.so. (This is why you must create the symbolic links or you will get the error "/usr/bin/ld: cannot find -lctest".)
The libraries will NOT be included in the executable but will be dynamically linked during runtime execution.
List Dependencies:
The shared library dependencies of the executable can be listed with the command: ldd name-of-executable
Example: ldd prog
libctest.so.1 => /opt/lib/libctest.so.1 (0x00002aaaaaaac000)
libc.so.6 => /lib64/tls/libc.so.6 (0x0000003aa4e00000)
/lib64/ld-linux-x86-64.so.2 (0x0000003aa4c00000)
Run Program:
* Set path: export LD_LIBRARY_PATH=/opt/lib:$LD_LIBRARY_PATH
* Run: prog
Prototype include file for the library: ctest.h
#ifndef CTEST_H
#define CTEST_H
#ifdef __cplusplus
extern "C" {
#endif
void ctest1(int *);
void ctest2(int *);
#ifdef __cplusplus
}
#endif
#endif
Use the notation extern "C" so the libraries can be used with C and C++. This statement prevents the C++ from name mangling and thus creating "unresolved symbols" when linking.
nm - list symbols from object files
1. Create object code
2. Create library
3. Optional: create default version using a symbolic link.
gcc -Wall -L/home/hanyh/workspace/cplusplus/linux prog.c -lctest -o prog
export LD_LIBRARY_PATH=/home/hanyh/workspace/cplusplus/linux/so:$LD_LIBRARY_PATH
Library creation example:
gcc -Wall -fPIC -c *.c
gcc -shared -Wl,-soname,libctest.so.1 -o libctest.so.1.0 *.o
mv libctest.so.1.0 /opt/lib
ln -sf /opt/lib/libctest.so.1.0 /opt/lib/libctest.so
ln -sf /opt/lib/libctest.so.1.0 /opt/lib/libctest.so.1
This creates the library libctest.so.1.0 and symbolic links to it.
Compiler options:
* -Wall: include warnings. See man page for warnings specified.
* -fPIC: Compiler directive to output position independent code, a characteristic required by shared libraries. Also see "-fpic".
* -shared: Produce a shared object which can then be linked with other objects to form an executable.
* -W1: Pass options to linker.
In this example the options to be passed on to the linker are: "-soname libctest.so.1". The name passed with the "-o" option is passed to gcc.
* Option -o: Output of operation. In this case the name of the shared object to be output will be "libctest.so.1.0"
Library Links:
* The link to /opt/lib/libctest.so allows the naming convention for the compile flag -lctest to work.
* The link to /opt/lib/libctest.so.1 allows the run time binding to work. See dependency below.
Compile main program and link with shared object library:
Compiling for runtime linking with a dynamically linked libctest.so.1.0:
gcc -Wall -I/path/to/include-files -L/path/to/libraries prog.c -lctest -o prog
Use:
gcc -Wall -L/opt/lib prog.c -lctest -o prog
Where the name of the library is libctest.so. (This is why you must create the symbolic links or you will get the error "/usr/bin/ld: cannot find -lctest".)
The libraries will NOT be included in the executable but will be dynamically linked during runtime execution.
List Dependencies:
The shared library dependencies of the executable can be listed with the command: ldd name-of-executable
Example: ldd prog
libctest.so.1 => /opt/lib/libctest.so.1 (0x00002aaaaaaac000)
libc.so.6 => /lib64/tls/libc.so.6 (0x0000003aa4e00000)
/lib64/ld-linux-x86-64.so.2 (0x0000003aa4c00000)
Run Program:
* Set path: export LD_LIBRARY_PATH=/opt/lib:$LD_LIBRARY_PATH
* Run: prog
Prototype include file for the library: ctest.h
#ifndef CTEST_H
#define CTEST_H
#ifdef __cplusplus
extern "C" {
#endif
void ctest1(int *);
void ctest2(int *);
#ifdef __cplusplus
}
#endif
#endif
Use the notation extern "C" so the libraries can be used with C and C++. This statement prevents the C++ from name mangling and thus creating "unresolved symbols" when linking.
nm - list symbols from object files