���隙�牟�����
�伪��� ������一��

��态�及��态��慕��� 
UNIX系统�����������为������员�供�舜����目��募�����一���������些���募��� 
�������������没����������蟆?�����员�����������约��目������芯�要���写 
����函�������些�������������������指��源�募��姆���同�������堑某� 
��������������然也�强��缘模���也��一些缺�: 
.... 

���������������删�态�。 
����知����态��也�频����,�诖说����募���实�������占���一系�械�目���募����� 
些目���募�������cc(gcc)�����源���������傻摹���耍���态�����煞���实���� 
���苑殖������� 
1.��������������诘�源�募������目录�募������,����myfunc.c���������������� 
��������目���募��� 
gcc -c myfunc.c 
��然���卸��源�募�时��只����gcc �������薪���直����暇涂����恕� 
����一���������芄��玫���源�募���目���募���������玫�myfunc.o 
2.����目���募��占������诺�一����态���募��小�����要������ar������桑��: 
ar r ~/lib/libtest.a myfunc.o 

������态���库、������要�玫�����墓��撸�������gcc���即����伞���时�������� 
���屑���-K PIC��-G������选�,�������强��越���libtest�亩�态�本�� 
gcc -K PIC -G -o $HOME/lib/libtest.so myfunc.c 



-- 

������: hellguard (小��), ����: Unix 
�� ��: 顺��������linux��态��� 
����站: BBS 水木�华站 (Fri Nov 12 10:49:03 1999) 

������式�芎每��模�����为要���������� 
扭���瞬��伲����录��隆���������linux�碌����樱� 
����solaris�碌�也�不�,����发�植�同���透� 
����细说���������貌��谩� 

ldd filename�涂��钥��������玫���些�。 

ld -o OUTPUT /lib/crt0.o hello.o -lc 
ld��������战���募� OUTPUT �� 
�玫�/lib/crt0.o hello.o�约�libc.a 
libc.a���员�准������路������慰�-l选������� 

ld��选��顺�����,�����馗�������慕�����前��摹� 

gcc -c -fPIC linuxlib.c -O3 -o linuxlib.o 
gcc -shared -Wl,-soname,liblinuxlib.so.1 \ 
-O3 -o liblinuxlib.so.1.0 linuxlib.o 
( 
ld -m elf_i386 -shared -o liblinuxlib.so.1.0 \ 
-soname liblinuxlib.so.1 linuxlib.o 
�����������募���小����知�������泻���� 
-o liblinuxlib.so.1.0 ����募������,�丶��呛��� 
�����������要��确 
��然�萍�使��������拇�本��息�����郑����� 
-soname liblinuxlib.so.1 
) 
ln -sf liblinuxlib.so.1.0 liblinuxlib.so.1 
( 
����时��dynamic linker��� -soname \ 
liblinuxlib.so.1 去寻��liblinuxlib.so.1 
) 
ln -sf liblinuxlib.so.1 liblinuxlib.so 
( 
��������一�����,ld������ -llinuxlib 
选��去寻��liblinuxlib.so 
) 
export LD_LIBRARY_PATH=. 
( 
����时������时����要��������拇��� 
����时�����������������������通�� 
��ldd�看�发��没�卸�态������息 
����时����������������������薹��业���态���涌� 

���������������路��"."���������谢����捅冉��烦 
export LD_LIBRARY_PATH=`pwd`:$LD_LIBRARY_PATH 
) 
gcc -L. -lbsd -llinuxlib -O3 -o linuxkiller linuxkiller.c 
( 
-L. 指�����准������路��之��目�����路�������指 
�������也���liblinuxlib.so 
���选�并���艽��� LD_LIBRARY_PATH ������������然����通�� 
����ldd linuxkiller��时��发��没�卸�态������息 
) 

���园��约��亩�态���涌�诺�/usr/lib����/lib��去������ 
�薷�/etc/ld.so.conf 
然������/sbin/ldconfig��������man ldconfig 

-- 


������: hellguard (小��), ����: Unix 
�� ��: Re: help :static library----what's wrong--Thanks. 
����站: BBS 水木�华站 (Thu Aug 16 14:22:39 2001) 


�� �� hululu (������~�丶���) �拇������到: �� 
: I am building a static library. But I do not know what is 
: wrong. Below is a sample: 
: tt.c: 
: #include 
: int func(){ 
: printf("func test\n"); 
: return 0; 
: } 
: ii.c: 
: #include 
: extern int func(); 
: ................... 
�� ��删�态���涌���� 

1. vi demo.h 
void demo ( void ); 

vi demo.c 
#include 
#include "demo.h" 
void demo ( void ) 
{ 
printf( "hello world\n" ); 
return; 
} 

2. g++ -Wstrict-prototypes -Wall -Wunused -O3 -c demo.c -o demo.o 
file demo.o 

3. ar -rsv libdemo.a demo.o 
a - demo.o 
file libdemo.a 
nm -s libdemo.a 
ar -tv libdemo.a 
4. vi scz.c 
#include 
#include "demo.h" 
int main ( int argc, void * argv[] ) 
{ 
demo(); 
return 0; 
} 

5. g++ scz.c -L. -ldemo -Wstrict-prototypes -Wall -Wunused -O3 -o scz 

�� �� SweetWang (Sweet) �拇������到: ��
: ���������为libhello.so�目�
: 谢谢~~


--
�� ��源:������未��站 bbs.pku.edu.cn��[FROM: 202.119.32.102]