VsCode C++环境的搭建,此处不叙述。本文是在环境配置后,实现代码编译,控制台输出。
1.区分gcc和g++
之前使用没有区分两者区分,使用默认gcc导致,在编译c++的时候,报错运行失败,提示json扩展等问题。后来查找后,才清楚gcc主要是处理c代码,g++才是c++.
特性gccg++全称GNU Compiler Collection (C 编译器驱动程序)GNU C++ Compiler (C++ 编译器驱动程序)主要用途默认处理 C 代码默认处理 C++ 代码链接行为默认不自动链接 C++ 标准库自动链接 C++ 标准库 (如 libstdc++)文件后缀识别优先识别 .c 为 C 代码优先识别 .cpp/.cc 为 C++ 代码2.相关json配置
- c_cpp_properties.json
按下 Ctrl+Shift+P,输入 C/C++: Edit Configurations (UI)。
自动生成 .vscode/c_cpp_properties.json
- {
- "configurations": [
- {
- "name": "Win32",
- "includePath": [
- "${workspaceFolder}/**"
- ],
- "defines": [
- "_DEBUG",
- "UNICODE",
- "_UNICODE"
- ],
- "compilerPath": "C:/Program Files/mingw64/bin/g++.exe",
- "cStandard": "c17",
- "cppStandard": "c++17",
- "intelliSenseMode": "windows-gcc-x64"
- }
- ],
- "version": 4
- }
复制代码
- tasks.json(编译配置)
按下 Ctrl+Shift+P,输入 Tasks: Configure Default Build Task → 选择 g++.exe。
修改生成的 .vscode/tasks.json:
- {
- "version": "2.0.0",
- "tasks": [
- {
- "label": "Build HelloWorld", // 必须与 launch.json 中的 preLaunchTask 一致
- "type": "shell",
- "command": "C:/Program Files/mingw64/bin/g++.exe",
- "args": [
- "-fdiagnostics-color=always",
- "-g",
- "${file}",
- "-o",
- "${fileDirname}/${fileBasenameNoExtension}.exe" // 修复1:路径分隔符改为 `/`
- ],
- "options": {
- "cwd": "${workspaceFolder}" // 修复2:工作目录设为项目根目录
- },
- "problemMatcher": [
- "$gcc"
- ],
- "group": {
- "kind": "build",
- "isDefault": true
- },
- "detail": "调试器生成的任务。"
- }
- ]
- }
复制代码
- launch.json(调试配置)
点击左侧边栏的 Run and Debug 图标(或按 Ctrl+Shift+D)。
点击顶部下拉菜单中的 create a launch.json file(若首次配置)
在弹出的选项中选择 C++ (GDB/LLDB)。
- {
- // 使用 IntelliSense 了解相关属性。
- // 悬停以查看现有属性的描述。
- // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
- "version": "0.2.0",
- "configurations": [
- {
- "name": "(gdb) 启动",// 自定义调试名称
- "type": "cppdbg",
- "request": "launch",
- "program": "${fileDirname}/${fileBasenameNoExtension}.exe",// 路径分隔符改为 `/`
- "args": [],
- "stopAtEntry": false,
- "cwd": "${workspaceFolder}",
- "environment": [],
- "externalConsole": true, // Windows 推荐 true(显示控制台输出)
- "MIMode": "gdb",
- "miDebuggerPath": "C:/Program Files/mingw64/bin/gdb.exe", // 检查路径是否正确
- "preLaunchTask": "Build HelloWorld" // 必须与 tasks.json 中的 label 一致
- }
- ]
- }
复制代码 注意:vscode自动生成的模板后,有些配置需要手动修改
3.执行运行调试
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |