gcc 编译

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 ")
}

/* 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. ", 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