最全最新:用vscode和MinGW 开发c/c++程序,vscodemingw
最全最新:用vscode和MinGW 开发c/c++程序,vscodemingw
第一步,下载vscode,在插件库中搜索关键字c,选择第一个插件进行安装。https://code.visualstudio.com/
第二步,下载MinGW,并在环境变量中将MInGW bin文件夹配置进系统环境变量。https://osdn.net/projects/mingw/releases/
下面的几步如果喜欢看英文的话推荐看微软官方的英文文档。https://code.visualstudio.com/docs/languages/cpp
如果不喜欢的话,就听我慢慢道来
用vscode打开一个文件夹,这个文件夹就是我们的工程文件夹(这个文件夹必须是英文路径,因为后面安装的插件不支持中文路径)
打开vscode命令行 (点击左下角设置按钮,第一个选项 Command Palette) 输入 C/Cpp:Edit Configurations 然后创建出来 c_cpp_properties.json 文件 我们编辑这个文件 成为下图的格式
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "D:\\MinGW\\bin\\gcc.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
其中,比较关键的参数就是 "includePath" 这个键,这个是标识你工程所用的头文件所在的地方,这里就是打开的工程文件夹。
还有一个比较关键的参数是 "compilerPath": "D:\\MinGW\\bin\\gcc.exe" 这个参数是标识你编译器的位置
剩下的基本不用改,"intelliSenseMode": "clang-x64",这个参数我说一下,是设置自动 智能感知补全的
有人会问,那stdio.h像这种库的头文件还要一个一个添加至"includePath"中吗? 答案是不用,
因为"compilerPath" 指定MinGW为默认编译器之后,这个c插件就会自动找到MInGW下的include目录,就不用手动再添加了,添加的只是自定义的头文件。
继续在命令行输入Tasks: Configure Tasks 生成 task.json 这个是构建项目时用的(就是用来生成.exe控制台程序的),将文件改成如下格式
{
"version": "2.0.0",
"tasks": [
{
"label": "build hello world",
"type": "shell",
"command": "g++",
"args": [
"helloworld.cpp", "-g","-o","helloworld.exe"
]
}
]
}
其中args是g++ 构建时的命令行,"args": [ "helloworld.cpp", "-g","-o","helloworld.exe"] 第一项是编译项,“-g” 是提供调试信息,为了能进行debbuger,“-o”指定编译连接后生成的文件 这里指定的是helloworld.exe
在命令行输入Tasks: Run Build Task 然后添加 group信息到task,json 中,task.json内容如下所示
{
"version": "2.0.0",
"tasks": [
{
"label": "build hello world",
"type": "shell",
"command": "g++",
"args": [
"helloworld.cpp",
"-g",
"-o",
"helloworld.exe"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
在打开文件夹下新建一个helloworld.cpp(Ctrl+N 并保存至 打开的文件夹下面)
#include<stdio.h>
#include<stdlib.h>
int main()
{
printf("Hello World!");
system("pause");
}
在Debug下面选一个Launcher (选择C++ (GDB/LLDB)这一项)生成launch.json 将文件改成如下格式
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/helloworld.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "D:\\mingw\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build hello world"
}
]
}
其中,miDebuggerPath这一项 是写的你的调试器路径;preLaunchTask这一项对应 task.json 中的label一项(因为调试是在构建完成之后,要不你调试啥)。“program”指定要调试的文件,就是构建生成的.exe文件
ok,一切都完成了,点击debug调试按钮,开始调试运行
注意多文件编译时只需要修改task.json(构建文件)文件中的args参数就可以了,这里举一个例子
{
"version": "2.0.0",
"tasks": [
{
"label": "build hello world",
"type": "shell",
"command": "g++",
"args": [
"source/helloworld.cpp",
"source/main.cpp",
"-g",
"-o",
"main.exe"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
args前两个是要编译链接的 文件,"-o"指定生成的文件是main.exe
好了,给个github地址 里面有多文件编译的实例 https://github.com/ZhangZhiShuo/vscode-MinGW- 解压即可
相关文章
- 暂无相关文章
用户点评