ºìÁªLinuxÃÅ»§
Linux°ïÖú

LINUX¶¯Ì¬Á´½Ó¿â¸ß¼¶Ó¦ÓÃ

·¢²¼Ê±¼ä:2006-02-21 13:12:51À´Ô´:ºìÁª×÷Õß:ccs
ÔÚ¡¶LINUX϶¯Ì¬Á´½Ó¿âµÄ´´½¨ÓëÓ¦Óá·Ò»ÎÄÖÐ,ÎÒ½éÉÜÁËLINUX¶¯Ì¬Á´½Ó¿âµÄ»ù±¾ÖªÊ¶.ÆäÒªµãÊÇ:Óû§¸ù¾Ýʵ¼ÊÇé¿öÐèÒª,ÀûÓÃdlopen,dlsym,dlcloseµÈ¶¯Ì¬Á´½Ó¿â²Ù×÷º¯Êý,×°ÈëÖ¸¶¨µÄ¶¯Ì¬Á´½Ó¿âÖÐÖ¸¶¨µÄº¯Êý,È»ºó¼ÓÒÔÖ´ÐÐ.³ÌÐòÖÐʹÓúÜÉٵĶ¯Ì¬º¯Êýʱ,ÕâÑùµÄ×ö·¨ÉпÉ.Èç¹û³ÌÐòÐèÒªµ÷ÓôóÁ¿µÄ¶¯Ì¬º¯Êý,ÄÇô²ÉÓÃÕâÑùµÄ±à³ÌÊֶν«ÊǷdz£·±¸´µÄ,ËùÒÔÎÒÃDZØÐëʹÓÃÒ»ÖÖ¸üΪ´ÏÃ÷µÄ°ì·¨,ÒÔ¼õÉÙ´úÂëÁ¿,Ìá¸ß¹¤×÷ЧÂÊ.Õâ¾ÍÊÇÏÖÔÚÎÒÒª¾ÙÀý½éÉܵġ¶LINUX¶¯Ì¬Á´½Ó¿â¸ß¼¶Ó¦Óá·.

×¢:±¾ÎľٵÄÀý×ÓÀàËÆÉÏƪÎÄÕÂ,Ö»ÊÇÎļþµÄÄÚÈÝÒÑ×öÏàÓ¦ÐÞ¸Ä,²Ã¼õÁ˲»ÉÙ.ʾÀý³ÌÐòady.cºÍÁ½¸ö¶¯Ì¬º¯ÊýµÄÔ´³ÌÐògetdate.cÓëgettime.c½öÐÞ¸ÄÁËÍ·ÎļþµÄÃû×Ö,ÆäÄÚÈݲ»ÔÙÁгö.±¾ÎÄʹÓÃÍ·ÎļþΪadatetime.h.


ÒªÏë¸ßЧµØÓ¦ÓÃLINUX¶¯Ì¬Á´½Ó¿â(ÓÈÆäÊÇÓû§×Ô¼º±àдµÄ),ÐèÒª×öÒÔϹ¤×÷:

Ò»¡¢±àдºÏ¸ñµÄ¶¯Ì¬Á´½Ó¿âÍ·Îļþ

CÓïÑÔµÄÍ·Îļþ,¿É¹©Ò»¸ö»ò¶à¸ö³ÌÐòÒýÓÃ,ÀïÃæÒ»°ã¶¨Òå³ÌÐòËùÐèµÄ³£Á¿,×Ô¶¨ÒåÀàÐͼ°º¯ÊýÔ­ÐÍ˵Ã÷µÈ.ÆäÖеĺ¯ÊýÔ­ÐÍ˵Ã÷,Ôò¹©±àÒëÆ÷¼ì²éÓï·¨,ÓÃÓÚÅųýÒýÓòÎÊýʱÀàÐͲ»Ò»ÖµĴíÎó.Ö»ÓбàдºÏ¸ñµÄ¶¯Ì¬Á´½Ó¿âÍ·Îļþ,³ÌÐòÔ±²ÅÄÜÕýȷʹÓö¯Ì¬Á´½Ó¿âÄڵĺ¯Êý.


¶¯Ì¬Á´½Ó¿âÍ·ÎļþÒª²ÉÓÃCÓïÑÔ±ê×¼¸ñʽ,ÆäÖеĶ¯Ì¬º¯ÊýÔ­ÐͶ¨Òå,²»±ØÏóÉÏÎĽéÉܵÄÄÇÑùÓÃ(*¶¯Ì¬º¯ÊýÃû)µÄÃèÊöÐÎʽ.Çë¿´ÏÂÃæµÄÀý×Ó:(ÿÐпªÊ¼µÄÊý×ÖΪËùÔÚÐÐÐкÅ,Ϊ±ÊÕßÌí¼Ó,¹©×¢½âʹÓÃ)


1 /* adatetime.h : ×ݺáÈí¼þÖÆ×÷ÖÐÐÄÓêÒàÆæ(zhsoft@371.net)±àд,
2
3 #ifndef __DATETIME_H
4
5 #define __DATETIME_H
6
7 /* ÈÕÆڽṹ */
8 typedef struct
9 {
10 int year;
11 int mon;
12 int day;
13 }DATETYPE;
14
15 /* ʱ¼ä½á¹¹ */
16 typedef struct
17 {
18 char hour;
19 char min;
20 char sec;
21 }TIMETYPE;
22
23 int getdate(DATETYPE *d); /* È¡µ±Ç°ÈÕÆÚ */
24 int gettime(TIMETYPE *t); /* È¡µ±Ç°Ê±¼ä */
25
26 #endif
27


×¢:ÓëÉÏÎĵÄdatetime.hÎļþ±È½Ï,´Ó¸ÃÍ·ÎļþµÚ23,24ÐпÉÒÔ¿´µ½,¶¯Ì¬º¯Êýgetdate,gettimeµÄÔ­ÐͶ¨Òå¸Ä±äÁË,²»ÔÙʹÓÃ(*getdate),(*gettime)µÄ¸ñʽÁË(ÕâÖÖ¸ñʽʹÓýÏΪÂÞàÂ).




¶þ¡¢ÕýÈ·±àÒëÓëÃüÃû¶¯Ì¬Á´½Ó¿â




ΪÁËÈÃGCC±àÒëÆ÷Éú³É¶¯Ì¬Á´½Ó¿â,±àÒëʱÐë¼ÓÑ¡Ïî-shared.(ÕâµãÐëÀμÇ)


LINUXϵͳÖÐ,ΪÁËÈö¯Ì¬Á´½Ó¿âÄܱ»ÏµÍ³ÖÐÆäËü³ÌÐò¹²Ïí,ÆäÃû×ÖÓ¦·ûºÏ¡°lib*.so*¡±ÕâÖÖ¸ñʽ.Èç¹ûij¸ö¶¯Ì¬Á´½Ó¿â²»·ûºÏ´Ë¸ñʽ,ÔòLINUXµÄ¶¯Ì¬Á´½Ó¿â×Ô¶¯×°Èë³ÌÐò(ld.so)½«ËÑË÷²»µ½´ËÁ´½Ó¿â,ÆäËü³ÌÐòÒ²ÎÞ·¨¹²ÏíÖ®.


¸ñʽÖÐ,µÚÒ»¸ö*ͨ³£±íʾΪ¼òдµÄ¿âÃû,µÚ¶þ¸ö*ͨ³£±íʾΪ¸Ã¿âµÄ°æ±¾ºÅ.Èç:ÔÚÎÒµÄϵͳÖÐ,»ù±¾C¶¯Ì¬Á´½Ó¿âµÄÃû×ÖΪlibc.so.6,Ïß³Ì pthread¶¯Ì¬Á´½Ó¿âµÄÃû×ÖΪlibpthread.so.0µÈµÈ.±¾ÎÄÀý×ÓËùÉú³ÉµÄ¶¯Ì¬Á´½Ó¿âµÄÃû×ÖΪlibmy.so,ËäûÓа汾ºÅ,µ«Ò²·ûºÏËùÒªÇóµÄ¸ñʽ.


Éú³É¸Ã¶¯Ì¬Á´½Ó¿âµÄά»¤Îļþmakefile-libÄÚÈÝÈçÏÂ:


1 # makefile : ×ݺáÈí¼þÖÆ×÷ÖÐÐÄÓêÒàÆæ±àд, 2002-03-07.
2
3 all : libmy.so
4
5 SRC = getdate.c gettime.c
6
7 TGT = $(SRC:.c=.o)
8
9 $(SRC) : adatetime.h
10 @touch $@
11
12 %.o : %.c
13 cc -c $?
14
15 # ¶¯Ì¬Á´½Ó¿â(libmy.so)Éú³É
16 libmy.so : $(TGT)
17 cc -s -shared -o $@ $(TGT)
18


ÔËÐÐÃüÁî:


$ make -f makefile-lib
$


¼´Éú³Élibmy.so¿â.


×¢: ά»¤ÎļþÖÐ,µÚ17ÐÐÓÃ-sharedÑ¡ÏîÒÔÉú³É¶¯Ì¬Á´½Ó¿â,ÓÃ-sÑ¡ÏîÒÔÈ¥µôÄ¿±êÎļþÖеķûºÅ±í,´Ó¶ø¼õСÎļþ³¤¶È.




Èý¡¢¹²Ïí¶¯Ì¬Á´½Ó¿â




3.1 ¶¯Ì¬Á´½Ó¿âÅäÖÃÎļþ


ΪÁËÈö¯Ì¬Á´½Ó¿âΪϵͳËùʹÓÃ,ÐèҪά»¤¶¯Ì¬Á´½Ó¿âµÄÅäÖÃÎļþ--/etc/ld.so.conf.´ËÎļþÄÚ,´æ·Å×ſɱ»LINUX¹²ÏíµÄ¶¯Ì¬Á´½Ó¿âËùÔÚĿ¼µÄÃû×Ö(ϵͳĿ¼/lib,/usr/lib³ýÍâ),¸÷¸öĿ¼Ãû¼äÒÔ¿Õ°××Ö·û(¿Õ¸ñ,»»ÐеÈ)»òðºÅ»ò¶ººÅ·Ö¸ô.Ò»°ãµÄLINUX·¢ÐаæÖÐ,´ËÎļþ¾ùº¬Ò»¸ö¹²ÏíĿ¼/usr/X11R6/lib,ΪX window´°¿ÚϵͳµÄ¶¯Ì¬Á´½Ó¿âËùÔÚµÄĿ¼.


ÏÂÃæ¿´¿´ÎÒµÄϵͳÖдËÎļþµÄÄÚÈÝÈçºÎ:


# cat /etc/ld.so.conf
/usr/X11R6/lib
/usr/zzz/lib
#


ÓÉÉÏ¿ÉÒÔ¿´³ö,¸Ã¶¯Ì¬¿âÅäÖÃÎļþÖÐ,Ôö¼ÓÁËÒ»¸ö/usr/zzz/libĿ¼.ÕâÊÇÎÒ×Ô¼ºÐ½¨µÄ¹²Ïí¿âĿ¼,ÏÂÃæ´æ·ÅÎÒпª·¢µÄ¿É¹©ÏµÍ³¹²ÏíµÄ¶¯Ì¬Á´½Ó¿â.


3.2 ¶¯Ì¬Á´½Ó¿â¹ÜÀíÃüÁî


ΪÁËÈö¯Ì¬Á´½Ó¿âΪϵͳËù¹²Ïí,»¹ÐèÔËÐж¯Ì¬Á´½Ó¿âµÄ¹ÜÀíÃüÁî--ldconfig.´ËÖ´ÐгÌÐò´æ·ÅÔÚ/sbinĿ¼ÏÂ.


ldconfigÃüÁîµÄÓÃ;,Ö÷ÒªÊÇÔÚĬÈÏËÑѰĿ¼(/libºÍ/usr/lib)ÒÔ¼°¶¯Ì¬¿âÅäÖÃÎļþ/etc/ld.so.confÄÚËùÁеÄĿ¼ÏÂ,ËÑË÷³ö¿É¹²ÏíµÄ¶¯Ì¬Á´½Ó¿â(¸ñʽÈçÇ°½éÉÜ,lib*.so*),½ø¶ø´´½¨³ö¶¯Ì¬×°Èë³ÌÐò(ld.so)ËùÐèµÄÁ¬½ÓºÍ»º´æÎļþ.»º´æÎļþĬÈÏΪ /etc/ld.so.cache,´ËÎļþ±£´æÒÑÅźÃÐòµÄ¶¯Ì¬Á´½Ó¿âÃû×ÖÁбí.


ldconfigͨ³£ÔÚϵͳÆô¶¯Ê±ÔËÐÐ,¶øµ±Óû§°²×°ÁËÒ»¸öеĶ¯Ì¬Á´½Ó¿âʱ,¾ÍÐèÒªÊÖ¹¤ÔËÐÐÕâ¸öÃüÁî.


ldconfigÃüÁîÐÐÓ÷¨ÈçÏÂ:


ldconfig [-v|--verbose] [-n] [-N] [-X] [-f CONF] [-C CACHE] [-r ROOT] [-l] [-p|--print-cache] [-c FORMAT] [--format=FORMAT] [-V] [-?|--help|--usage] path...


ldconfig¿ÉÓõÄÑ¡Ïî˵Ã÷ÈçÏÂ:


(1) -v»ò--verbose : ÓôËÑ¡Ïîʱ,ldconfig½«ÏÔʾÕýÔÚɨÃèµÄĿ¼¼°ËÑË÷µ½µÄ¶¯Ì¬Á´½Ó¿â,»¹ÓÐËüËù´´½¨µÄÁ¬½ÓµÄÃû×Ö.


(2) -n : ÓôËÑ¡Ïîʱ,ldconfig½öɨÃèÃüÁîÐÐÖ¸¶¨µÄĿ¼,²»É¨ÃèĬÈÏĿ¼(/lib,/usr/lib),Ò²²»É¨ÃèÅäÖÃÎļþ/etc/ld.so.confËùÁеÄĿ¼.


(3) -N : ´ËÑ¡Ïîָʾldconfig²»Öؽ¨»º´æÎļþ(/etc/ld.so.cache).ÈôδÓÃ-XÑ¡Ïî,ldconfigÕÕ³£¸üÐÂÎļþµÄÁ¬½Ó.


(4) -X : ´ËÑ¡Ïîָʾldconfig²»¸üÐÂÎļþµÄÁ¬½Ó.ÈôδÓÃ-NÑ¡Ïî,Ôò»º´æÎļþÕý³£¸üÐÂ.


(5) -f CONF : ´ËÑ¡ÏîÖ¸¶¨¶¯Ì¬Á´½Ó¿âµÄÅäÖÃÎļþΪCONF,ϵͳĬÈÏΪ/etc/ld.so.conf.


(6) -C CACHE : ´ËÑ¡ÏîÖ¸¶¨Éú³ÉµÄ»º´æÎļþΪCACHE,ϵͳĬÈϵÄÊÇ/etc/ld.so.cache,´ËÎļþ´æ·ÅÒÑÅźÃÐòµÄ¿É¹²ÏíµÄ¶¯Ì¬Á´½Ó¿âµÄÁбí.


(7) -r ROOT : ´ËÑ¡Ïî¸Ä±äÓ¦ÓóÌÐòµÄ¸ùĿ¼ΪROOT(Êǵ÷ÓÃchrootº¯ÊýʵÏÖµÄ).Ñ¡Ôñ´ËÏîʱ,ϵͳĬÈϵÄÅäÖÃÎļþ/etc/ld.so.conf,ʵ¼Ê¶ÔÓ¦µÄΪ ROOT/etc/ld.so.conf.ÈçÓÃ-r /usr/zzzʱ,´ò¿ªÅäÖÃÎļþ/etc/ld.so.confʱ,ʵ¼Ê´ò¿ªµÄÊÇ/usr/zzz/etc/ld.so.confÎļþ.ÓôËÑ¡Ïî,¿ÉÒÔ´ó´óÔö¼Ó¶¯Ì¬Á´½Ó¿â¹ÜÀíµÄÁé»îÐÔ.


(8) -l : ͨ³£Çé¿öÏÂ,ldconfigËÑË÷¶¯Ì¬Á´½Ó¿âʱ½«×Ô¶¯½¨Á¢¶¯Ì¬Á´½Ó¿âµÄÁ¬½Ó.Ñ¡Ôñ´ËÏîʱ,½«½øÈëר¼Òģʽ,ÐèÒªÊÖ¹¤ÉèÖÃÁ¬½Ó.Ò»°ãÓû§²»ÓôËÏî.


(9) -p»ò--print-cache : ´ËÑ¡Ïîָʾldconfig´òÓ¡³öµ±Ç°»º´æÎļþËù±£´æµÄËùÓй²Ïí¿âµÄÃû×Ö.


(10) -c FORMAT »ò --format=FORMAT : ´ËÑ¡ÏîÓÃÓÚÖ¸¶¨»º´æÎļþËùʹÓõĸñʽ,¹²ÓÐÈýÖÖ:old(Àϸñʽ),new(иñʽ)ºÍcompat(¼æÈݸñʽ,´ËΪĬÈϸñʽ).


(11) -V : ´ËÑ¡Ïî´òÓ¡³öldconfigµÄ°æ±¾ÐÅÏ¢,¶øºóÍ˳ö.


(12) -? »ò --help »ò --usage : ÕâÈý¸öÑ¡Ïî×÷ÓÃÏàͬ,¶¼ÊÇÈÃldconfig´òÓ¡³öÆä°ïÖúÐÅÏ¢,¶øºóÍ˳ö.


¾ÙÈý¸öÀý×Ó:


Àý1:


# ldconfig -p
793 libs found in cache `/etc/ld.so.cache'
libzvt.so.2 (libc6) => /usr/lib/libzvt.so.2
libzvt.so (libc6) => /usr/lib/libzvt.so
libz.so.1.1.3 (libc6) => /usr/lib/libz.so.1.1.3
libz.so.1 (libc6) => /lib/libz.so.1
......
#


×¢: ÓÐʱºòÓû§ÏëÖªµÀϵͳÖÐÓÐÄÄЩ¶¯Ì¬Á´½Ó¿â,»òÕßÏëÖªµÀϵͳÖÐÓÐûÓÐij¸ö¶¯Ì¬Á´½Ó¿â,Õâʱ,¿ÉÓÃ-pÑ¡ÏîÈÃldconfigÊä³ö»º´æÎļþÖеĶ¯Ì¬Á´½Ó¿âÁбí,´Ó¶ø²éѯµÃµ½.Àý×ÓÖÐ,ldconfigÃüÁîµÄÊä³ö½á¹ûµÚ1ÐбíÃ÷ÔÚ»º´æÎļþ/etc/ld.so.cacheÖÐÕÒµ½793¸ö¹²Ïí¿â,µÚ2ÐпªÊ¼±ãÊÇһϵÁй²Ïí¿âµÄÃû×Ö¼°ÆäÈ«Ãû(¾ø¶Ô·¾¶).ÒòΪʵ¼ÊÊä³ö½á¹ûÌ«¶à,Ϊ½Úʡƪ·ù,ÒÔ......±íʾʡÂԵIJ¿·Ö.




Àý2:


# ldconfig -v
/lib:
liby.so.1 -> liby.so.1
libnss_wins.so -> libnss_wins.so
......
/usr/lib:
libjscript.so.2 -> libjscript.so.2.0.0
libkspell.so.2 -> libkspell.so.2.0.0
......
/usr/X11R6/lib:
libmej-0.8.10.so -> libmej-0.8.10.so
libXaw3d.so.7 -> libXaw3d.so.7.0
......
#


×¢: ldconfigÃüÁîÔÚÔËÐÐÕý³£µÄÇé¿öÏÂ,ĬÈϲ»Êä³öʲô¶«Î÷.±¾ÀýÖÐÓÃÁË-vÑ¡Ïî,ÒÔʹldconfigÔÚÔËÐÐʱÊä³öÕýÔÚɨÃèµÄĿ¼¼°ËÑË÷µ½µÄ¹²Ïí¿â, Óû§¿ÉÒÔÇå³þµØ¿´µ½ÔËÐеĽá¹û.Ö´ÐнáÊøºó,ldconfig½«Ë¢Ð»º´æÎļþ/etc/ld.so.cache.


Àý3:


# ldconfig /usr/zhsoft/lib
#


×¢: µ±Óû§ÔÚij¸öĿ¼ÏÂÃæ´´½¨»ò¿½±´ÁËÒ»¸ö¶¯Ì¬Á´½Ó¿â,ÈôÏëʹÆ䱻ϵͳ¹²Ïí,¿ÉÒÔÖ´ÐÐÒ»ÏÂ"ldconfig Ŀ¼Ãû"Õâ¸öÃüÁî.´ËÃüÁîµÄ¹¦ÄÜÔÚÓÚÈÃldconfig½«Ö¸¶¨Ä¿Â¼ÏµĶ¯Ì¬Á´½Ó¿â±»ÏµÍ³¹²ÏíÆðÀ´,Òâ¼´:ÔÚ»º´æÎļþ/etc/ld.so.cacheÖÐ×·¼Ó½øÖ¸¶¨Ä¿Â¼ÏµĹ²Ïí¿â.±¾ÀýÈÃϵͳ¹²ÏíÁË/usr/zhsoft/libĿ¼ÏµĶ¯Ì¬Á´½Ó¿â.ÐèҪ˵Ã÷µÄÊÇ,Èç¹û´ËĿ¼²»ÔÚ/lib,/usr/lib ¼°/etc/ld.so.confÎļþËùÁеÄĿ¼ÀïÃæ,ÔòÔÙ¶ÈÔËÐÐldconfigʱ,´ËĿ¼ÏµĶ¯Ì¬Á´½Ó¿â¿ÉÄܲ»±»ÏµÍ³¹²ÏíÁË.

3.3 ¶¯Ì¬Á´½Ó¿âÈçºÎ¹²Ïí

Á˽âÁËÒÔÉÏ֪ʶ,ÎÒÃÇ¿ÉÒÔ²ÉÓÃÒÔÏÂÈýÖÖ·½·¨À´¹²Ïí¶¯Ì¬Á´½Ó¿â:(×¢:¾ùÐëÔÚ³¬¼¶Óû§×´Ì¬Ï²Ù×÷,ÒÔÎҵĶ¯Ì¬Á´½Ó¿âlibmy.so¹²Ïí¹ý³ÌΪÀý)


(1)¿½±´¶¯Ì¬Á´½Ó¿âµ½ÏµÍ³¹²ÏíĿ¼ÏÂ,»òÔÚϵͳ¹²ÏíĿ¼ÏÂΪ¸Ã¶¯Ì¬Á´½Ó¿â½¨Á¢¸öÁ¬½Ó(Ó²Á¬½Ó»ò·ûºÅÁ¬½Ó¾ù¿É,³£Ó÷ûºÅÁ¬½Ó).ÕâÀï˵µÄϵͳ¹²ÏíĿ¼,Ö¸µÄÊÇLINUX¶¯Ì¬Á´½Ó¿â´æ·ÅµÄĿ¼,Ëü°üº¬/lib,/usr/libÒÔ¼°/etc/ld.so.confÎļþÄÚËùÁеÄһϵÁÐĿ¼.


# cp libmy.so /lib
# ldconfig
#


»ò:


# ln -s `pwd`/libmy.so /lib
# ldconfig
#


(2)½«¶¯Ì¬Á´½Ó¿âËùÔÚĿ¼Ãû×·¼Óµ½¶¯Ì¬Á´½Ó¿âÅäÖÃÎļþ/etc/ld.so.confÖÐ.


# pwd >> /etc/ld.so.conf
# ldconfig
#


(3)ÀûÓö¯Ì¬Á´½Ó¿â¹ÜÀíÃüÁîldconfig,Ç¿ÖÆÆäËÑË÷Ö¸¶¨Ä¿Â¼,²¢¸üлº´æÎļþ,±ãÓÚ¶¯Ì¬×°Èë.


# ldconfig `pwd`
#


ÐèҪ˵Ã÷µÄÊÇ,ÕâÖÖ²Ù×÷·½·¨ËäÈ»ÓÐЧ,µ«Ð§¹ûÊÇÔÝʱµÄ,¹©³ÌÐò²âÊÔ»¹¿ÉÒÔ,Ò»µ©ÔÙ¶ÈÔËÐÐldconfig,Ôò»º´æÎļþÄÚÈÝ¿ÉÄܸıä,ËùÐèµÄ¶¯Ì¬Á´½Ó¿â¿ÉÄܲ»±»ÏµÍ³¹²ÏíÁË.ÓëÖ®Ïà±È½Ï,Ç°Á½ÖÖ·½·¨ÊÇ¿É¿¿µÄ·½·¨,ÖµµÃÒµÒѶ¨Ð͵Ķ¯Ì¬Á´½Ó¿â¹²Ïíʱ²ÉÓÃ.Ç°Á½ÖÖ·½·¨»¹ÓÐÒ»¸öÌصã,¼´×îºóÒ»ÌõÃüÁÊÇ ldconfig,Ò²¼´¾ùÐèÒª¸üÐÂһϻº´æÎļþ,ÒÔÈ·±£¶¯Ì¬Á´½Ó¿âµÄ¹²ÏíÉúЧ.




ËÄ¡¢º¬Óж¯Ì¬º¯ÊýµÄ³ÌÐòµÄ±àÒë




4.1 ·ÀÖ¹±àÒëÒòδָ¶¨¶¯Ì¬Á´½Ó¿â¶ø³ö´í


µ±Ò»¸ö³ÌÐòʹÓö¯Ì¬º¯Êýʱ,±àÒë¸Ã³ÌÐòʱ¾Í±ØÐëÖ¸¶¨º¬ËùÓö¯Ì¬º¯ÊýµÄ¶¯Ì¬Á´½Ó¿â,·ñÔò±àÒ뽫»á³ö´íÍ˳ö.Èç±¾ÎÄʾÀý³ÌÐòady.cµÄ±àÒë(δÃ÷È·ÒýÓö¯Ì¬Á´½Ó¿âlibmy.so):


# cc -o ady ady.c
/tmp/ccL4FsJp.o: In function `main':
/tmp/ccL4FsJp.o(.text+0x43): undefined reference to `gettime'
collect2: ld returned 1 exit status
#


×¢: ÒòΪady.cËùº¬µÄ¶¯Ì¬º¯Êýgetdate,gettime²»ÔÚϵͳº¯Êý¿âÖÐ,ËùÒÔÁ¬½Óʱ³ö´í.


4.2 ±àÒëʱÒýÓö¯Ì¬Á´½Ó¿âµÄ¼¸ÖÖ·½Ê½


(1)µ±ËùÓõĶ¯Ì¬Á´½Ó¿âÔÚϵͳĿ¼(/lib,/usr/lib)ÏÂʱ,¿ÉÓñàÒëÑ¡Ïî-lÀ´ÒýÓÃ.¼´:


# cc -lmy -o ady ady.c
#


×¢:±àÒëʱÓÃ-lÑ¡ÏîÒýÓö¯Ì¬Á´½Ó¿âʱ,¿âÃûÐëʹÓÃÆäËõдÐÎʽ.±¾ÀýµÄmy,±íʾÒýÓÃlibmy.so¿â.ÈôÒýÓùâ±ê¿âlibncurses.so,ÐëÓÃ-lncurses.×¢Òâ,-lÑ¡ÏîÓë²ÎÊýÖ®¼ä²»ÄÜÓпոñ,·ñÔò»á³ö´í.


(2)µ±ËùÓõĶ¯Ì¬Á´½Ó¿âÔÚϵͳĿ¼(/lib,/usr/lib)ÒÔÍâµÄĿ¼ʱ,ÐëÓñàÒëÑ¡Ïî-LÀ´Ö¸¶¨¶¯Ì¬Á´½Ó¿âËùÔÚµÄĿ¼(¹©±àÒëÆ÷²éÕÒÓÃ),ͬʱÓÃ-lÑ¡ÏîÖ¸¶¨ËõдµÄ¶¯Ì¬Á´½Ó¿âÃû.¼´:


# cc -L/usr/zzz/lib -lmy -o ady ady.c
#


(3)Ö±½ÓÒýÓÃËùÐèµÄ¶¯Ì¬Á´½Ó¿â.¼´:


# cc -o ady ady.c libmy.so
#


»ò


# cc -o ady ady.c /lib/libmy.so
#


µÈµÈ.ÆäÖÐ,¶¯Ì¬Á´½Ó¿âµÄ¿âÃû¿ÉÒÔ²ÉÓÃÏà¶Ô·¾¶ÐÎʽ(ÎļþÃû²»ÒÔ/¿ªÍ·),Ò²¿É²ÉÓþø¶Ô·¾¶ÐÎʽ(ÎļþÃûÒÔ/¿ªÍ·).




Îå¡¢¶¯Ì¬Á´½Ó³ÌÐòµÄÔËÐÐÓë¼ì²é




5.1 ÔËÐÐ


±àÒëÁ¬½ÓºÃº¬¶¯Ì¬º¯ÊýµÄ³ÌÐòºó,¾Í¿ÉÒÔÔËÐÐËüÁË.¶¯Ì¬Á´½Ó³ÌÐòÒòΪ¹²ÏíÁËϵͳÖеĶ¯Ì¬Á´½Ó¿â,ËùÒÔÆä¿Õ¼äÕ¼ÓúÜС.µ«Õâ²¢²»Òâζ¹¦ÄܵļõÉÙ,ËüµÄÖ´ÐÐÓ뾲̬Á¬½ÓµÄ³ÌÐòÖ´ÐÐ,Ч¹ûÍêÈ«Ïàͬ.ÔÚÃüÁîÌáʾ·ûϼüÈë³ÌÐòÃû¼°Ïà¹Ø²ÎÊýºó»Ø³µ¼´¿É,ÈçÏÂÀý:


$ ady
¶¯Ì¬Á´½Ó¿â¸ß¼¶Ó¦ÓÃʾ·¶
µ±Ç°ÈÕÆÚ: 2002-03-11
µ±Ç°Ê±¼ä: 19:39:06
$


5.2 ¼ì²é


¼ì²éʲô?¼ì²é¶¯Ì¬Á´½Ó³ÌÐò¾¿¾¹ÐèÒªÄÄЩ¹²Ïí¿â,ϵͳÖÐÊÇ·ñÒÑÓÐÕâЩ¿â,ûÓеĻ°,Óû§ºÃÏë°ì·¨°ÑÕâЩ¿â×°ÉÏ.


Ôõô¼ì²éÄØ?ÕâÀï,¸æËßÄãÒ»¸öʵÓóÌÐò--ldd,Õâ¸ö³ÌÐò¾ÍÊÇרÃÅÓÃÀ´¼ì²é¶¯Ì¬Á´½Ó³ÌÐòÒÀÀµÄÄЩ¹²Ïí¿âµÄ.


lddÃüÁîÐÐÓ÷¨ÈçÏÂ:


ldd [--version] [-v|--verbose] [-d|--data-relocs] [-r|--function-relocs] [--help] FILE...


¸÷Ñ¡Ïî˵Ã÷ÈçÏÂ:


(1) --version : ´ËÑ¡ÏîÓÃÓÚ´òÓ¡³ölddµÄ°æ±¾ºÅ.


(2) -v »ò --verbose : ´ËÑ¡ÏîָʾlddÊä³ö¹ØÓÚËùÒÀÀµµÄ¶¯Ì¬Á´½Ó¿âµÄ¾¡¿ÉÄÜÏêϸµÄÐÅÏ¢.


(3) -d »ò --data-relocs : ´ËÑ¡ÏîÖ´ÐÐÖض¨Î»,²¢ÇÒÏÔʾ²»´æÔڵĺ¯Êý.


(4) -r »ò --function-relocs : ´ËÑ¡ÏîÖ´ÐÐÊý¾Ý¶ÔÏóÓ뺯ÊýµÄÖض¨Î»,ͬʱ±¨¸æ²»´æÔڵĶÔÏó.


(5) --help : ´ËÑ¡ÏîÓÃÓÚ´òÓ¡³ölddµÄ°ïÖúÐÅÏ¢.


×¢: ÉÏÊöÑ¡ÏîÖÐ,³£ÓÃ-v(»ò--verbose)Ñ¡Ïî.


lddµÄÃüÁîÐвÎÊýΪFILE...,¼´Ò»¸ö»ò¶à¸öÎļþÃû(¶¯Ì¬Á´½Ó³ÌÐò»ò¶¯Ì¬Á´½Ó¿â).


Àý1:


$ ldd ady
libmy.so => ./libmy.so (0x40026000)
libc.so.6 => /lib/libc.so.6 (0x40028000)
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)
$


×¢: ÿÐÐ=>Ç°ÃæµÄ,Ϊ¶¯Ì¬Á´½Ó³ÌÐòËùÐèµÄ¶¯Ì¬Á´½Ó¿âµÄÃû×Ö,¶ø=>ºóÃæµÄ,ÔòÊÇÔËÐÐʱϵͳʵ¼Êµ÷ÓõĶ¯Ì¬Á´½Ó¿âµÄÃû×Ö,ËùÐèµÄ¶¯Ì¬Á´½Ó¿âÔÚϵͳÖв»´æÔÚʱ,=>ºóÃ潫ÏÔʾ"not found",À¨ºÅËùÀ¨µÄÊý×ÖΪÐéÄâµÄÖ´ÐеØÖ·.±¾ÀýÁгöadyËùÐèµÄÈý¸ö¶¯Ì¬Á´½Ó¿â,ÆäÖÐlibmy.soΪ×Ô¼ºÐ½¨µÄ¶¯Ì¬Á´½Ó¿â,¶ø libc.so.6Óë/lib/ld-linux.so.2¾ùΪϵͳµÄ¶¯Ì¬Á´½Ó¿â,Ç°Ò»¸öΪ»ù±¾C¿â,ºóÒ»¸ö¶¯Ì¬×°Èë¿â(ÓÃÓÚ¶¯Ì¬Á´½Ó¿âµÄ×°Èë¼°ÔËÐÐ).


Àý2:


ÒýÓÃ:
$ ldd -v ady
libmy.so => ./libmy.so (0x40026000)
libc.so.6 => /lib/libc.so.6 (0x40028000)
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)


Version information:
./ady:
libc.so.6 (GLIBC_2.1.3) => /lib/libc.so.6
libc.so.6 (GLIBC_2.0) => /lib/libc.so.6
./libmy.so:
libc.so.6 (GLIBC_2.1.3) => /lib/libc.so.6
libc.so.6 (GLIBC_2.0) => /lib/libc.so.6
/lib/libc.so.6:
ld-linux.so.2 (GLIBC_2.1.1) => /lib/ld-linux.so.2
ld-linux.so.2 (GLIBC_2.2.3) => /lib/ld-linux.so.2
ld-linux.so.2 (GLIBC_2.1) => /lib/ld-linux.so.2
ld-linux.so.2 (GLIBC_2.2) => /lib/ld-linux.so.2
ld-linux.so.2 (GLIBC_2.0) => /lib/ld-linux.so.2
$



×¢:±¾ÀýÓÃ-vÑ¡ÏîÒÔÏÔʾ¾¡¿ÉÄܶàµÄÐÅÏ¢,ËùÒÔÀýÖгýÁгöadyËùÐèÒªµÄ¶¯Ì¬Á´½Ó¿âÍâ,»¹ÁгöÁ˳ÌÐòËùÐ趯̬Á´½Ó¿â°æ±¾·½ÃæµÄÐÅÏ¢.


С½á: ÔÚLINUX¶¯Ì¬Á´½Ó¿âµÄ¸ß¼¶Ó¦ÓÃÖÐ,¹Ø¼üÓÐÁ½µã,Ò»ÊÇÈçºÎÈö¯Ì¬Á´½Ó¿âΪLINUXϵͳËù¹²Ïí,¶þÊDZàÒëÁ¬½Ó³ÌÐòʱÈçºÎ×ö.Èö¯Ì¬Á´½Ó¿âΪϵͳËù¹²Ïí, Ö÷ÒªÊÇÓÃldconfig¹ÜÀíÃüÁî,ά»¤ºÃϵͳ¹²Ïí¿âµÄ»º´æÎļþ/etc/ld.so.cache.±àÒëÁ¬½ÓʱÈçºÎ×ö?×¢ÒâÁ¬½ÓÉÏËùÓõĶ¯Ì¬Á´½Ó¿â¾Í¿ÉÒÔÁË.LINUX¶¯Ì¬Á´½Ó¿âµÄ¸ß¼¶Ó¦ÓÃ,ÓÃÒ»ÓþÍÃ÷°×:Æäʵ,¾ÍÊÇÕâô¼òµ¥!
µã»÷ÕâÀïÏÂÔØʾÀý³ÌÐò¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ