手动运行gcc的步骤,编译,组装,链接
如果你有一个简单的C程序,比如
int main(void) {return 0;}
它可以使用gcc -o test test.c
编译。
据我所知,gcc执行编译,组装然后链接。 后两个步骤是通过运行as
和ld
来实现的。
我可以使用gcc -S test.c
生成汇编代码。
你会在终端中键入什么,将汇编代码转换为可执行文件?
(这样做的原因是学习assembly)
这些是使用gcc的不同阶段
gcc -E --> Preprocessor, but don't compile gcc -S --> Compile but don't assemble gcc -c --> assemble but don't link gcc with no switch will link your object files and generate the executable
gcc test.s -o test
将为你编译test.s
的test
。
NASM可能也值得你花时间 – 它可能比gcc
编译汇编更容易/更友好。
执行gcc -S -o test.s test.c
,键入gcc -o test test.s
您可以让gcc
在任何地方启动和停止编译过程。 gcc test.s -o test
将把它从汇编编译成一个可执行文件。
您可能知道或者可能不知道,编译的四个阶段是预处理(-E),编译到汇编(-S),汇编到目标代码(-c),最后链接。 我最难弄明白的是如何使用预处理器输出。 这是怎么做的:
gcc -E hello.c | gcc -S -xc -o hello.s - gcc -c hello.s -o hello.o gcc hello.o -o hello