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

LinuxÖд´½¨¾²Ì¬¿âºÍ¶¯Ì¬¿â

·¢²¼Ê±¼ä:2008-04-23 15:39:26À´Ô´:ºìÁª×÷Õß:History
ÎÒÃÇͨ³£°ÑһЩ¹«Óú¯ÊýÖÆ×÷³Éº¯Êý¿â£¬¹©ÆäËü³ÌÐòʹÓ᣺¯Êý¿â·ÖΪ¾²Ì¬¿âºÍ¶¯Ì¬¿âÁ½ÖÖ¡£¾²Ì¬¿âÔÚ³ÌÐò±àÒëʱ»á±»Á¬½Óµ½Ä¿±ê´úÂëÖУ¬³ÌÐòÔËÐÐʱ½«²»ÔÙÐèÒª¸Ã¾²Ì¬¿â¡£¶¯Ì¬¿âÔÚ³ÌÐò±àÒëʱ²¢²»»á±»Á¬½Óµ½Ä¿±ê´úÂëÖУ¬¶øÊÇÔÚ³ÌÐòÔËÐÐÊDzű»ÔØÈ룬Òò´ËÔÚ³ÌÐòÔËÐÐʱ»¹ÐèÒª¶¯Ì¬¿â´æÔÚ¡£±¾ÎÄÖ÷Ҫͨ¹ý¾ÙÀýÀ´ËµÃ÷ÔÚLinuxÖÐÈçºÎ´´½¨¾²Ì¬¿âºÍ¶¯Ì¬¿â£¬ÒÔ¼°Ê¹ÓÃËüÃÇ¡£

ÔÚ´´½¨º¯Êý¿âÇ°£¬ÎÒÃÇÏÈÀ´×¼±¸¾ÙÀýÓõÄÔ´³ÌÐò£¬²¢½«º¯Êý¿âµÄÔ´³ÌÐò±àÒë³É.oÎļþ¡£

µÚ1²½£º±à¼­µÃµ½¾ÙÀýµÄ³ÌÐò--hello.h¡¢hello.cºÍmain.c£»

hello.c(¼û³ÌÐò2)ÊǺ¯Êý¿âµÄÔ´³ÌÐò£¬ÆäÖаüº¬¹«Óú¯Êýhello£¬¸Ãº¯Êý½«ÔÚÆÁÄ»ÉÏÊä³ö"Hello XXX!"¡£hello.h(¼û³ÌÐò1)Ϊ¸Ãº¯Êý¿âµÄÍ·Îļþ¡£main.c(¼û³ÌÐò3)Ϊ²âÊÔ¿âÎļþµÄÖ÷³ÌÐò£¬ÔÚÖ÷³ÌÐòÖе÷ÓÃÁ˹«Óú¯Êýhello¡£

#ifndef HELLO_H
#define HELLO_H

void hello(const char *name);

#endif //HELLO_H

³ÌÐò1: hello.h

#include

void hello(const char *name)
{
printf("Hello %s!\n", name);
}

³ÌÐò2: hello.c

#include "hello.h"

int main()
{
hello("everyone");
return 0;
}
³ÌÐò3: main.c

µÚ2²½£º½«hello.c±àÒë³É.oÎļþ£»

ÎÞÂÛ¾²Ì¬¿â£¬»¹ÊǶ¯Ì¬¿â£¬¶¼ÊÇÓÉ.oÎļþ´´½¨µÄ¡£Òò´Ë£¬ÎÒÃDZØÐ뽫Դ³ÌÐòhello.cͨ¹ýgccÏȱàÒë³É.oÎļþ¡£

ÔÚϵͳÌáʾ·ûϼüÈëÒÔÏÂÃüÁîµÃµ½hello.oÎļþ¡£

# gcc -c hello.c

#

(×¢1£º±¾ÎIJ»½éÉܸ÷ÃüÁîʹÓúÍÆä²ÎÊý¹¦ÄÜ£¬ÈôÏ£ÍûÏêϸÁ˽âËüÃÇ£¬Çë²Î¿¼ÆäËûÎĵµ¡£)

(×¢2£ºÊ××Ö·û"#"ÊÇϵͳÌáʾ·û£¬²»ÐèÒª¼üÈ룬ÏÂÎÄÏàͬ¡£)

ÎÒÃÇÔËÐÐlsÃüÁî¿´¿´ÊÇ·ñÉú´æÁËhello.oÎļþ¡£

# ls

hello.c hello.h hello.o main.c

#

(×¢3£ºÊ××Ö·û²»ÊÇ"#"ΪϵͳÔËÐнá¹û£¬ÏÂÎÄÏàͬ¡£)

ÔÚlsÃüÁî½á¹ûÖУ¬ÎÒÃÇ¿´µ½ÁËhello.oÎļþ£¬±¾²½²Ù×÷Íê³É¡£

ÏÂÃæÎÒÃÇÏÈÀ´¿´¿´ÈçºÎ´´½¨¾²Ì¬¿â£¬ÒÔ¼°Ê¹ÓÃËü¡£

µÚ3²½£ºÓÉ.oÎļþ´´½¨¾²Ì¬¿â£»

¾²Ì¬¿âÎļþÃûµÄÃüÃû¹æ·¶ÊÇÒÔlibΪǰ׺£¬½ô½Óןú¾²Ì¬¿âÃû£¬À©Õ¹ÃûΪ.a¡£ÀýÈ磺ÎÒÃǽ«´´½¨µÄ¾²Ì¬¿âÃûΪmyhello£¬Ôò¾²Ì¬¿âÎļþÃû¾ÍÊÇlibmyhello.a¡£ÔÚ´´½¨ºÍʹÓþ²Ì¬¿âʱ£¬ÐèҪעÒâÕâµã¡£´´½¨¾²Ì¬¿âÓÃarÃüÁî¡£

ÔÚϵͳÌáʾ·ûϼüÈëÒÔÏÂÃüÁ´´½¨¾²Ì¬¿âÎļþlibmyhello.a¡£

# ar cr libmyhello.a hello.o

#

ÎÒÃÇͬÑùÔËÐÐlsÃüÁî²é¿´½á¹û£º

# ls

hello.c hello.h hello.o libmyhello.a main.c

#

lsÃüÁî½á¹ûÖÐÓÐlibmyhello.a¡£

µÚ4²½£ºÔÚ³ÌÐòÖÐʹÓþ²Ì¬¿â£»

¾²Ì¬¿âÖÆ×÷ÍêÁË£¬ÈçºÎʹÓÃËüÄÚ²¿µÄº¯ÊýÄØ£¿Ö»ÐèÒªÔÚʹÓõ½ÕâЩ¹«Óú¯ÊýµÄÔ´³ÌÐòÖаüº¬ÕâЩ¹«Óú¯ÊýµÄÔ­ÐÍÉùÃ÷£¬È»ºóÔÚÓÃgccÃüÁîÉú³ÉÄ¿±êÎļþʱָÃ÷¾²Ì¬¿âÃû£¬gcc½«»á´Ó¾²Ì¬¿âÖн«¹«Óú¯ÊýÁ¬½Óµ½Ä¿±êÎļþÖС£×¢Ò⣬gcc»áÔÚ¾²Ì¬¿âÃûÇ°¼ÓÉÏǰ׺lib£¬È»ºó×·¼ÓÀ©Õ¹Ãû.aµÃµ½µÄ¾²Ì¬¿âÎļþÃûÀ´²éÕÒ¾²Ì¬¿âÎļþ¡£

ÔÚ³ÌÐò3:main.cÖУ¬ÎÒÃÇ°üº¬Á˾²Ì¬¿âµÄÍ·Îļþhello.h£¬È»ºóÔÚÖ÷³ÌÐòmainÖÐÖ±½Óµ÷Óù«Óú¯Êýhello¡£ÏÂÃæÏÈÉú³ÉÄ¿±ê³ÌÐòhello£¬È»ºóÔËÐÐhello³ÌÐò¿´¿´½á¹ûÈçºÎ¡£

# gcc -o hello main.c -L. -lmyhello

# ./hello

Hello everyone!

#

ÎÒÃÇɾ³ý¾²Ì¬¿âÎļþÊÔÊÔ¹«Óú¯ÊýhelloÊÇ·ñÕæµÄÁ¬½Óµ½Ä¿±êÎļþ helloÖÐÁË¡£

# rm libmyhello.a

rm: remove regular file `libmyhello.a'? y

# ./hello

Hello everyone!

#

³ÌÐòÕÕ³£ÔËÐУ¬¾²Ì¬¿âÖеĹ«Óú¯ÊýÒѾ­Á¬½Óµ½Ä¿±êÎļþÖÐÁË¡£

ÎÒÃǼÌÐø¿´¿´ÈçºÎÔÚLinuxÖд´½¨¶¯Ì¬¿â¡£ÎÒÃÇ»¹ÊÇ´Ó.oÎļþ¿ªÊ¼¡£

µÚ5²½£ºÓÉ.oÎļþ´´½¨¶¯Ì¬¿âÎļþ£»

¶¯Ì¬¿âÎļþÃûÃüÃû¹æ·¶ºÍ¾²Ì¬¿âÎļþÃûÃüÃû¹æ·¶ÀàËÆ£¬Ò²ÊÇÔÚ¶¯Ì¬¿âÃûÔö¼Óǰ׺lib£¬µ«ÆäÎļþÀ©Õ¹ÃûΪ.so¡£ÀýÈ磺ÎÒÃǽ«´´½¨µÄ¶¯Ì¬¿âÃûΪmyhello£¬Ôò¶¯Ì¬¿âÎļþÃû¾ÍÊÇlibmyhello.so¡£ÓÃgccÀ´´´½¨¶¯Ì¬¿â¡£

ÔÚϵͳÌáʾ·ûϼüÈëÒÔÏÂÃüÁîµÃµ½¶¯Ì¬¿âÎļþlibmyhello.so¡£

# gcc -shared -fPCI -o libmyhello.so hello.o

#

ÎÒÃÇÕÕÑùʹÓÃlsÃüÁî¿´¿´¶¯Ì¬¿âÎļþÊÇ·ñÉú³É¡£

# ls

hello.c hello.h hello.o libmyhello.so main.c

#

µÚ6²½£ºÔÚ³ÌÐòÖÐʹÓö¯Ì¬¿â£»

ÔÚ³ÌÐòÖÐʹÓö¯Ì¬¿âºÍʹÓþ²Ì¬¿âÍêÈ«Ò»Ñù£¬Ò²ÊÇÔÚʹÓõ½ÕâЩ¹«Óú¯ÊýµÄÔ´³ÌÐòÖаüº¬ÕâЩ¹«Óú¯ÊýµÄÔ­ÐÍÉùÃ÷£¬È»ºóÔÚÓÃgccÃüÁîÉú³ÉÄ¿±êÎļþʱָÃ÷¶¯Ì¬¿âÃû½øÐбàÒë¡£ÎÒÃÇÏÈÔËÐÐgccÃüÁîÉú³ÉÄ¿±êÎļþ£¬ÔÙÔËÐÐËü¿´¿´½á¹û¡£

# gcc -o hello main.c -L. -lmyhello

# ./hello

./hello: error while loading shared libraries: libmyhello.so: cannot open shared object file: No such file or directory

#

Ŷ£¡³ö´íÁË¡£¿ì¿´¿´´íÎóÌáʾ£¬Ô­À´ÊÇÕÒ²»µ½¶¯Ì¬¿âÎļþlibmyhello.so¡£³ÌÐòÔÚÔËÐÐʱ£¬»áÔÚ/usr/libºÍ/libµÈĿ¼ÖвéÕÒÐèÒªµÄ¶¯Ì¬¿âÎļþ¡£ÈôÕÒµ½£¬ÔòÔØÈ붯̬¿â£¬·ñÔò½«ÌáʾÀàËÆÉÏÊö´íÎó¶øÖÕÖ¹³ÌÐòÔËÐС£ÎÒÃǽ«Îļþlibmyhello.so¸´ÖƵ½Ä¿Â¼/usr/libÖУ¬ÔÙÊÔÊÔ¡£

# mv libmyhello.so /usr/lib

# ./hello

Hello everyone!

#

³É¹¦ÁË¡£ÕâÒ²½øÒ»²½ËµÃ÷Á˶¯Ì¬¿âÔÚ³ÌÐòÔËÐÐʱÊÇÐèÒªµÄ¡£

ÎÒÃǻعýÍ·¿´¿´£¬·¢ÏÖʹÓþ²Ì¬¿âºÍʹÓö¯Ì¬¿â±àÒë³ÉÄ¿±ê³ÌÐòʹÓõÄgccÃüÁîÍêÈ«Ò»Ñù£¬Äǵ±¾²Ì¬¿âºÍ¶¯Ì¬¿âͬÃûʱ£¬gccÃüÁî»áʹÓÃÄĸö¿âÎļþÄØ£¿±§×ŶÔÎÊÌâ±Ø¾¿µ½µ×µÄÐÄÇ飬À´ÊÔÊÔ¿´¡£

ÏÈɾ³ý³ý.cºÍ.hÍâµÄËùÓÐÎļþ£¬»Ö¸´³ÉÎÒÃǸոձ༭Íê¾ÙÀý³ÌÐò״̬¡£

# rm -f hello hello.o /usr/lib/libmyhello.so

# ls

hello.c hello.h main.c

#

ÔÚÀ´´´½¨¾²Ì¬¿âÎļþlibmyhello.aºÍ¶¯Ì¬¿âÎļþlibmyhello.so¡£

# gcc -c hello.c

# ar cr libmyhello.a hello.o

# gcc -shared -fPCI -o libmyhello.so hello.o

# ls

hello.c hello.h hello.o libmyhello.a libmyhello.so main.c

#

ͨ¹ýÉÏÊö×îºóÒ»ÌõlsÃüÁ¿ÉÒÔ·¢ÏÖ¾²Ì¬¿âÎļþlibmyhello.aºÍ¶¯Ì¬¿âÎļþlibmyhello.so¶¼ÒѾ­Éú³É£¬²¢¶¼ÔÚµ±Ç°Ä¿Â¼ÖС£È»ºó£¬ÎÒÃÇÔËÐÐgccÃüÁîÀ´Ê¹Óú¯Êý¿âmyhelloÉú³ÉÄ¿±êÎļþhello£¬²¢ÔËÐгÌÐò hello¡£

# gcc -o hello main.c -L. -lmyhello

# ./hello

./hello: error while loading shared libraries: libmyhello.so: cannot open shared object file: No such file or directory

#

´Ó³ÌÐòhelloÔËÐеĽá¹ûÖкÜÈÝÒ×ÖªµÀ£¬µ±¾²Ì¬¿âºÍ¶¯Ì¬¿âͬÃûʱ£¬gccÃüÁÓÅÏÈʹÓö¯Ì¬¿â¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ