网站平台建设什么意思/关键词文案生成器
文章目录
- 前言
- Using C++ on Linux in VS Code
- vscode中编译过程配置
- vscode中调试配置
- vscode中C/C++配置
- 实际案例
- vscode调试的其他问题
前言
在Linux中,我习惯用vscode写C++代码。
先手动创建工程。
➜ tree
.
├── doc # 文档目录
├── README.md # 介绍
└── src # 源码目录
我们在src目录中添加一个main.cpp
和CMakeLists.txt
。
// main.cpp
#include <iostream>using namespace std;int main(void)
{cout<<"Hello World"<<endl;
}
cmake_minimum_required(VERSION 3.5)project (main)add_executable(${PROJECT_NAME} main.cpp)
此时,我们来编译代码。
cd src
mkdir build && cd build
cmake ..
make
此时,我们如何使用vscode的调试功能呢? 我不咋会用gdb,我需要使用vscode的调试功能。
即,vscode中,通过cmake->make编译的代码,如何调试。
Using C++ on Linux in VS Code
我们需要阅读vscode的配置文档:Using C++ on Linux in VS Code
vscode中编译过程配置
编译这一节,我用不到。顺带看下吧。我推荐直接在命令行进行编译。
从主菜单中,选择终端>配置默认构建任务。将出现一个下拉列表,显示 C++ 编译器的各种预定义构建任务。选择C/C++: g++ build active file。
此时在当前项目的根目录中,出现了.vscode/tasks.json
文件。内容如下:
{"version": "2.0.0","tasks": [{"type": "cppbuild","label": "C/C++: g++ 生成活动文件","command": "/usr/bin/g++","args": ["-fdiagnostics-color=always","-g","${file}","-o","${fileDirname}/${fileBasenameNoExtension}"],"options": {"cwd": "${fileDirname}"},"problemMatcher": ["$gcc"],"group": {"kind": "build","isDefault": true},"detail": "编译器: /usr/bin/g++"}]
}
command设置指定要运行的程序;在这种情况下是 g++。该args数组指定将传递给 g++ 的命令行参数。这些参数必须按照编译器预期的顺序指定。
这个任务告诉g++获取活动${file}文件。生成文件放入该文件去除后缀名。
该label值是您将在任务列表中看到的;你可以随意命名它。
对象中的"isDefault": true值指定当您按Ctrl+Shift+B group时将运行此任务。此属性仅为方便起见;如果您将其设置为 false,您仍然可以从 Terminal 菜单中使用Tasks: Run Build Task运行它。
当然,我们可以修改tasks.json
,使得一键编译。
修改方式一,来自:vscode + cmake编译环境配置
{// See https://go.microsoft.com/fwlink/?LinkId=733558// for the documentation about the tasks.json format"version": "2.0.0","tasks": [{"label": "make build",//编译的项目名,build,更改"type": "shell","command": "cd ./src/build ;cmake ../ ;make",//编译命令,更改"group": {"kind": "build","isDefault": true}},{"label": "clean","type": "shell","command": "make clean",}]
}
点击中断->运行任务…,便可以选择不同的任务(label标记)运行。
修改方式二,来自:使用VSCODE生成调试Linux下C++的CMake项目。这个修改,比上一个好。
{"version": "2.0.0","tasks": [{"label": "mkdir","type": "shell","command": "mkdir build -p"},{"label": "cmake","type": "shell","command": "cmake -DCMAKE_BUILD_TYPE=debug ..","dependsOn": ["mkdir"], "options": {"cwd": "${workspaceFolder}/build" },},{"label": "make","type": "shell","command": "make -j4","options": {"cwd": "${workspaceFolder}/build" },}]
}
vscode中调试配置
这是我们关注的小节。
接下来,您将创建一个launch.json文件来配置 VS Code,以便在您按F5调试程序时启动 GDB 调试器。
从主菜单中,选择**Run > Add Configuration…**然后选择C++ (GDB/LLDB)。
VS Code 创建一个launch.json文件,在编辑器中打开它。如下所示:
{// 使用 IntelliSense 了解相关属性。 // 悬停以查看现有属性的描述。// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387"version": "0.2.0","configurations": []
}
右键,或者点击右下角的“添加配置” --> 添加gdb调试。此时的配置文件内容有所增加,如下所示:
{// 使用 IntelliSense 了解相关属性。 // 悬停以查看现有属性的描述。// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387"version": "0.2.0","configurations": [{"name": "(gdb) 启动","type": "cppdbg","request": "launch","program": "输入程序名称,例如 ${workspaceFolder}/a.out","args": [],"stopAtEntry": false,"cwd": "${fileDirname}","environment": [],"externalConsole": false,"MIMode": "gdb","setupCommands": [{"description": "为 gdb 启用整齐打印","text": "-enable-pretty-printing","ignoreFailures": true},{"description": "将反汇编风格设置为 Intel","text": "-gdb-set disassembly-flavor intel","ignoreFailures": true}]}]
}
在上面的 JSON 中,program指定要调试的程序。此处其默认设置为项目根目录下的a.out文件。
默认情况下,C++ 扩展不会向您的源代码添加任何断点,并且该stopAtEntry值设置为false.
stopAtEntry将值更改为true以使调试器main在您开始调试时停止该方法。
setupCommands: 设置GDB或LLDB的命令的JSON数组。
所以,我们暂时只需要修改"program"
。
{// 使用 IntelliSense 了解相关属性。 // 悬停以查看现有属性的描述。// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387"version": "0.2.0","configurations": [{"name": "(gdb) 启动","type": "cppdbg","request": "launch","program": "${fileDirname}/build/${fileBasenameNoExtension}",// "program": "${workspaceFolder}/build/result","args": [],"stopAtEntry": false,"cwd": "${fileDirname}","environment": [],"externalConsole": false,"MIMode": "gdb","setupCommands": [{"description": "为 gdb 启用整齐打印","text": "-enable-pretty-printing","ignoreFailures": true},{"description": "将反汇编风格设置为 Intel","text": "-gdb-set disassembly-flavor intel","ignoreFailures": true}]}]
}
vscode中C/C++配置
我们会在src
目录的外面,建立一个include
目录,来放置头文件。
有时候,vscode不大聪明:源码中包含了对应的头文件,但编辑的时候,下方会有红色的波浪线,提示未定义。有时候,这个挺讨厌的。我们需要添加它的头文件扫描路径。
您可以创建一个c_cpp_properties.json文件,该文件将允许您更改设置,例如编译器的路径、包含路径、C++ 标准(默认为 C++17)等等。
您可以通过运行命令C/C++: Edit Configurations (UI) from the Command Palette ( Ctrl+Shift+P ) 来查看 C/C++ 配置 UI。
我们来看下c_cpp_properties.json
中的内容。
{"configurations": [{"name": "Linux","includePath": ["${workspaceFolder}/**"],"defines": [],"cStandard": "c11","cppStandard": "c++14","intelliSenseMode": "linux-clang-x64"}],"version": 4
}
可以在"includePath"
中添加一些路径。
实际案例
调试openssl
{"version": "0.2.0","configurations": [{"name": "gdb-opnessl","type": "cppdbg","request": "launch","program": "/home/dacao/work/openssl-learn/openssl/apps/openssl", // 被调试程序的绝对路径"args": ["dgst", "-sign", "./tmp/private_key.pem", "-sha256", "-out", "./tmp/rsa-sign.dat", "./tmp/plain.dat"],"stopAtEntry": true,"cwd": "/home/dacao/work/openssl-learn", // 进入的目录"environment": [{"name":"LD_LIBRARY_PATH","value":"/home/dacao/work/openssl-learn/openssl:$LD_LIBRARY_PATH"}],"console":"none","MIMode": "gdb","setupCommands": [{"description": "Enable pretty-printing for gdb","text": "-enable-pretty-printing","ignoreFailures": true}]}]
}
vscode调试的其他问题
- 调试特权程序
- 第一种方法是vscode用root启动。root权限打开VScode 、Electron杂谈 - 以VS Code为例
- 第二种是,gdb启动的时候,使用sudo。vscode下gdb调试需要root权限问题解决、ow to debug programs with “sudo” in VSCODE