gcc编译选项的-g -pg和-l

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chengqiuming/article/details/88383619

一 -g选项

1 点睛

选项-g可以产生供gdb调试用的的可执行文件,即可执行文件中包含供gdb调试器进行调试所需要的信息,因此,加了这个选项后,产生可执行文件尺寸要大些。 

2 实战

2.1 源代码

#include <stdio.h>
int main()
{
        bool b = false;
        printf("hello, boy \n" );  
        return 0;
}

2.2 编译比较,带了-g选项编译出的可执行文件大些

[root@localhost test]# gcc test.cpp -g -o test
[root@localhost test]# gcc test.cpp -o testNodebuginfo
[root@localhost test]# ll
total 28
-rwxr-xr-x. 1 root root 9600 Mar 10 20:35 test
-rw-r--r--. 1 root root  122 Mar 10 08:07 test.cpp
-rwxr-xr-x. 1 root root 8512 Mar 10 20:35 testNodebuginfo

二 -pg选项

1 点睛

选项-pg能产生供gprof剖析用的可执行文件。gprof是Linux下对C++程序进行性能分析的工具。

既然在可执行文件中加入了性能分析所需的信息,那么可执行程序的尺寸肯定变大了,但比加了-g后还要小些。

2 实战 

2.1 源代码

#include <stdio.h>
int main()
{
        bool b = false;
        printf("hello, boy \n" );  
        return 0;
}

2.2 编译比较大小

[root@localhost test]# gcc test.cpp -g -o test
[root@localhost test]# gcc test.cpp -o testNodebuginfo
[root@localhost test]# gcc test.cpp -pg -o testWithPginfo
[root@localhost test]# ll
total 28
-rwxr-xr-x. 1 root root 9600 Mar 10 20:35 test
-rw-r--r--. 1 root root  122 Mar 10 08:07 test.cpp
-rwxr-xr-x. 1 root root 8512 Mar 10 20:35 testNodebuginfo
-rwxr-xr-x. 1 root root 8728 Mar 10 20:43 testWithPginfo

三 -l选项

1 点睛

选项-l可以用来链接共享库(动态链接库)。共享库是可执行程序运行时候需要用到的一个函数库,可执行程序调用的函数是这个库里实现的,因此需要链接它。-l的用法是后面直接加动态库的名字,比如编译一个实用C++标准库内容的程序,则需要链接C++标准库。

gcc test.cpp -lstdc++ -o test

-lstdc++是C++标准库的名字,它和l之间没空格。

猜你喜欢

转载自blog.csdn.net/chengqiuming/article/details/88383619