gcc main.c -o main
gcc main.c -o ./output/main
compile
gcc -c main.c -o main.o
link
gcc main.o -o main.out
###############
preprocess .c--->.i
compile .i---> .s
assemble .s---> .o
link .o--->bin
##############
-E
-S
-c
-o
-I directory
-g
-save-temps
####
-c
/* func.c */
#include <stdio.h>
void func_a()
{
printf("func_a\n")
}
/* main.c */
#include <stdio.h>
int main(void)
{
func_a();
func_b();
return 0;
}
$ gcc -c func.c main.c >>> no error
$ gcc func.c main.c >>> link error
####
-E
macro expansion
file include
gcc -E main.c -o main.i
// -C
//prevent preprocess from deleting comments
gcc -E -C main.c -o main.i
#####
-S
$ gcc -S main.c
// variable name used comment in .s
$ ggc -S -fverbose-asm main.c
####
-l
libc.a .a represent achieve
libc.so .so share object
/* main.c */
#include <stdio.h>
#include <math.h>
#define PI 3.415926
int main()
{
double param, result;
param = 60.0;
result = cos(param *PI /180.0);
printf("The consine of %f degrees is %f. \n", param, result);
return 0;
}
//standard library
// libm.a prefix lib suffix .a
$ gcc main.c -o main.out -lm
// other directory library
1. used as object file
$ gcc main.c -o main.out /usr/lib/libm.a
2. -Lpath
$ gcc main.c -o main.out -L/uar/lib -lm
3.
$ add path to environment LIBRARYPATH
##########
gcc main.o func.o -o app.out -lm
########
file type
.c :
.h : head file
.i :
.s : assemble
.S : has c command, need to preprocess
########
generate .so
-shared
-fPIC // Position-Independent Code
$ gcc -fPIC -shared func.c -o libfunc.so
$ gcc -fPIC -c func.c -o func.o // -fPIC used in compile stage
$ gcc -shared func.o -o libfunc.so
$ gcc main.c libfunc.so -o app.out
转载于:https://www.cnblogs.com/youngvoice/p/11575307.html
本文详细解析了GCC编译器的整个编译流程,从预处理到链接阶段,包括源文件如何转换为可执行文件的具体步骤。介绍了宏展开、变量名使用、注释处理等细节,并展示了如何使用GCC参数来控制编译过程,如-fPIC用于生成位置独立代码。同时,文章还讲解了如何链接标准库和外部库,以及如何生成共享库。

5608

被折叠的 条评论
为什么被折叠?



