1.1.1 LinuxWrite SoHow files are
1 first compile to add -fpic -fpic is to tell GCC generate a location-independent code
2GCC link to add -shared option, meaning to generate a shared library.
for linux or unix , one so file, file extension must be
1.1.2 LinuxUse So
GCCwhen linking, you need to add-L.representative from the current directory to find the relevant Sofiles,- Lfile name (but not including the file name at the beginning ofLiband extension So)
For example, compiling a MAIN.O file to be used in the current directory. liba.so Library
Gcc–o Main.out–l.–la MAIN.O
1.1.3 Configuration Profilefiles can be found in the current directory Sofile
Linux not in the current directory to find executable programs, but also not early in the current directory to find So Library Files
How to modify a User configuration file
1
Cd
2
VI. bash_profile
3
Export Ld_library_path = $LD _library_path:.
4
Save exit
5
. . bash_profile
[Email protected]:~/link/c$ cat mymax.c int max (Int a,int b) {if (a>b) Return a;elsereturn b;} [email protected]:~/link/c$ cat mymax.h #ifndef mymax_c_#define mymax_c_int max (int a,int b); #endif [email protected]:~/link/c$ cat main.c # include <stdio.h> #include "mymax.h" Int main () {printf ("max =%d \n", Max (1,4)); return 0;} [EMAIL&NBSP;PROTECTED]:~/LINK/C$&NBSP;[EMAIL&NBSP;PROTECTED]:~/LINK/C$&NBSP;GCC&NBSP;-C&NBSP;-O&NBSP;MYMAX.O -fPIC mymax.c tells the compiler to generate location-independent code [email protected]:~/link/c$ gcc - shared -o libmymax.so mymax.o mymax.o generate so shared library, must start with LIB, the extension must be .so[email protected]:~/link/c$ gcc -c main.c -o main.o[email protected]:~/link/c$ gcc -o main.out main.o -L. -lmymax[email protected]:~/link/c$ ./main.out max =4echo ' export ld_library_path= $LD _library_path:. ' >> /home/chunli/.bashrc[email protected]:~/link/c$ . /home/chunli/.bashrc
Copy to another directory:
Still running:
[Email protected]:~/link/c$ cp libmymax.so Main.out/tmp/[email protected]:/tmp$./main.out Max =4
This article is from the "Soul Bucket" blog, please be sure to keep this source http://990487026.blog.51cto.com/10133282/1771934
Linux C dynamic shared library compilation link