至此,与动态库连接的函数编译成了一个可执行文件。貌似成功了,但还差最后一步。如果直接运行该程序,会给出这样的错误信息:
error while loading shared libraries: libhello.so:
cannot open shared object file: No such file or directory
这是因为与动态库连接的程序在运行时,首先将该动态库加载到内存中,而gcc默认加载动态库文件所在目录为/usr/local/lib, /usr/lib。刚才的程序虽然能编译成功,但如果我们自己建立的动态库没有位于默认目录中,则执行时会应为无法找到它而失败。
解决办法:改变加载路径对应的环境变量,然后再执行。
export LD_LIBRARY_PATH=动态库所在目录:$LD_LIBRARY_PATH
查看archive内容
$ ar tv archiveNAME
t : 显示archive中member的内容,若不指定member,则列出所有。
v : 与t结合使用时,显示member的详细信息。
要想进了解ar的详细选项,参考ar的on-line manual
nm
nm用来列出目标文件中的符号,可以帮助程序员定位和分析执行程序和目标文件中的符号信息和它的属性。
如果没有目标文件作为参数传递给nm, nm假定目标文件为a.out.
这里用一个简单的示例程序来介绍nm的用法:
main.c:
int main(int argc, char *argv[])
{
hello();
bye();
return 0;
}
hello.c:
void hello(void)
{
printf("hello!\n");
}
bye.c:
void bye(void)
{
printf("good bye!\n");
}
运行下列命令:
$ gcc -Wall -c main.c hello.c bye.c
gcc生成main.o, hello.o, bye.o三个目标文件(这里没有声明函数原型,加了-Wall,gcc会给出警告)
$ nm main.o hello.o bye.o
结果显示如下:
main.o:
U bye
U hello
00000000 T main
hello.o:
00000000 T hello
U puts
bye.o:
00000000 T bye
U puts
结合这些输出结果,以及程序代码,可以知道:
对于main.o, bye和hello未被定义, main被定义了
对于hello.o, hello被定义了, puts未被定义
对于bye.o, bye被定义了,puts未被定义
几个值得注意的问题:
(1)"目标文件"指.o文件, 库文件, 最终的可执行文件
.o : 编译后的目标文件,即含有最终编译出的机器码,但它里面所引用的其他文件中函数的内存位置尚未定义.
(2)如果用nm查看可执行文件, 输出会比较多, 仔细研究输出, 可以对nm用法有更清醒的认识.
(3)在上述hello.c, bye.c中, 调用的是printf(), 而nm输出中显示调用的是puts(), 说明最终程序实际调用的puts(), 如果令hello.c或bye.c中的printf()使用格式化输出,则nm显示调用printf(). ( 如: printf("%d", 1); )
$ readelf -h hello
ELF Header:
Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
Class: ELF32
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: EXEC (Executable file)
Machine: Intel 80386
Version: 0x1
Entry point address: 0x80482c0
Start of program headers: 52 (bytes into file)
Start of section headers: 3848 (bytes into file)
Flags: 0x0
Size of this header: 52 (bytes)
Size of program headers: 32 (bytes)
Number of program headers: 7
Size of section headers: 40 (bytes)
Number of section headers: 34
Section header string table index: 31
注意: readelf只能用于ELF格式目标文件, 且选项中至少要指定一个(除V, H外)的选项!
gprof
gprof被用来测量程序的性能. 它记录每个函数被调用的次数以及相应的执行时间. 这样就能锁定程序执行时花费时间最多的部分, 对程序的优化就可集中于对它们的优化.
用一个简单的数值计算程序来掩饰gprof的用法:
collatz.c:
#include
/* Computes the length of Collatz sequences */
unsigned int step (unsigned int x)
{
if (x % 2 == 0)
{
return (x / 2);
}
else
{
return (3 * x + 1);
}
}
unsigned int nseq (unsigned int x0)
{
unsigned int i = 1, x;
if (x0 == 1 || x0 == 0)
return i;
x = step (x0);
while (x != 1 && x != 0)
{
x = step (x);
i++;
}
return i;
}
int main (void)
{
unsigned int i, m = 0, im = 0;
for (i = 1; i m)
{
m = k;
im = i;
printf ("sequence length = %u for %u\n", m, im);
}
}
return 0;
}
先将collatz.c编译成目标文件collatz.o, gcc通过 -pg选项来打开gprof支持:
$ gcc -Wall -c -pg collatz.c
$ gcc -Wall -pg -o collatz collatz.o
注意:两条命令都要加 "-pg"选项。前一条命令生成collatz.o目标文件。后一条命令生成可执行文件,该可执行文件中包含了记录函数执行时间的指令。
生成collatz可执行文件后,现执行它,结果与一般程序的执行无疑。但此时在PWD目录生成一个名为"gmon.out"的文件,gprof通过它来分析程序的执行。
如果不现执行程序,而直接用gprof来分析它,会提示“gmon.out: No such file or directory”。
gprof用法: