考虑以下代码:
one.c:
#include <stdio.h>
int one() {
printf("one!\n");
return 1;
}
Run Code Online (Sandbox Code Playgroud)
two.c:
#include <stdio.h>
int two() {
printf("two!\n");
return 2;
}
Run Code Online (Sandbox Code Playgroud)
prog.c中
#include <stdio.h>
int one();
int two();
int main(int argc, char *argv[])
{
one();
two();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想将这些程序链接在一起.所以我这样做:
gcc -c -o one.o one.c
gcc -c -o two.o two.c
gcc -o a.out prog.c one.o two.o
Run Code Online (Sandbox Code Playgroud)
这很好用.
或者我可以创建一个静态库:
ar rcs libone.a one.o
ar rcs libtwo.a two.o
gcc prog.c libone.a libtwo.a
gcc -L. prog.c -lone -ltwo
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:为什么我会使用第二个版本 - 我创建了一个".a"文件 - 而不是链接我的".o"文件?它们似乎都是静态链接,所以它们之间是否有优势或架构差异?
根据这个答案,它应该打印所有函数名称:
[root@ test]# cat hw.c
#include <stdio.h>
int func(void)
{
return 1;
}
int main(void)
{
func();
printf("%d",6);
return 6;
}
[root@ test]# gcc -Wall hw.c -o hw -finstrument-functions
[root@ test]# ./hw
6
[root@ test]# gcc --version
gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-48)
Copyright (C) 2006 Free Software Foundation, Inc.
Run Code Online (Sandbox Code Playgroud)
但为什么它不适合我呢?