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\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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值