LinuxÏÂʵÏÖ¶¯Ì¬Á´½Ó¿âµÄ³£¼ûÉèÖü¼ÇÉ
linuxÏÂʵÏÖ¶¯Ì¬Á´½Ó¿âµÄ³£¼ûÉèÖü¼ÇÉ
¶¯Ì¬Á´½Ó¿â£¨Dynamic Link Library£¬¼ò³ÆDLL£©ÊÇÒ»ÖÖ¿ÉÒÔÔÚ¶à¸ö³ÌÐòÖ®¼ä¹²ÏíµÄ´úÂëºÍ×ÊÔ´¿â¡£ÔÚLinuxϵͳÖУ¬¶¯Ì¬Á´½Ó¿â±»³ÆΪ¹²Ïí¿â£¨Shared Library£©£¬Í¨¹ý½«´úÂëºÍ×ÊÔ´Óë³ÌÐòÊèÉ¢£¬¿ÉÒÔÌá¸ß´úÂëµÄ¸´ÓÃÐԺͳÌÐòµÄÔËÐÐЧÂÊ¡£±¾ÎĽ«ÏÈÈÝÔÚlinuxÏÂʵÏÖ¶¯Ì¬Á´½Ó¿âµÄ³£¼ûÉèÖü¼ÇÉ£¬²¢¸ø³öÏìÓ¦µÄ´úÂëʾÀý¡£
Ò»¡¢½¨É趯̬Á´½Ó¿â
ÔÚLinuxϵͳÖУ¬½¨É趯̬Á´½Ó¿âÐèÒªÒÔϼ¸¸ö°ì·¨£º
±àдԴ´úÂ룺½¨ÉèÐèÒª¹²ÏíµÄ´úÂëºÍ×ÊÔ´Îļþ£¬ºÃ±Èº¯Êý¡¢±äÁ¿¡¢³£Á¿µÈ¡£
±àд±àÒë¾ç±¾£º½¨Éè±àÒëºÍÁ´½Ó¶¯Ì¬Á´½Ó¿âµÄ¾ç±¾Îļþ£¬Í¨³£ÎªMakefile¡£
±àÒëÁ´½Ó£ºÖ´ÐбàÒë¾ç±¾£¬½«Ô´´úÂë±àÒë³É¶¯Ì¬Á´½Ó¿â¡£
ÒÔÏÂÊÇÒ»¸ö¼òÆÓµÄʾÀý£¬Õ¹Ê¾ÁËÔõÑù½¨ÉèÒ»¸ö¼òÆӵĶ¯Ì¬Á´½Ó¿â£º
Ê×ÏÈÊǶ¯Ì¬Á´½Ó¿âµÄÔ´´úÂ룬ÎÒÃǽ¨ÉèÒ»¸öÃûΪlibhello.cµÄÎļþ£¬ÆäÖаüÀ¨Ò»¸öÃûΪhelloµÄº¯Êý£º
#include <stdio.h> void hello() { printf("Hello, World! "); }
µÇ¼ºó¸´ÖÆ
½ÓÏÂÀ´ÊDZàÒë¾ç±¾Makefile£¬ÄÚÈÝÈçÏ£º
CC = gcc CFLAGS = -Wall -shared -fPIC libhello.so: libhello.c $(CC) $(CFLAGS) $^ -o $@
µÇ¼ºó¸´ÖÆ
ÔÚÏÂÁîÐÐÖÐÖ´ÐÐmakeÏÂÁ¼´¿ÉÌìÉú¶¯Ì¬Á´½Ó¿âlibhello.so¡£
¶þ¡¢Ê¹Óö¯Ì¬Á´½Ó¿â
ʹÓö¯Ì¬Á´½Ó¿âͬÑùÐèÒª¼¸¸ö°ì·¨£º
Í·Îļþ°üÀ¨£ºÔÚÐèҪʹÓö¯Ì¬Á´½Ó¿âµÄ´úÂëÖУ¬°üÀ¨¶¯Ì¬Á´½Ó¿âµÄÍ·Îļþ¡£
ÉèÖÃÁ´½ÓÑ¡ÏÔÚ±àÒëºÍÁ´½Ó´úÂëʱ£¬Ìí¼Ó¶¯Ì¬Á´½Ó¿âµÄÁ´½ÓÑ¡Ïî¡£
ŲÓú¯Êý£ºÔÚ´úÂëÖÐͨ¹ýŲÓö¯Ì¬Á´½Ó¿âÖеĺ¯ÊýÀ´Ê¹ÓÃÆ书Ч¡£
ÒÔÏÂÊÇÒ»¸ö¼òÆÓµÄʾÀý£¬Õ¹Ê¾ÁËÔõÑùʹÓøոս¨ÉèµÄ¶¯Ì¬Á´½Ó¿â£º
Ê×ÏÈÊÇʹÓö¯Ì¬Á´½Ó¿âµÄÔ´´úÂ룬ÎÒÃǽ¨ÉèÒ»¸öÃûΪmain.cµÄÎļþ£¬ÆäÖаüÀ¨Å²Óö¯Ì¬Á´½Ó¿âÖеÄhelloº¯ÊýµÄ´úÂë¡£
#include <stdio.h> #include "libhello.h" int main() { hello(); return 0; }
µÇ¼ºó¸´ÖÆ
½ÓÏÂÀ´ÊDZàÒë¾ç±¾Makefile£¬ÄÚÈÝÈçÏ£º
CC = gcc CFLAGS = -Wall -L. -lhello main: main.c $(CC) $(CFLAGS) $^ -o $@
µÇ¼ºó¸´ÖÆ
ÔÚÏÂÁîÐÐÖÐÖ´ÐÐmakeÏÂÁ¼´¿ÉÌìÉú¿ÉÖ´ÐÐÎļþmain¡£ÔËÐÐÕâ¸ö³ÌÐò£¬¼´¿É¿´µ½´òÓ¡³ö”Hello, World!”¡£
Èý¡¢¶¯Ì¬Á´½Ó¿âµÄ³£¼ûÉèÖü¼ÇÉ
¶¯Ì¬Á´½Ó¿âµÄÃüÃû
ÔÚ½¨É趯̬Á´½Ó¿âʱ£¬Í¨³£Ê¹ÓÃlib×÷Ϊǰ׺£¬.so×÷ΪÀ©Õ¹Ãû¡£ºÃ±ÈÉÏÃæµÄʾÀýÖУ¬ÎÒÃÇʹÓÃÁËlibhello.so×÷Ϊ¶¯Ì¬Á´½Ó¿âµÄÃû×Ö¡£ÕâÊÇÒ»ÖÖÃüÃûÔ¼¶¨£¬ÓÐÖúÓÚÇø·Ö¶¯Ì¬Á´½Ó¿âºÍÆäËûÀàÐ͵ÄÎļþ¡£
¶¯Ì¬Á´½Ó¿âµÄ°æ±¾¿ØÖÆ
ΪÁËÀû±ã¶Ô¶¯Ì¬Á´½Ó¿â¾ÙÐа汾¿ØÖÆ£¬¿ÉÒÔÔÚ¶¯Ì¬Á´½Ó¿âµÄÃû×ÖÖмÓÈë°æ±¾ºÅ¡£ºÃ±È£¬libhello.so¿ÉÒÔ¸ÄΪlibhello.so.1£¬ÌåÏÖ°æ±¾ºÅΪ1µÄ¶¯Ì¬Á´½Ó¿â¡£Í¬Ê±£¬»¹¿ÉÒÔÔÚ±àÒëʱʹÓÃ-fvisibility=hiddenÀ´Òþ²Ø¶¯Ì¬Á´½Ó¿âÖв»ÐèÒª¶ÔÍâ̻¶µÄ·ûºÅ¡£
¶¯Ì¬Á´½Ó¿âµÄ·¾¶ÉèÖÃ
ÔÚʹÓö¯Ì¬Á´½Ó¿âʱ£¬²Ù×÷ϵͳÐèÒªÖªµÀ¶¯Ì¬Á´½Ó¿âµÄ·¾¶¡£¿ÉÒÔͨ¹ýÒÔϼ¸ÖÖ·½·¨À´ÉèÖö¯Ì¬Á´½Ó¿âµÄ·¾¶£º
½«¶¯Ì¬Á´½Ó¿â¿½±´µ½±ê×¼µÄ¿â·¾¶£¬ºÃ±È/usr/lib»ò/usr/local/lib¡£
ÔÚ±àÒëʱʹÓÃ-LÑ¡ÏîÖ¸¶¨¿âµÄ·¾¶£¬ºÃ±È-L/opt/libs¡£
ÔÚ³ÌÐòÔËÐÐʱ£¬Í¨¹ýÉèÖÃLD_LIBRARY_PATHÇéÐαäÁ¿À´Ö¸¶¨¶¯Ì¬Á´½Ó¿âµÄËÑË÷·¾¶£¬ºÃ±Èexport LD_LIBRARY_PATH=/opt/libs¡£
ͨ¹ýÒÔÉÏÉèÖü¼ÇÉ£¬¿ÉÒÔÔÚLinuxϵͳÖÐʵÏÖ¶¯Ì¬Á´½Ó¿âµÄ³£¼ûÉèÖá£
ËÄ¡¢×ܽá
ͨ¹ý±¾ÎĵÄÏÈÈÝ£¬ÎÒÃÇÏàʶµ½ÁËÔõÑùÔÚlinuxÏÂʵÏÖ¶¯Ì¬Á´½Ó¿âµÄ³£¼ûÉèÖü¼ÇÉ¡£¶¯Ì¬Á´½Ó¿âÄܹ»Ìá¸ß´úÂëµÄ¸´ÓÃÐԺͳÌÐòµÄÔËÐÐЧÂÊ£¬ÊÇÈí¼þ¿ª·¢Öг£¼ûµÄÊÖÒÕ¡£Ï£Íû±¾ÎĶÔÄúÔÚLinuxÏÂʹÓö¯Ì¬Á´½Ó¿âÓÐËù×ÊÖú£¬²¢ÌṩÁËÏìÓ¦µÄ´úÂëʾÀý¡£
²Î¿¼×ÊÁÏ£º
http://www.runoob.com/linux/linux-libraries.html
https://zh.wikipedia.org/wiki/¶¯Ì¬Á´½Ó¿â
ÒÔÉϾÍÊÇLinuxÏÂʵÏÖ¶¯Ì¬Á´½Ó¿âµÄ³£¼ûÉèÖü¼ÇɵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡