making static libraries and dynamic libraries under Linux
Directory structure:
File contents:
Main.c
#include <stdio.h>
#include ". /include/add.h "
int main ()
{
printf ("%d ", add (1,2));
____________________________________________
//add.c
#include ". /include/add.h '
int Add (int a, int b)
{return
a+b;
}
______________________________________________
//add.h
int Add (int a, int b)
Dynamic Library production (. So)
Gcc-shared-fpic-o lib/libadd.so SRC/ADD.C
or
Gcc-fpic-c src/add.c
Gcc-shared-o libadd.so
using shared libraries
GCC Src/main.c-llib-ladd-o Main
Run an error:
./main:error while loading shared libraries:libadd.so:cannot open Shared object file:no such file or directory
Solution Summary :
1. Add-wl and Rpath
2. Modify Ld_library_path
Export Ld_library_path=~/test/lib: $LD _library_path
3. Use Ldconfig
Put the library on the standard path
CP ~/test/lib/libadd.so/usr/lib
chmod 0775/usr/lib/libadd.so
/update cache
ldconfig
gcc Src/main.c-llib-ladd-o Main
-fpic: Compiling a dynamic library must, output the code that does not depend on the location
-shared: Compile dynamic library must options
-wi: Pass some parameters to the linker static library production
Gcc-c src/add.c-o ADD.O //Make obj file
ar-crv lib/libadd.a add.o //Archive file
gcc src/main.c-llib-ladd-o ma In //plus L (indicates link directory) L (indicates link library name)