在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的执行情况:

[[email protected] 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
[[email protected] hello]# make        #执行make
g++ -c -g -o hello.o hello.cc
g++ hello.o -o hello
[[email protected] 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
[[email protected] hello]# ./hello          #执行hello文件
hello VS Code                                #hello的执行输出
[[email protected] hello]# make clean       #执行make clean清除中间文件
rm -f *.o
[[email protected] 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】,点击调试按钮即可进行项目调试了。

原文地址:https://www.cnblogs.com/lenmom/p/9193388.html

时间: 2024-07-30 08:47:18

在CentOS 7中使用VS Code编译调试C++项目的相关文章

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

最近项目需求,需要在Linux下开发C++相关项目,经过一番摸索,简单总结了一下如何通过VS Code进行编译调试的一些注意事项. 关于VS Code在Linux下的安装这里就不提了,不管是CentOS还是Ubuntu,如果不懂且搜Q足够的情况下,你会解决的. 一. 前置知识——gcc/g++的编译链接过程 在Windows下,如果你用Visual Studio进行开发,C/C++的编译器一般采用微软提供的MSBuild:在Linux下C/C++的编译器大多采用gcc/g++.既然要在Linux

CentOS 7中Nginx1.9.5编译安装教程systemctl启动

先安装gcc 等 yum -y install gcc gcc-c++ wget 复制代码 .然后装一些库 yum -y install gcc wget automake autoconf libtool libxml2-devel libxslt-devel perl-devel perl-ExtUtils-Embed pcre-devel openssl-devel 复制代码 进入默认的软件目录 cd /usr/local/src/ 复制代码 下载 nginx软件 wget http://

使用ng serve 在Vs Code里调试 anguar 项目

ng serve 是@angular/cli 下的运行命令 我们可以通过下载@angular/cli来生成angular2的模板 npm install -g @angular/cli ng new Anguar-Example --默认会下载依赖包 cd Angura-Example npm install --如果下载了依赖包这步可以不做 ng serve --运行angular 或 npm start 运行项目 默认通过http://localhost:4200/ 访问. 如果我们想用VS

[Cordova] 无法编译Visual Studio项目里Plugin副本的Native Code

[Cordova] 无法编译Visual Studio项目里Plugin副本的Native Code 问题情景 开发Cordova Plugin的时候,开发的流程应该是: 建立Cordova Plugin 发布到本机文件系统或是Git服务器 使用Visual Studio挂载Plugin 编译并执行项目 在这个开发的过程中,如果在编译并执行项目的这个步骤,发现Plugin的Native Code需要修正.直觉的想法,会是直接修改Cordova项目里Plugin副本的Native Code之后,再

Centos 7 安装 Visual stdio Code

最近微软正式发布了.net code 和asp.net code.尝试了下在linux下.net code和asp.net code使用. 具体怎么使用.net code 和asp.net code 请大家阅读大内老A写的“通过几个Hello World感受.NET Core全新的开发体验". 这里主要写在Centos 7 安装 Visual stdio Code. 环境参数: 操作系统版本:CentOS-7-x86_64-1511 软件版本:visual stdio code 1.2 操作步骤

如何在 CentOS 7 中安装、配置和安全加固 FTP 服务

步骤 1:安装 FTP 服务器 1. 安装 vsftpd 服务器很直接,只要在终端运行下面的命令. # yum install vsftpd 2. 安装完成后,服务先是被禁用的,因此我们需要手动启动,并设置在下次启动时自动启用: # systemctl start vsftpd # systemctl enable vsftpd 3. 接下来,为了允许从外部系统访问 FTP 服务,我们需要打开 FTP 守护进程监听的 21 端口: # firewall-cmd --zone=public --p

在一个未知的CentOS服务器中如何加上PHP的openssl扩展

1. 服务器是定制过的,不知对应的centos版本: 2. PHP是自己编译的,而且服务器上没有保留对应版本的源代码,通过/pathto/php -v 找出php版本号,然后wget去下载对应的php源码包: 3. 加压代码,到源码的ext/openssl目录下,使用phpize的方式进行编译环境的配置,大致步骤如下: Cannot find config.m4. Make sure that you run '/usr/local/bin/phpize' in the top level so

【转载】如何在 Ubuntu 15.04/CentOS 7 中安装 Lighttpd Web 服务器

Lighttpd 是一款开源 Web 服务器软件.Lighttpd 安全快速,符合行业标准,适配性强并且针对高配置环境进行了优化.相对于其它的 Web 服务器而言,Lighttpd 占用内存更少:因其对 CPU 占用小和对处理速度的优化而在效率和速度方面从众多 Web 服务器中脱颖而出.而 Lighttpd 诸如 FastCGI.CGI.认证.输出压缩.URL 重写等高级功能更是那些面临性能压力的服务器的福音. 以下便是我们在运行 Ubuntu 15.04 或 CentOS 7 Linux 发行

CentOS 6.6 下源码编译安装MySQL 5.7.5

版权声明:转自:http://www.linuxidc.com/Linux/2015-08/121667.htm 说明:CentOS 6.6 下源码编译安装MySQL 5.7.5 1. 安装相关工具# yum -y install gcc-c++ ncurses-devel cmake make perl \ gcc autoconf automake zlib libxml libgcrypt libtool bison2. 清理环境检查boost版本: # rpm -qa boost*卸载b