cpp: vscode 配置C,C++开发环境 (windows)

下载编译器MinGW并解压
官网页面:https://www.mingw-w64.org/
下载页面:https://sourceforge.net/projects/mingw-w64/files/

https://cmake.org/download/

安装插件“Doxygen Documentation Generator”,用来生成注释。
                        安装插件”C/C++ Snippets”,用来生成文件头、代码块分割线等。或 KoroFileHeader
                        C/C++ Snippets插件设置

 

需要环境变量进行设置 (图为WIN10)

 

 

settings.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
{
  "C_Cpp_Runner.cCompilerPath": "gcc",
  "C_Cpp_Runner.cppCompilerPath": "g++",
  "C_Cpp_Runner.debuggerPath": "gdb",
  "C_Cpp_Runner.cStandard": "c11",
  "C_Cpp_Runner.cppStandard": "c++11",
  "C_Cpp_Runner.msvcBatchPath": "",
  "C_Cpp_Runner.useMsvc": false,
  "C_Cpp_Runner.warnings": [
    "-Wall",
    "-Wextra",
    "-Wpedantic",
    "-Wshadow",
    "-Wformat=2",
    "-Wcast-align",
    "-Wconversion",
    "-Wsign-conversion",
    "-Wnull-dereference"
  ],
  "C_Cpp_Runner.msvcWarnings": [
    "/W4",
    "/permissive-",
    "/w14242",
    "/w14287",
    "/w14296",
    "/w14311",
    "/w14826",
    "/w44062",
    "/w44242",
    "/w14905",
    "/w14906",
    "/w14263",
    "/w44265",
    "/w14928"
  ],
  "C_Cpp_Runner.enableWarnings": true,
  "C_Cpp_Runner.warningsAsError": false,
  "C_Cpp_Runner.compilerArgs": [],
  "C_Cpp_Runner.linkerArgs": [],
  "C_Cpp_Runner.includePaths": [],
  "C_Cpp_Runner.includeSearch": [
    "*",
    "**/*"
  ],
  "C_Cpp_Runner.excludeSearch": [
    "**/build",
    "**/build/**",
    "**/.*",
    "**/.*/**",
    "**/.vscode",
    "**/.vscode/**"
  ],
  "C_Cpp_Runner.useAddressSanitizer": false,
  "C_Cpp_Runner.useUndefinedSanitizer": false,
  "C_Cpp_Runner.useLeakSanitizer": false,
  "C_Cpp_Runner.showCompilationTime": false,
  "C_Cpp_Runner.useLinkTimeOptimization": false
}

c_cpp_properties.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
  "configurations": [
    {
      "name": "windows-gcc-x64",
      "includePath": [
        "${workspaceFolder}/**"
      ],
      "compilerPath": "C:/GC/mingw64/bin/gcc.exe",
      "cStandard": "${default}",
      "cppStandard": "${default}",
      "intelliSenseMode": "windows-gcc-x64",
      "compilerArgs": [
        ""
      ]
    }
  ],
  "version": 4
}

  

 

 

 

 系统无设置UNICode 用以下配置的GBK编码 

task.json:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc.exe 生成活动文件",    //和preLaunchTask一致
            "command": "C:\\GC\\mingw64\\bin\\gcc.exe", //让g++执行 的.c文件变为.exe文件
            "args": [
                "-fdiagnostics-color=always",
                "-g"//调试的选项
                "${file}",    //当执行哪一个文件,编译器就解释那个文件。
                "-finput-charset=UTF-8",
                "-fexec-charset=GBK", //解决输出中文问题
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"  //生成exe文件
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}

  

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc.exe 生成活动文件",    //和preLaunchTask一致
            "command": "gcc", //让g++执行 的.c文件变为.exe文件 用可以用: C:\\GC\\mingw64\\bin\\gcc.exe
            "args": [
                "-fdiagnostics-color=always",
                "-g"//调试的选项
                "${file}",    //当执行哪一个文件,编译器就解释那个文件。
                "-std=c11",
                "-finput-charset=UTF-8",
                "-fexec-charset=GBK", //解决输出中文问题
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"  //生成exe文件
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc.exe 生成活动文件",    //和preLaunchTask一致
            "command": "C:\\CU\\mingw64\\bin\\gcc.exe", //让g++执行 的.c文件变为.exe文件
            "args": [
                "-fdiagnostics-color=always",
                "-g"//调试的选项
                //"${file}",    //当执行哪一个文件,编译器就解释那个文件。
                "*.c"//liunx windows
                "-std=c11",
                "-finput-charset=UTF-8",
                "-fexec-charset=GBK", //解决输出中文问题 WIN10系统区域设置,如果是勾了UNICODE,这里配置UTF-8
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"  //生成exe文件
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}

  

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc.exe 生成活动文件",    //和preLaunchTask一致
            "command": "gcc", //让g++执行 的.c文件变为.exe文件 用可以用: C:\\GC\\mingw64\\bin\\gcc.exe
            "args": [
                "-fdiagnostics-color=always",
                "-g"//调试的选项
                //"${file}",    //当执行哪一个文件,编译器就解释那个文件。
                "${workspaceFolder}/*.c"//*.c 需要当前指定的文件至.c的文件编译所有.c的文件  ${workspaceFolder}/*.c 在当前选择其他JSON文件时,它也会编译 https://stackoverflow.com/questions/71924077/configuring-task-json-and-launch-json-for-c-in-vs-code
                "-std=c11",
                "-finput-charset=UTF-8",
                "-fexec-charset=GBK", //解决输出中文问题
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"  //生成exe文件
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}

  

lanuch.json:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
{
  "configurations": [
    {
      "name": "C/C++: gcc.exe 生成和调试活动文件",
      "type": "cppdbg",
      "request": "launch",
      "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${fileDirname}",
      "environment": [],
      "externalConsole": true,
      "MIMode": "gdb",
      "miDebuggerPath": "C:\\GC\\mingw64\\bin\\gdb.exe",
      "setupCommands": [
        {
          "description": "为 gdb 启用整齐打印",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        },
        {
          "description": "将反汇编风格设置为 Intel",
          "text": "-gdb-set disassembly-flavor intel",
          "ignoreFailures": true
        }
      ],
      "preLaunchTask": "C/C++: gcc.exe 生成活动文件"
    },
    {
      "name": "C/C++ Runner: Debug Session",
      "type": "cppdbg",
      "request": "launch",
      "args": [],
      "stopAtEntry": false,
      "externalConsole": true,
      "cwd": "g:/cpp/testcpp",
      "program": "g:/cpp/testcpp/build/Debug/outDebug",
      "MIMode": "gdb",
      "miDebuggerPath": "gdb",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ]
    }
  ],
  "version": "2.0.0"
}

  

 

helloword.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
 
 
int main()
{
    ////SetConsoleOutputCP(65001); 
    printf("hello word\n");
    printf("你好,中囯\n");
    system("pause");
    return 0;
 
 
}

  

如果操作系统区域设置不一样,配置文件设置不一样。

 

tasks.json:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc.exe 生成活动文件",    //和preLaunchTask一致
            "command": "C:\\GC\\mingw64\\bin\\gcc.exe", //让g++执行 的.c文件变为.exe文件
            "args": [
                "-fdiagnostics-color=always",
                "-g"//调试的选项
                "${file}",    //当执行哪一个文件,编译器就解释那个文件。
                "-finput-charset=UTF-8",
                "-fexec-charset=UTF-8", //解决输出中文问题GBK 这和操作系统的环境有关
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"  //生成exe文件
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}

  

 

 

 

 

posted @   ®Geovin Du Dream Park™  阅读(120)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2007-09-10 ajax +asp.net2.0 sqlserver2005 三级联
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示