????Linux???????????????????? libxxx.so??????so?? Shared Object ????д????????????????????
????????????????????????????????????????????????????????У??????????????м????????????á?
??????????????????????????????????????????????????????????????????????
????Linux?????????????????????£?
??????д??????
??????????????????????????????????
??????? -L<path> -lxxx ??gcc????????????libxxx.so??
??????libxxx.so????????????·????????? LD_LIBRARY_PATH????????????????libxxx.so?????
?????????????????????
??????д????
????????????????? max.c?????????£?
????int max(int n1?? int n2?? int n3)
????{
????int max_num = n1;
????max_num = max_num < n2? n2: max_num;
????max_num = max_num < n3? n3: max_num;
????return max_num;
????}
????????????????
????gcc -fPIC -shared -o libmax.so max.c
???????????libmax.so??
?????????????????????????????????? -fPIC????????PIC?? Position Independent Code ????д??????????λ??????????????????????????? -shared?????????????gcc??????????????????????
??????????????????????
????gcc -c -fPIC max.c
????gcc -shared -o libmax.so max.o
???????????д??????
??????????????????????????????Щ????????????????д???????????
???????? max.h ?????????′???
????#ifndef __MAX_H__
????#define __MAX_H__
????int max(int n1?? int n2?? int n3);
????#endif
?????????????????????????????
??????????????max??????test.c?????????£?
????#include <stdio.h>
????#include "max.h"
????int main(int argc?? char *argv[])
????{
????int a = 10?? b = -2?? c = 100;
????printf("max among 10?? -2 and 100 is %d. "?? max(a?? b?? c));
????return 0;
????}
????gcc test.c -L. -lmax ????a.out??????-lmax????????libmax.so??
????-L.?????????????????????????·????
???????????????????????????????????????? libmax.so ?? libmax.a ??????·???£?
??????gcc??????????????
????????
???????? ./a.out ???????μ?????????
????./a.out: error while loading shared libraries: libmax.so: cannot open shared object file: No such file or directory
?????????libmax.so?????Linux????? /etc/ld.so.cache ??????????????????
?????? /etc/ld.so.cache ?? ldconfig ?????? /etc/ld.so.conf ?????????
????????? /etc/ld.so.conf ?в???????? /lib ?? /usr/lib??ldconfig??????????????????????
???????????? libmax.so ?????·?????? /etc/ld.so.conf ?У?????root??????? ldconfig ???????? /etc/ld.so.cache ??a.out?????????????? libmax.so??
????????????????????????????????????????????????????
????????????????????????a.out??? LD_LIBRARY_PATH??
????LD_LIBRARY_PATH=. ./a.out
?????????????????????LD_LIBRARY_PATH=. ????? a.out????????·??????????????
????????elf????????г???????ld-linux.so*????????????????elf????? DT_RPATH ?Σ? ???????? LD_LIBRARY_PATH?? /etc/ld.so.cache????б?? /lib/??/usr/lib???? ???????????????????. (http://blog.chinaunix.net/uid-23592843-id-223539.html)
????makefile?ù????????
??????дmakefile?????????£?
????.PHONY: build test clean
????build: libmax.so
????libmax.so: max.o
????gcc -o $@  -shared $<
????max.o: max.c
????gcc -c -fPIC $<
????test: a.out
????a.out: test.c libmax.so
????gcc test.c -L. -lmax
????LD_LIBRARY_PATH=. ./a.out
????clean:
????rm -f *.o *.so a.out
????make build??????libmax.so?? make test??????a.out????У?make clean?????????????????