gcc 编译器使用指南

参考:
https://b23.tv/tWqKrC
我的linux版本如下:
Linux version 5.4.0-88-generic (buildd@lgw01-amd64-008) (gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04))
使用命令:

cat /proc/version

即可查看

安装

sudo apt update
# 安装编译器和调试器
sudo apt install build-essential gdb
# 安装完成后,确认是否成功
gcc --version
g++ --version
gdb --version

准备test.cpp

mkdir test
cd test
touch test.cpp
vim test.cpp
# 设置tab缩进
:set tabstop=4

然后写代码:

#include<iostream>
using namespace std;
int main()
{
    
    
    cout << "test" << endl;
    return 0;
}

编译

在实际使用中,使用gcc指令编译c代码,g++指令编译c++代码。
一般cpp文件需要通过四个过程生成可执行文件:
预处理、编译、汇编、链接

# 预处理 .cpp -> .i
# -E 表示编译器只对输入文件进行预处理
g++ -E test.cpp -o test.i
# 编译 .i -> .s
# -S 表示在c++代码产生了汇编语言文件后停止编译,产生的汇编语言文件为 .s
g++ -S test.i -o test.s
#  汇编 .s -> .o
# -c g++仅把源代码编译为机器语言的目标代码
g++ -c test.s -o test.o
# 链接 .o -> bin文件
# -o 为将来产生的可执行文件用指定的文件名
g++ test.o -o test 

以上的步骤也可以直接用一个步骤代替:

g++ test.cpp -o test

现在test文件夹里面是这样:

drwxrwxrwx 2 dyy dyy  4096 1019 14:44 ./
drwxrwxrwx 8 777 dyy  4096 1019 13:54 ../
-rwxrwxr-x 1 dyy dyy 17320 1019 14:44 test*
-rw-rw-r-- 1 dyy dyy    91 1019 14:38 test.cpp

运行它: 成功

dyy@dyy-Lenovo-ThinkBook-14-IIL:~/Desktop/test$ ./test 
test

g++ 编译参数

-g :编译带调试信息的可执行文件

# -g 选项告诉 GCC 产生能被 GNU 调试器GDB使用的调试信息,以调试程序。
g++ -g test.cpp

-O[n] :开启优化

# -O 选项告诉 g++ 对源代码进行基本优化。这些优化在大多数情况下都会使程序执行的更快。 -
# -O0 表示不做优化
# -O1 为默认优化
# -O2 除了完成-O1的优化之外,还进行一些额外的调整工作,如指令调整等。
# -O3 则包括循环展开和其他一些与处理特性相关的优化工作。
g++ -O2 test.cpp

-l 和 -L :指定库文件 | 指定库文件路径

# -l参数(小写)就是用来指定程序要链接的库,-l参数紧接着就是库名
# 在/lib和/usr/lib和/usr/local/lib里的库直接用-l参数就能链接

# 链接glog库
g++ -lglog test.cpp

# 如果库文件没放在上面三个目录里,需要使用-L参数(大写)指定库文件所在目录
# -L参数跟着的是库文件所在的目录名
# 链接mytest库,libmytest.so在/home/bing/mytestlibfolder目录下
g++ -L/home/bing/mytestlibfolder -lmytest test.cpp

-I :指定头文件搜索目录

# -I
# /usr/include目录一般是不用指定的,gcc知道去那里找,但 是如果头文件不在/usr/icnclude
# 里我们就要用-I参数指定了,比如头文件放在/myinclude目录里,那编译命令行就要加上-
# I/myinclude 参数了,如果不加你会得到一个”xxxx.h: No such file or directory”的错
# 误。-I参数可以用相对路径,比如头文件在当前 目录,可以用-I.来指定。上面我们提到的–cflags参
# 数就是用来生成-I参数的。
g++ -I/myinclude test.cpp

-Wall 和 -w:打印警告信息 | 关闭警告信息

# 打印出gcc提供的警告信息
g++ -Wall test.cpp
# 关闭警告信息
g++ -w test.cpp

-std=c++11 :设置编译标准

# 使用c++11的标准编译 test.cpp
g++ -std=c++11 test.cpp

-o :指定输出文件名

# 若不指定,生成文件为a.out
g++ test.cpp -o test

-D :定义宏

# 常用场景:
# -DDEBUG 定义DEBUG宏,可能文件中有DEBUG宏部分的相关信息,用个DDEBUG来选择开启或关闭
# DEBUG
g++ -DDEBUG debugTest.cpp -o debugTest

比如:

#include<stdio.h>
using namespace std;
int main()
{
    
    
    #ifdef DEBUG
        printf("DEBUG LOG\n");
    #endif
        printf("in\n");
}

我们编译运行:

g++ -DDEBUG debugTest.cpp -o debugTest
dyy@dyy-Lenovo-ThinkBook-14-IIL:~/Desktop/test$ ./debugTest 
DEBUG LOG
in

实例演示

test项目录如下:

dyy@dyy-Lenovo-ThinkBook-14-IIL:~/Desktop/GCCTEST$ tree
.
├── include
│   └── swap.h
├── main.cpp
└── src
    └── swap.cpp

2 directories, 3 files

main.cpp内容

#include<iostream>
#include<swap.h>
using namespace std;
int main()
{
    
    
    int val1 = 10;
    int val2 = 20;

    cout << "before" << endl;
    cout << val1 << " " << val2 << endl;
    swap(val1, val2);
    cout << "after" << endl;
    cout << val1 << " " << val2 <<endl;
    return 0; 
}

swap.h内容

#ifndef _swap_h_
#define _swap_h_
#include<stdio.h>
void swap(int& a, int& b);
#endif

swap.cpp内容

#include"swap.h"
void swap(int& a, int& b)
{
    
    
  int tmp = a;
  a = b;
  b = tmp;
}    

直接编译

最简单编译运行

# 将 main.cpp src/Swap.cpp 编译为可执行文件
g++ main.cpp src/Swap.cpp -Iinclude
# 运行可执行文件
dyy@dyy-Lenovo-ThinkBook-14-IIL:~/Desktop/GCCTEST$ ./a.out 
before
10 20
after
20 10

增加编译参数运行

g++ main.cpp src/swap.cpp -Iinclude -std=c++11 -O2 -Wall -o b.out

生成库文件并编译

首先尝试生成静态库

cd src
# 汇编,生成swap.o文件
g++ swap.cpp -c -I../include
# 生成静态库libswap.a
ar rs libswap.a swap.o
cd ..
# 链接 生成可执行文件staticmain
g++ main.cpp -Iinclude -Lsrc -lswap -o staticmain

现在观察一下项目文件:

dyy@dyy-Lenovo-ThinkBook-14-IIL:~/Desktop/GCCTEST$ tree
.
├── include
│   └── swap.h
├── main.cpp
├── src
│   ├── libswap.a
│   ├── swap.cpp
│   └── swap.o
└── staticmain

2 directories, 6 files
dyy@dyy-Lenovo-ThinkBo

现在尝试动态链接

cd src
# 生成动态库
g++ swap.cpp -I../include -fPIC -shared -o libswap.so
# 上面的命令等同于以下两条合起来:
# g++ swap.cpp -I../include -c -fPIC
# g++ -shared -o libswap.so swap.o
cd ..
# 链接,生成可执行文件
g++ main.cpp -Iinclude -Lsrc -lswap -o sharemain

现在观察一下项目文件:

dyy@dyy-Lenovo-ThinkBook-14-IIL:~/Desktop/GCCTEST$ tree
.
├── include
│   └── swap.h
├── main.cpp
├── sharemain
├── src
│   ├── libswap.a
│   ├── libswap.so
│   ├── swap.cpp
│   └── swap.o
└── staticmain

运行可执行文件

# 运行静态链接的可执行文件
dyy@dyy-Lenovo-ThinkBook-14-IIL:~/Desktop/GCCTEST$ ./staticmain 
before
10 20
after
20 10
# 运行含有动态链接的可执行文件
dyy@dyy-Lenovo-ThinkBook-14-IIL:~/Desktop/GCCTEST$ ./sharemain 
./sharemain: error while loading shared libraries: libswap.so: cannot open shared object file: No such file or directory
# 指定一下动态库目录然后再运行
dyy@dyy-Lenovo-ThinkBook-14-IIL:~/Desktop/GCCTEST$ LD_LIBRARY_PATH=src ./sharemain 
before
10 20
after
20 10

猜你喜欢

转载自blog.csdn.net/qq_42604176/article/details/120845248