Linux下编译实践

以上三篇文章转载了Linux编译原理:预处理 > 编译 > 汇编 > 链接

strings:查看打印文件中可打印的字符
nm: 列出某些文件中的符号
=============================================
以下是实际操作实践Linux编译原理

在ubutun系统目录/home/spx/编译下有test1.c test2.c main.c三个文件
spx@spx-linux:~/编译$ cat test1.c 
#include<stdio.h>

void test1(){
	printf("test1...\n"); 
}
void no_use_fun()
{
	printf("no use fun...\n");
}
spx@spx-linux:~/编译$ cat test2.c 
#include<stdio.h>

void test2()
{
	printf("test2\n");
}
spx@spx-linux:~/编译$ cat main.c 
#include<stdio.h>

main()
{
	test1();
}
spx@spx-linux:~/编译$ 

将test1.c编译动态库libtest1.so  静态库libtest1.a 并且nm查看符号列表 

n m可以看出 动态库libtest1.so  静态库libtest1.a 均包含了函数test1 、no_use_fun
spx@spx-linux:~/编译$ gcc -fPIC -c test1.c 
spx@spx-linux:~/编译$ gcc -shared -fPIC -o libtest1.so test1.o 
spx@spx-linux:~/编译$ ar rc libtest1.a test1.o 
spx@spx-linux:~/编译$ nm libtest1.a 

test1.o:
                 U _GLOBAL_OFFSET_TABLE_
0000000000000012 T no_use_fun
                 U puts
0000000000000000 T test1
spx@spx-linux:~/编译$ nm libtest1.so 
0000000000200e50 a _DYNAMIC
0000000000200fe8 a _GLOBAL_OFFSET_TABLE_
                 w _Jv_RegisterClasses
0000000000200e30 d __CTOR_END__
0000000000200e28 d __CTOR_LIST__
0000000000200e40 d __DTOR_END__
0000000000200e38 d __DTOR_LIST__
00000000000006f8 r __FRAME_END__
0000000000200e48 d __JCR_END__
0000000000200e48 d __JCR_LIST__
0000000000201018 A __bss_start
                 w __cxa_finalize@@GLIBC_2.2.5
00000000000005f0 t __do_global_ctors_aux
0000000000000520 t __do_global_dtors_aux
0000000000201010 d __dso_handle
                 w __gmon_start__
0000000000201018 A _edata
0000000000201028 A _end
0000000000000628 T _fini
00000000000004b8 T _init
0000000000000500 t call_gmon_start
0000000000201018 b completed.6531
0000000000201020 b dtor_idx.6533
00000000000005a0 t frame_dummy
00000000000005de T no_use_fun
                 U puts@@GLIBC_2.2.5
00000000000005cc T test1
spx@spx-linux:~/编译$ 

分别将 动态库libtest1.so 静态库libtest1.a 链接到可执行文件main1、main2中,并且执行。执行main1错误,找不到共享库 libtest1.so。这是动态库执行常见错误,需要将动态库文件路径配置到环境变量。可以ldd查看执行文件链接库
spx@spx-linux:~/编译$ gcc -o main1 main.c libtest1.so 
spx@spx-linux:~/编译$ gcc -o main2 main.c libtest1.a
spx@spx-linux:~/编译$ ./main1 
./main1: error while loading shared libraries: libtest1.so: cannot open shared object file: No such file or directory
spx@spx-linux:~/编译$ ./main2 
test1...
spx@spx-linux:~/编译$ 


spx@spx-linux:~/编译$ ldd main1 
	linux-vdso.so.1 =>  (0x00007fff019ff000)
	libtest1.so => not found
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f148e2a9000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f148e678000)
spx@spx-linux:~/编译$
加入环境变量LD_LIBRARY_PATH,ldd找到动态库,并且执行main1成功
spx@spx-linux:~/编译$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/spx/编译
spx@spx-linux:~/编译$ echo $LD_LIBRARY_PATH
:/home/spx/编译
spx@spx-linux:~/编译$ ldd main1 
	linux-vdso.so.1 =>  (0x00007fff7b9ff000)
	libtest1.so (0x00007f86876ff000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f8687332000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f8687903000)
spx@spx-linux:~/编译$ ./main1 
test1...
spx@spx-linux:~/编译$ 
如果我们修改test1.c的内容,重新生成libtest1.so libtest1.a,再执行
spx@spx-linux:~/编译$ cat test1.c 
#include<stdio.h>

void test1(){
	printf("test1 update...\n"); 
}
void no_use_fun()
{
	printf("no use fun...\n");
}
spx@spx-linux:~/编译$ gcc -fPIC -c test1.c 
spx@spx-linux:~/编译$ gcc -shared -fPIC -o libtest1.so test1.o 
spx@spx-linux:~/编译$ ar rc libtest1.a test1.o 
spx@spx-linux:~/编译$ ./main1 
test1 update...
spx@spx-linux:~/编译$ ./main2 
test1...

main1执行修改后的内容,而main2未修改。因为静态链接将实际.o文件加入可执行文件,不会自己更新,当库文件更新时,需要重新编译。而动态链接,会在可执行文件加入指向动态库的指针,当库文件更新时不需要重新编译。
spx@spx-linux:~/编译$ gcc -o main2 main.c libtest1.a 
spx@spx-linux:~/编译$ ./main2 
test1 update...
spx@spx-linux:~/编译$

猜你喜欢

转载自blog.csdn.net/weixin_40017062/article/details/80574871