在CentOS 7中使用VS Code编译调试C++项目

1. 安装VSCODE

见VSCode官方链接 https://code.visualstudio.com/docs/setup/linux#_rhel-fedora-and-centos-based-distributions

 先下载yum源

sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
sudo sh -c 'echo -e "[code]\nname=Visual Studio Code\nbaseurl=https://packages.microsoft.com/yumrepos/vscode\nenabled=1\ngpgcheck=1\ngpgkey=https://packages.microsoft.com/keys/microsoft.asc" > /etc/yum.repos.d/vscode.repo'

安装VSCODE

yum check-update    #更新yum源
yum -y install code   #安装VSCode

2. 安装GCC

yum -y install gcc gcc-g++ 

3. C/C++编译过程

假设我们有如下代码hello.cc需要进行编译

#include <iostream>
using namespace std;

int main() {
     cout << "Hello, VS Code!" << endl;
     return 0;
 }

GCC编译器按照编译->链接两步来生成应用程序。其中编译生成的结果是.o文件,链接会生成可执行程序或静态/动态库文件,在linux中为.a, .sa, .la为后缀的文件,可执行文件在linux中可以没有后缀,如果没有特别指定,默认为a.out.

3.1 编译hello.cc

g++ -c hello.cc

输出结果是一个hello.o文件,这是编译过程的生成的中间文件。-c 表示只编译,不链接

3.2 链接hello.o生成hello.out

g++ -o hello.out hello.o

其中-o 表示生成的目标文件的名称,如果不指定,默认的文件名为a.out,生成的,目标文件可以没有后缀,也就是说以下命令也是正确的

g++ -o hello hello.o

当然,如果第1、2步是可以合并执行,直接执行命令

g++ -o hello.out hello.cpp

3.3 运行hello.out

 ./hello.out

输出如下: 

Hello, VS Code!

4. 构建项目

4.1 安装make

Linux中,构建项目要用到make,先确认make已经安装,在控制台输入如下指令:

make -v

如果已经安装make,则会输出make的版本信息

GNU Make 3.82
Built for x86_64-redhat-linux-gnu
Copyright (C) 2010  Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

否则,就没有安装make,要安装make,使用以下命令:

yum -y install cmake

4.2 准备构建脚本

在当前项目根目录下,输入

vi makefile

在makefile中输入如下内容:

1 hello:hello.o  
2         g++ hello.o -o hello             #按照makefile 语法,前面的不是空格,而是tab键,否则构建会失败
3 hello.o:hello.cc  
4         g++ -c -g -o hello.o hello.cc    #按照makefile 语法,前面的不是空格,而是tab键,否则构建会失败
5 clean: 
6         rm -f *.o                        #按照makefile 语法,前面的不是空格,而是tab键,否则构建会失败

输入:wq保存退出.

解释一下makefile的语法,

target ... : prerequisites ...
  command    #注意前面是tab,而不是空格

target是一个目标文件,可以是Object File,也可以是执行文件,还可以是一个标签;

prerequisites是要生成那个target所需要的文件或是目标;

command是make需要执行的命令(任意的Shell命令)。

说白了就是target这一个或多个目标,依赖于prerequisites列表中的文件,其执行规则定义在command里。如果prerequisites列表中文件比target要新,就会执行command,否则就跳过。这就是整个make过程的基本原理。

注意第3行中的 -g参数,在生成hello.o文件过程中,g++命令中 -g 表示生成的文件是可调试的,如果没有-g,调试时无法命中断点

在默认情况下,只需输入make,则发生了以下行为:

a. make在当前目录下找名为makefile或Makefile的文件;

b. 如果找到,它会找文件中的第一个target,如上述文件中的build,并作为终极目标文件;

c. 如果第一个target的文件不存在,或其依赖的.o 文件修改时间要比target这个文件新,则会执行紧接着的command来生成这个target文件;

d. 如果第一个target所依赖的.o文件不存在,则会在makefile文件中找target为.o的依赖,如果找到则执行command,.o的依赖必是.h或.cpp,于是make可以生成 .o 文件了

e. 回溯到b步执行最终目标

测试一下makefile的执行情况:

[root@lenmomDesktop hello]# ls -l      #查看执行前的文件列表,只有两个文件 hello.cc makefile 
total 8
-rw-rw-r-- 1 lenmom lenmom 174 Jun 17 17:05 hello.cc    
-rw-rw-r-- 1 lenmom lenmom 115 Jun 17 17:43 makefile
[root@lenmomDesktop hello]# make        #执行make
g++ -c -g -o hello.o hello.cc
g++ hello.o -o hello
[root@lenmomDesktop hello]# ls -l            #查看make之后的文件列表,发现多了hello和hello.o两个文件
total 56
-rwxr-xr-x 1 root root 21128 Jun 17 20:27 hello
-rw-rw-r-- 1 lenmom lenmom 174 Jun 17 17:05 hello.cc
-rw-r--r-- 1 root root 23896 Jun 17 20:27 hello.o
-rw-rw-r-- 1 lenmom lenmom 115 Jun 17 17:43 makefile
[root@lenmomDesktop hello]# ./hello          #执行hello文件
hello VS Code                                #hello的执行输出
[root@lenmomDesktop hello]# make clean       #执行make clean清除中间文件
rm -f *.o
[root@lenmomDesktop hello]# ls -l            #查看执行clean之后的文件列表,发现hello.o已经没有了
total 32
-rwxr-xr-x 1 root root 21128 Jun 17 20:27 hello
-rw-rw-r-- 1 lenmom lenmom 174 Jun 17 17:05 hello.cc
-rw-rw-r-- 1 lenmom lenmom 115 Jun 17 17:43 makefile

5. vscode调试

5.1 安装gdb

yum -y install gdb

5.2 创建launch.json

mkdir ./.vscode
vi  ./.vscode/launch.json

输入以下内容:

 1 {
 2     // Use IntelliSense to learn about possible attributes.
 3     // Hover to view descriptions of existing attributes.
 4     // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
 5     "version": "0.2.0",
 6     "configurations": [
 7 
 8         {
 9             "name": "C++ Launch",
10             "type": "cppdbg",
11             "request": "launch",
12             "program": "${workspaceFolder}/hello",
13             "args": [],
14             "stopAtEntry": false,
15             "cwd": "${workspaceFolder}",
16             "environment": [],
17             "externalConsole": false,
18             "MIMode": "gdb",
19             "preLaunchTask": "build",  
20             "setupCommands": [
21                 {
22                     "description": "Enable pretty-printing for gdb",
23                     "text": "-enable-pretty-printing",
24                     "ignoreFailures": true
25                 }
26             ]
27         }
28     ]
29 }

其中第12行,表示启动的程序的名称,本例中build之后的输出文件为hello。

第19行,build表示在启动调试之前,要做的任务,显然在调试之前应该编译工程,也就是要make 执行以下makefile,产生最新的项目输出。

所以我们还要创建一个构建任务的Json文件,其中任务名称为build,这个任务被launch引用,也就是第19行中的build的含义。

vi  ./.vscode/tasks.json

输入以下内容:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "reveal": "always",  
    "tasks": [
        {
            "args": ["-f", "makefile"],  
            "label":"build",
            "type": "shell",
            "command": "make"
        }
    ]
}

这个task的意思是,在shell命令行中执行make  -f   makefile

接下来在vscode中选择C++ Launch【launch.json文件中的name】,点击调试按钮即可进行项目调试了。

猜你喜欢

转载自www.cnblogs.com/lenmom/p/9193388.html