1. Vscode 安装
在 Ubuntu Software
中搜索 vscode,然后安装即可。
2. 常用快捷键总结:
ctrl + shitf + p # 打开指令窗口
ctrl + shitf + d # 打开调试窗口
ctrl + F # 查找
ctrl + H # 替换
3. 插件工具
//必装(建议必装,可以代码编译,开发,调试有关)
C++ Intellisense:C++ 语法智能感知
C/C++:C/C++ 执行环境
Chinese (Simplified) Language Pack for Visual Studio Code:汉化工具
Code Runner
CMake Tools
CMake
//选装(有助于提高开发效率)
Prettier - Code formatter:语法格式化
Bracket Pair Colorizer : 括号高亮
highlight-words : 词组高亮
run in Terminal :终端插件
better Aligin :符号对齐插件
4. 环境配置【生成c_cpp_properties.json 文件】
ctrl + shift + p
进入控制台,输入 C/C++:Edit Configurations
即可生成一个 c_cpp_properties.json
文件。
c_cpp_properties.json文件主要是设置系统级的大环境,基本上不用改(除非有第三方库,后面会说)
{
"configurations": [
{
"name": "Linux", //有三个选项可供选择. Linux, Mac, 和 Win32
//在搜索包含的标头时 IntelliSense 引擎要使用的路径列表。
//对这些路径的搜索不是递归搜索。指定 "**" 可指示递归搜索。
//例如: "${workspaceFolder}/**" 将搜索所有子目录,
//而 "${workspaceFolder}" 将不搜索所有子目录。
"includePath": [
"${workspaceFolder}/**"
],
//分析文件时 IntelliSense 引擎要使用的预处理器定义的列表。
//(可选)使用 = 设置值,例如 VERSION=1
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
//正在使用以便启用更准确的 IntelliSense 的编译器的完整路径,
//例如 /usr/bin/gcc。
"compilerPath": "/usr/bin/gcc",
//用于 IntelliSense 的 C 语言标准的版本。
//注意: GNU 标准仅用于查询设置编译器以获取 GNU 定义,
//并且 IntelliSense 将模拟等效的 C 标准版本。
"cStandard": "gnu17",
//用于 IntelliSense 的 C++ 语言标准的版本。
//注意: GNU 标准仅用于查询设置用来获取 GNU 定义的编译器,
//并且 IntelliSense 将模拟等效的 C++ 标准版本。
"cppStandard": "gnu++14",
//要使用的 IntelliSense 模式,该模式映射到 MSVC、gcc 或 Clang 的平台和体系结构变体。
//如果未设置或设置为 ${default},则扩展将选择该平台的默认值。
//Windows 默认为 windows-msvc-x64,
//Linux 默认为 linux-gcc-x64,
//macOS 默认为 macos-clang-x64。
//仅指定 - 变体(例如 gcc-x64)的 IntelliSense 模式是旧模式,
//它们会根据主机平台自动转换为 -- 变体。
"intelliSenseMode": "linux-gcc-x64"
}
],
//配置文件的版本。此属性由扩展托管。请勿更改它。
"version": 4
}
5. 编译
编译 C++ 文件方法可分为自定义编译和使用插件编译。
5.1. 自定义编译【生成tasks.json 文件】
主要通过 Configure Tasks
来实现。tasks.json
文件相当于 vscode 的 .sh
或 .bat
文件,用来记录一系列操作的宏。
一系列动作,那就可以用来设置 如何编译文件,如何 运行文件,几乎 .sh 能干的都可以干。
设置编译
按 ctrl + shift + P
打开vscode控制台,输入 Tasks: Configure Tasks
,再选择Create tasks.json file from templates
,选择 Others
模板 ,就自动生成了一个 tasks.json
文件,这样你就可以在该文件中编写参数来调整设置。
注意:
因为我安装了汉化工具,所以截图中显示有部分是中文,实际操作时,请中英互译以下。
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
//你的设置文件名,可随便起
"label": "build1111",
//运行task的平台,一般是shell
"type": "shell",
//普通的shell命令,运行你的.sh文件
"command": "bash ./build.sh",
"group": {
//设置为build组,这样当你在vscode中执行build命令时,
"kind": "build",
//就能自动执行"command"中的命令了
"isDefault": true
}
}
]
}
设置完“group”
参数后,就能通过 Tasks: Run Build Task (Ctrl+Shift+B)
来运行该文件夹下的 build.sh
文件(也是你自己新建的)——当然,前提是你已经安装了 cmake
。
一般地,build.sh可以写为(如需变更,可以在其基础上进行优化扩展)。
#!/bin/bash
if [ ! -d "build" ]; then
mkdir build
else
rm -rf build/*
fi
cd build
Local_Dir=$(cd "$(dirname "$0")"; pwd)
echo "Now work at Dir:$Local_Dir"
cmake ..
make
CMakeLists.txt文件内容如下(当然也可以根据你自己的需要改)
#项目名称
project(hello_word)
#代码路径
aux_source_directory(. DIR_TOOT_SRCS)
#dubug 模式
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
#生成可执行的文件
add_executable(hello_word ${DIR_TOOT_SRCS})
&emap; 这样以来,一旦执行 Tasks: Run Build Task (Ctrl+Shift+B)
,就通过该文件夹路径下的 CMakeLists.txt
文件开始通过 CMaker
编译文件,生成的可执行文件位于 build
文件夹内。
vscode调用CMaker来编译C++代码,同样,vscode也能调用 gcc
来编译C++代码,如
{
"version": "2.0.0",
"tasks": [
{
"label": "build hello world",
"type": "shell",
"command": "g++", //改这里
"args": [ //改这里
"-g", "helloworld.cpp"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
5.2. 使用插件编译
vscode 的插件是很多很强大的,在Extensions中搜索 CMake
,CMake Tools
,并 Install(安装)。
直接在控制台按照如下截图顺序操作,插件就能直接在项目文件夹路径下生成一个 CMakeList
文件,你可以自己编辑设置。
然后,按 F7
或 Shirft+F7
就能自动在项目文件夹下新建一个 Build
文件夹,并将生成目标放至Build
文件夹下,非常方便!
CMakeLists.txt——用于组织Cmaker进行编译。
6. 调试(Debug)[生成 launch.json 文件]
vscode 自带调试模块,直接点击左侧边栏的 Debug 图标(Ctrl+Shirft+D)
,再点上方的齿轮图标 configure,就能自动生成 launch.json
文件。
launch.json文件主要用来设置 如何调试。
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
//配置名称;显示在启动配置下拉菜单中。
"name": "(gdb) 启动",
//配置名称;显示在启动配置下拉菜单中。
"type": "cppdbg",
//请求配置类型。可以是“启动”或“附加”
"request": "launch",
//程序可执行文件的完整路径。
"program": "输入程序名称,例如 ${workspaceFolder}/a.out",
//传递给程序的命令行参数。
"args": [],
//可选参数。如果为 true,则调试程序应在目标的入口点处停止。
//如果传递了 processId,则不起任何作用。
"stopAtEntry": false,
//目标的工作目录
"cwd": "${fileDirname}",
//要添加到程序环境的环境变量。
//示例: [ { "name": "config", "value": "Debug" } ],而不是 [ { "config": "Debug" } ]。
"environment": [],
//如果为 true,则为调试对象启动控制台。
//如果为 false,它在 Linux 和 Windows 上会显示在集成控制台中。
"externalConsole": false,
//指示 MIDebugEngine 要连接到的控制台调试程序。
//允许的值为 "gdb"、"lldb"。
"MIMode": "gdb",
//为了安装基础调试程序而执行的一个或多个 GDB/LLDB 命令。
"setupCommands": [
{
"description": "为 gdb 启用整齐打印", //此命令的可选说明。
"text": "-enable-pretty-printing", //要执行的调试命令。
"ignoreFailures": true //如果为 true,应忽略此命令的失败。默认值为 false。
}
],
"preLaunchTask": "build1111" //调试会话开始前要运行的任务。
}
]
}
一般地只需要改 "program"
部分内容改为 项目路径下生成的执行文件 即可。
如果需要调试前重新编译一遍,可以新增一条 "preLaunchTask"
,里面的内容改为 tasks.json
中的label
名称。
接下来的调试方法就和Visual Studio一样了,设置断点,开启调试……
7. 添加外部依赖库
7.1. 代码提示方面
直接改 CMakeLists.txt
,添加相关外部库,具体方法就不说了。编译之后, cmaker
才会在 build
文件下生成 compile_commands.json
文件。
具体使用方法,可以参考《CMakeLists.txt 语法介绍与实例演练》这边文章。
7.2. 软件编译方面
修改CMakeLists.txt文件:
cmake_minimum_required(VERSION 3.0.0)
project(opencvTest VERSION 0.1.0)
find_package( OpenCV REQUIRED ) # 寻找OpenCV库
include_directories( ${OpenCV_INCLUDE_DIRS} ) # 添加头文件
add_executable(opencvTest main.cpp)
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
target_link_libraries( opencvTest ${OpenCV_LIBS} ) # 链接OpenCV库
修改 c_cpp_properties.json
文件中的 browse.path
, includePath
添加你的库的头文件路径,这样就能获得头文件相关的提示了。
{
"configurations": [
{
"name": "Linux",
"browse": {
"path": [
"${workspaceFolder}",
"/usr/local/opencv/include/" //添加头文件路径
],
"limitSymbolsToIncludedHeaders": true
},
"includePath": [
"${workspaceFolder}",
"/usr/local/opencv/include/" //添加头文件路径
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64",
"compileCommands": "${workspaceFolder}/build/compile_commands.json" //添加cmaker生成的compile_commands.json
}
],
"version": 4
}
如果还想直接去看相关库的定义。在 c_cpp_properties.json
文件中的添加 compileCommands
属性,并添加之前 CMaker
生成的 compile_commands.json
文件的相对路径,这样就能自动查看外部库的定义了。
注意:json文件中不允许注释,必须请把注释的部分删除掉。
附录:常见的json变量
${workspaceFolder} //the path of the folder opened in VS Code
${workspaceRootFolderName} //the name of the folder opened in VS Code without any slashes (/)
${file} //the current opened file
${relativeFile} //the current opened file relative to workspaceRoot
${fileBasename} //the current opened file’s basename
${fileBasenameNoExtension} //the current opened file’s basename with no file extension
${fileDirname} //the current opened file’s dirname
${fileExtname} //the current opened file’s extension
${cwd} //the task runner’s current working directory on startup
${lineNumber} //the current selected line number in the active file
${env:Path} //environment variables reference path