博客
关于我
gcc -static 命令
阅读量:489 次
发布时间:2019-03-07

本文共 1867 字,大约阅读时间需要 6 分钟。

好的,以下是优化后的文章:


刚发现这是一个神奇的命令!比如,我们有一个简单的程序,这个程序依赖动态库 libpthread.so。想象一下,现在有一个程序,它依靠第二个线程来完成某些任务。那我们可以通过观察这个程序,看看它是如何运行的。

首先,让我们看看这个程序的代码:

#include 
#include
void *thread_exe(void *x_void_ptr) { /* 这个函数由第二个线程执行 */ int *x_ptr = (int *)x_void_ptr; while (++(*x_ptr) < 100); // 增加到 100 printf("x increment finished\n"); return NULL;}int testFunc(int param) { printf(" testFunc %i\n", param); pthread_t inc_x_thread; int x = 0, y = 0; /* 创建一个线程运行 thread_exe(&y) */ if (pthread_create(&inc_x_thread, NULL, thread_exe, &y)) { fprintf(stderr, "Error creating thread\n"); return 1; } /* 在第一个线程中,增加 y 的值 */ while (++y < 100) { printf("y increment finished\n"); } /* 等待第二个线程完成 */ if (pthread_join(inc_x_thread, NULL)) { fprintf(stderr, "Error joining thread\n"); return 2; } /* 展示结果 */ printf("x: %d, y: %d\n", x, y); return 0;}int main(int argc, char *argv) { int a = 100; testFunc(a); return 1;}

static 编译的情况

编译时使用 gcc -static 会让程序静态编译。这意味着所有依赖的函数和相关内容都会被编译到程序中。比如:

gcc test_main.c -static -o test_main -lpthread

运行后,你会发现生成的程序文件非常大!这是因为静态编译会将所有相关的函数(例如 pthread_create() 和其所有依赖)都编译进去,程序不再依赖外部动态库。为了验证这一点,我们可以使用 nm 工具检查文件主аних:

nm test_main

截取部分结果:

0000000000404fb0 T __pthread_cleanup_push0000000000404fb0 T _pthread_cleanup_push...

可以看到,和 pthread 相关的各种函数都被包含进来了。


不加 -static 的情况

如果不使用 -static 加编译:

gcc test_main.c -o test_main -lpthread

生成的程序文件会变得小很多!使用 nm 检查时,只会看到少量的函数:

0000000000601060 B __bss_start...U printf@@GLIBC_2.2.5U pthread_create@@GLIBC_2.2.5U pthread_join@@GLIBC_2.2.5

这些符号表示这些函数是从动态库中导入的。


总结

当使用 gcc -static 编译程序时,会将所有依赖的动态库函数以及它们的支持内容编制入程序,生成的大文件体量较大,但运行时无需外部动态库支持。相反,如果不加 -static,程序依赖动态库,文件会小许多,但运行时需要这些库。静态编译的好处是最大程度地减少依赖,缺点则是文件体量较大。

如果对编译过程不太熟悉,或者想进一步了解如何优化程序性能,可以继续探索更多的编译选项。比如,了解哪些选项会影响程序的调试体验,或者怎样平衡 compilation speed 计划和程序的大小等。


转载地址:http://lwedz.baihongyu.com/

你可能感兴趣的文章
Node中同步与异步的方式读取文件
查看>>
node中的get请求和post请求的不同操作【node学习第五篇】
查看>>
Node中的Http模块和Url模块的使用
查看>>
Node中自启动工具supervisor的使用
查看>>
Node入门之创建第一个HelloNode
查看>>
node全局对象 文件系统
查看>>
Node出错导致运行崩溃的解决方案
查看>>
Node响应中文时解决乱码问题
查看>>
node基础(二)_模块以及处理乱码问题
查看>>
node安装卸载linux,Linux运维知识之linux 卸载安装node npm
查看>>
node安装及配置之windows版
查看>>
Node实现小爬虫
查看>>
Node提示:error code Z_BUF_ERROR,error error -5,error zlib:unexpected end of file
查看>>
Node提示:npm does not support Node.js v12.16.3
查看>>
Node搭建静态资源服务器时后缀名与响应头映射关系的Json文件
查看>>
Node服务在断开SSH后停止运行解决方案(创建守护进程)
查看>>
node模块化
查看>>
node模块的本质
查看>>
node环境下使用import引入外部文件出错
查看>>
node环境:Error listen EADDRINUSE :::3000
查看>>