假设我有以下程序 (hello.c
):
#include <stdio.h>
#include <math.h>
#define NAME "ashoka"
int main(int argc, char *argv[])
{
printf("Hello, world! My name is %s\n", NAME);
}
所以,据我所理解,编译这个程序的过程是:
预处理:将复制粘贴 stdio.h 和 math.h 函数声明,并将
NAME
替换为"ashoka"
。
clang -E hello.c
编译:会将 c 代码转换成汇编代码
clang -S hello.c
文件生成: hello.s
汇编: 将汇编代码转换为目标代码
clang -c hello.s
文件产生:hello.o
链接:将目标文件合并成一个我们将要执行的文件。
clang hello.o -lm
或者(假设我也想链接hello2.o)
clang hello.o hello2.o
所以,以下是问题:
描述的过程是否正确?
在链接阶段中,我们将
.o
(目标代码)文件链接在一起。我知道math.h
位于/usr/include
目录中。那么math.o
在哪里?链接器如何找到它?Linux中的
.a
(静态库)和.so
(动态库)是什么?它们与.o
文件和链接阶段有什么关系?假设我想与世界分享我制作的库。 我有一个
mylib.c
文件,在其中声明并实现了我的函数。 我该如何分享此文件,以便人们可以通过#include <mylib.h>
或#include "mylib.h"
将其包含在他们的项目中?
"
而产生编译错误。 - MikeCAT