博客
关于我
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/

你可能感兴趣的文章
php openssl aes ecb,php openssl_encrypt AES-128-ECB iOS
查看>>
php paypal rest api,PayPal REST API指定网络配置文件PHP
查看>>
php pcntl 多进程学习
查看>>
PHP pcntl_fork不能在web服务器中使用的变通方法
查看>>
php private ,public protected三者的区别
查看>>
php PSR规范
查看>>
php rand() 重复,array_rand()函数从另外一个数组中随机取得的一定数量的数组的元素是否会重复?...
查看>>
php redis pub/sub(Publish/Subscribe,发布/订阅的信息系统)之基本使用
查看>>
php redis 集群扩展类文件
查看>>
php redis(2)
查看>>
PHP Redis分布式锁
查看>>
php session超时时间_php怎么设置session超时时间
查看>>
PHP SOAP模块的使用方法:NON-WSDL模式
查看>>
php Socket通信
查看>>
PHP SPL标准库-迭代器
查看>>
PHP Static延迟静态绑定
查看>>
php str_pad();
查看>>
PHP study 环境变量composer
查看>>
PHP trim() 函数
查看>>
php unicode编码转成unioce字符(中文)
查看>>