VsCode配置C、C++环境编写运行C、C++(Windows)

本文详细介绍如何在Windows环境下使用VsCode配置C/C++及Opencv开发环境,无需安装MinGW,通过设置.vscode文件夹下的四个配置文件,实现代码智能感知、编译及调试。

VsCode配置C、C++ opencv环境/编写运行C、C++(Windows)

这个方法不用安装MinGW

1.配置基本文件

首先在文件夹下新建.vscode文件,然后往里面放四个文件,具体的代码如下

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZCtqHCDW-1586364065084)(assets/1586363530833.png)]

.vscode\c_cpp_properties.json

第7,8行换成自己的opencv配置环境

第十六行换成自己的visual studio Hostx86/x64/cl.exe 路径

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "D:/Program Files/opencv/build/include",
                "D:/Program Files/opencv/build/include/opencv2"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.17763.0",
            "compilerPath": "D:/Program Files/visual studio 2019/VC/Tools/MSVC/14.24.28314/bin/Hostx86/x64/cl.exe",
            "intelliSenseMode": "msvc-x64"
        }
    ],
    "version": 4
}

.vscode\c_cpp_properties.json

这里的"program": "${fileDirname}\\main.exe"可以自己指定生成的exe的位置和名称

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "cl.exe debug",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "${fileDirname}\\main.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
        },

        {
            "name": "cl.exe build and debug active file",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "${fileDirname}\\main.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "preLaunchTask": "cl.exe build active file"
        }
    ]
}

.vscode\settings.json

配置一些需要的头文件

{
    "files.associations": {
        "fstream": "cpp",
        "iostream": "cpp",
        "vector": "cpp",
        "cmath": "cpp",
        "xstring": "cpp",
        "array": "cpp",
        "deque": "cpp",
        "initializer_list": "cpp",
        "list": "cpp",
        "queue": "cpp",
        "type_traits": "cpp",
        "xhash": "cpp",
        "xtree": "cpp"
    },
    "python.pythonPath": "D:\\Anaconda3\\python.exe"
}

.vscode\tasks.json

同样这里的17-21行要换成自己的opencv地址

{
// 有关 tasks.json 格式的文档,请参见
    // https://go.microsoft.com/fwlink/?LinkId=733558
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "cl.exe build active file",
            "command": "cl.exe",
            "args": [
                "/Zi",
                "/EHsc",
                "/Fe:",
                "${fileDirname}\\main.exe",
                "twosum.cpp",
                "/I",
                "D:/Program Files/opencv/build/include",
                "/I",
                "D:/Program Files/opencv/build/include/opencv2",
                "/link",
                "D:/Program Files/opencv/build/x64/vc15/lib/opencv_world410.lib"
            ],
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "type": "shell",
            "label": "cl.exe build active file",
            "command": "cl.exe",
            "args": [
                "/Zi",
                "/EHsc",
                "/Fe:",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "${file}"
            ],
            "problemMatcher": [
                "$msCompile"
            ],
            "group": "build"
        }
    ]
}

2.编译

注意,在这个地方,首先要通过x86 x64 Cross Tools Command Prompt for VS 2019 这个打开,才能有vs环境,然后输入文件的地址,输入code,就自动打开vscode。

在这里插入图片描述

然后在左侧选第三个,run这里选第二个,点击三角形符号,就可以运行成功了。大功告成!

s Command Prompt for VS 2019 这个打开,才能有vs环境,然后输入文件的地址,输入code,就自动打开vscode。

[外链图片转存中…(img-Vp9K4QjR-1586364065086)]

然后在左侧选第三个,run这里选第二个,点击三角形符号,就可以运行成功了。大功告成!

在这里插入图片描述

### VSCode 配置 C/C++ 运行环境教程 为了在 Visual Studio Code (VSCode)配置 C/C++ 开发环境以便运行 C 语言代码,以下是详细的说明: #### 安装与基础设置 首先需要下载并安装最新版本的 VSCode[^3]。完成后启动程序,在扩展市场中搜索 **C/C++** 插件(由 Microsoft 提供),点击安装按钮将其添加到编辑器环境中。 #### 工具链准备 对于 Windows 用户来说,推荐使用 MinGW-w64 或者 MSYS2 来获取 GCC 编译器支持。可以通过访问官方网站或者通过其他可信渠道来下载这些工具包,并按照指示完成它们的本地化部署过程[^1]。 #### 设置编译任务 打开任意文件夹作为工作区后,按下 `Ctrl+Shift+B` 组合键触发构建操作提示;如果这是首次执行,则会询问关于创建 tasks.json 文件的选择——请选择 “Create tasks.json file”,接着选取适合自己的平台选项比如 "Others" 自定义命令行参数。 这里给出一个简单的例子用于演示如何指定 gcc 命令来进行源码转换为目标可执行形式的过程: ```json { "version": "2.0.0", "tasks": [ { "label": "build hello world", "type": "shell", "command": "gcc", "args": [ "-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe" ], "group": { "kind": "build", "isDefault": true }, "problemMatcher": ["$gcc"] } ] } ``` 上述脚本片段展示了当用户希望利用 GNU Compiler Collection 处理当前活动文档(`${file}`)时应采取的具体措施[^2]。 #### 调试功能启用 除了能够顺利地生成最终产物之外,还可能期望实现断点调试等功能。这同样依赖于先前提到过的插件所提供的辅助机制。具体做法涉及修改 launch.json 文件中的相关内容项如下所示: ```json { "configurations": [ { "name": "(gdb) Launch", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/your_program.exe", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": true, "MIMode": "gdb", "miDebuggerPath": "/path/to/gdb", // For windows users, this could be something like 'C:\\MinGW\\bin\\gdb.exe' "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "build hello world", "internalConsoleOptions": "openOnSessionStart" } ] } ``` 注意调整 `"miDebuggerPath"` 的路径指向实际存在的 GDB 可执行档位置。 至此,整个流程结束,现在应该可以在 VSCode 上面愉快地编写、测试以及优化您的 C 程序啦! ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值