Linux?????????????????
???????????? ???????[ 2015/3/12 13:29:10 ] ????????Linux ?????? ???? ???? ????
????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?????????????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11