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:~/编译$ 
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:~/编译$ 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:~/编译$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:~/编译$ 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:~/编译$

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不会叫的狼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值