GCC的基本操作

来源:互联网 发布:电脑管家软件管理 编辑:程序博客网 时间:2024/06/05 06:10
查看gcc版本号
gcc -v

指定查找头文件的路径
1.    gcc main.c math.c -Wall -I./
cd /1114
mkdir include
mkdir src
touch Makefile

cd src/
touch main.c
touch math.c
touch math.h

vi math.c
int add(int a, int b)
{
    return a+b;
}
int sub(int a, int b)
{
    return a-b;
}
int dive(int a, int b)
{
    return a/b;
}
int mul(int a, int b)
{
    return a*b;
}
:wq

vi math.h
#ifndef MATH_H_
#define MATH_H_
int add(int a, int b);
int sub(int a, int b);
int dive(int a, int b);
int mul(int a, int b);
#endif
:wq

vi main.c
#include<stdio.h>
#include <math.h>
int main(void)
{
    int a=6, b=3;
    printf("res=%d\n", add(a, b));
    printf("res=%d\n", sub(a, b));
    printf("res=%d\n", dive(a, b));
    printf("res=%d\n", mul(a, b));
    return 0;
}
:wq

gcc main.c math.c -Wall -I./
./a.out
rm a.out


2.    gcc src/main.c src/math.c -I./include
cd /1114/src
mv math.h ../include/
cd ..
tree
gcc src/main.c src/math.c -I./include
./a.out
rm a.out


3.    gcc -I./include src/*.c -o app
cd /1114
vi Makefile
all:
    gcc -I./include src/*.c -o app
clean:
    rm app
:wq
tree
make
ls
./app
make clean
tree


只编译,生成.O文件,不进行链接
gcc -c main.c
gcc -c math.c

cd /1114/src
gcc -c main.c
ls
file main.o
gcc -c math.c
ls
gcc main.0 math.o -o app
./app

cd ..
ls
vi Makefile
all:
    gcc -c src/main.c
    gcc -c src/math.c
    gcc main.o math.o -o app
clean:
    rm -f src/*.o
    rm -f *.o
    rm -f app
make
tree
make clean
tree
rm src/app
tree
make
./app

包含调试信息
gcc main.c math.c -g -o app

cd /1114/src/
ls
gcc main.c math.c -g -o app
gcc main.c math.c -o app1
ls -l
gdb app1
list
quit
gdb app
list
quit
ls -l


查看文件的反汇编
objdump -dSsx app
objdump -dSsx app > file

cd /1114/src
rm app1
ls
objdump -dSsx app
objdump -dSsx app > file
vi file

编译优化,数字越大优化得越多
gcc -O0 *.c -o app1
gcc -O3 *.c -o app2

cd /1114/src
ls
rm app
rm file
gcc -O0 *.c -o app1
gcc -O3 *.c -o app2
ls -l

提示更多警告信息
gcc *.c -Wall
gcc *.c -Wall -I../include

cd /1114/src
vi main.c
#include<stdio.h>
#include <math.h>
int main(void)
{
    int a=6, b=3;
    printf("res=%d\n", add(a, b));
    printf("res=%d\n", sub(a, b));
    int c=10;
    printf("res=%d\n", dive(a, b));
    printf("res=%d\n", mul(a, b));
    printf("c=%d\n", c);
    return 0;
}
:wq
gcc *.c -Wall
gcc *.c -Wall -I../include

编译时定义宏
gcc *.c -Wall -DDEBUG -I../includ

cd /1114/src
vi main.c
#include<stdio.h>
#include <math.h>
#define DEBUG 1
int main(void)
{
    int a=6, b=3, c=10;

    printf("res=%d\n", add(a, b));
    printf("res=%d\n", sub(a, b));
    printf("res=%d\n", dive(a, b));
    printf("res=%d\n", mul(a, b));

#ifdef DEBUG
    printf("c=%d\n", c);
#endif
    return 0;
}
:wq
gcc *.c -Wall -I../include
./a.out

vi main.c
#include<stdio.h>
#include <math.h>
int main(void)
{
    int a=6, b=3, c=10;

    printf("res=%d\n", add(a, b));
    printf("res=%d\n", sub(a, b));
    printf("res=%d\n", dive(a, b));
    printf("res=%d\n", mul(a, b));

#ifdef DEBUG
    printf("c=%d\n", c);
#endif
    return 0;
}
:wq
gcc *.c -Wall -DDEBUG -I../include
./a.out

生成预处理文件
gcc -E main.c
gcc -E main.c > file

cd /1114/src
gcc -E main.c
gcc -E main.c > file
vi file
0 0
原创粉丝点击