在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 ;
}
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) Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 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中输入如下内容:
hello:hello.o
g++ hello.o -o hello #按照makefile 语法,前面的不是空格,而是tab键,否则构建会失败
hello.o:hello.cc
g++ -c -g -o hello.o hello.cc #按照makefile 语法,前面的不是空格,而是tab键,否则构建会失败
clean:
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
输入以下内容:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [ {
"name": "C++ Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/hello",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"preLaunchTask": "build",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
其中第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】,点击调试按钮即可进行项目调试了。
在CentOS 7中使用VS Code编译调试C++项目的更多相关文章
- 【转载】在Linux中使用VS Code编译调试C++项目
原文:在Linux中使用VS Code编译调试C++项目 最近项目需求,需要在Linux下开发C++相关项目,经过一番摸索,简单总结了一下如何通过VS Code进行编译调试的一些注意事项. 关于VS ...
- 【菜鸟玩Linux开发】在Linux中使用VS Code编译调试C++项目
最近项目需求,需要在Linux下开发C++相关项目,经过一番摸索,简单总结了一下如何通过VS Code进行编译调试的一些注意事项. 关于VS Code在Linux下的安装这里就不提了,不管是CentO ...
- 在Linux中使用VS Code编译调试C++项目
最近项目需求,需要在Linux下开发C++相关项目,经过一番摸索,简单总结了一下如何通过VS Code进行编译调试的一些注意事项. 关于VS Code在Linux下的安装这里就不提了,不管是CentO ...
- 解决VS Code编译调试中文输出乱码
最近尝试用VS Code配置了C和C++的编译调试环境,结果遇到了中文输出乱码问题,查阅网上竟然还没有相关问题,有怀疑是mingw中文支持问题,但最后证明是VS Code编码问题. 解决方案: 文件- ...
- [vim配置]windows下在vim中使用gcc/g++编译调试c/cpp文件
在Linux里面混了一个多月,vim编程用得甚爽.无奈前天将Linux里面的编程文件夹误删,而技术不精无法找回,悲痛欲绝.再者,无限怀念windows里面的游戏,并觉得现在在Linux里面也学不到什么 ...
- CentOS 7中Nginx1.9.5编译安装教程systemctl启动
先安装gcc 等 yum -y install gcc gcc-c++ wget 复制代码 .然后装一些库 yum -y install gcc wget automake autoconf libt ...
- 在MVC中如何使用vs2013编译调试razor cshtml
打开mvc项目的csproj文件: <MvcBuildViews>false</MvcBuildViews> 改为 <MvcBuildViews>true</ ...
- [Cordova] 无法编译Visual Studio项目里Plugin副本的Native Code
[Cordova] 无法编译Visual Studio项目里Plugin副本的Native Code 问题情景 开发Cordova Plugin的时候,开发的流程应该是: 建立Cordova Plug ...
- Linux下C++/C的编译调试
这几天因为任务的原因我需要在ubuntu下编写程序.因此恶补了许多linux程序编写的知识.我分以下几个方面总结我所学的知识. gcc,g++,make命令的使用 gdb 调试 VScode的使用 c ...
随机推荐
- three.js入门——先跑个旋转的正方体
WebGl中文网看了几篇教程,又百度了几篇文章,顿时感觉手痒,打开编辑器,写个demo玩玩. demo是写在vue项目中的,所以首先: npm install three --save; npm in ...
- 从JDK源码角度看Byte
Java的Byte类主要的作用就是对基本类型byte进行封装,提供了一些处理byte类型的方法,比如byte到String类型的转换方法或String类型到byte类型的转换方法,当然也包含与其他类型 ...
- SAP NetWeaver Business Client (NWBC) 简介
1.NWBC 简介 SAP NetWeaver Business Client (NWBC) 是新一代SAP用户界面,集成了SAPGUI事务和新的web dynpro应用,类似于桌面应用程序. SAP ...
- U-Boot 不能识别FAT32 SD分区
/********************************************************************* * U-Boot 不能识别FAT32 SD分区 * 说明: ...
- Centos6.x搭建lnmp环境
查看系统版本 #cat /etc/redhat-release CentOS release 6.7 (Final) 配置静态ip #vi /etc/sysconfig/network-scripts ...
- JavaScript:逻辑操作符“==”与“===”的区别
Summary JavaScript中,逻辑操作符“===”会先检查操作数的数据类型,对不同的数据类型会返回false. 而“==”对不同类型的操作数进行比较时,会进行类型转换后再比较. Descri ...
- BZOJ1001 BeiJing2006 狼抓兔子 【网络流-最小割】*
BZOJ1001 BeiJing2006 狼抓兔子 Description 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的,而且现在的兔子还比较 ...
- python 客户端点对点通信小案例
点对点通讯分为客户端和服务器,多个客户端通过服务器进行信息的交流 服务器端代码 service端 #!/usr/bin/env python # -*- coding:utf-8 -*- impor ...
- c++ 中 毫秒级时间获取
#include <time.h> clock_t start,ends; start=clock(); Sleep(); ends=clock(); cout<<ends-s ...
- android 自己定义checkbox 背景图无效的问题
http://blog.csdn.net/zuolongsnail/article/details/7106586 正常的定义能够參考这个网址 可是我參考它以后发现我执行时候 根本不工作嘛 结果 ...