window系统下VS Code C语言开发环境配置

165 阅读2分钟

在做electron的桌面端应用,有些部分需要使用c代码。 就准备研究下js调用c的部分。

第一步,使用VSCODE配置c开发环境,成功了。此篇记录配置的过程,备忘。

参考文章

VS Code C语言开发环境配置附图版保姆教程_blog.csdn.net/incredibleeimpact/article/details/10-CSDN博客

VS Code配置C语言开发环境的超详细教程 - 知乎 (zhihu.com)

代码结构

image.png

.vscode\c_cpp_propertise.json

{   
    "configurations": [       
     {          
         "name": "Win32",       
         "includePath": [
         "${workspaceRoot}",               
         "D:/SunnyPrivate/software_forwork/vscode_need/mingw64/include/**",               
         "D:/SunnyPrivate/software_forwork/vscode_need/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++", 
         "D:/SunnyPrivate/software_forwork/vscode_need/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
         "D:/SunnyPrivate/software_forwork/vscode_need/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
         "D:/SunnyPrivate/software_forwork/vscode_need/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
         "D:/SunnyPrivate/software_forwork/vscode_need/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
         "D:/SunnyPrivate/software_forwork/vscode_need/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
    ],      
    "defines": [
        "_DEBUG",               
        "UNICODE",
        "__GNUC__=6",               
        "__cdecl=__attribute__((__cdecl__))"            
        ],           
    "intelliSenseMode": "msvc-x64",
    "browse": {              
        "limitSymbolsToIncludedHeaders": true,
        "databaseFilename": "",
        "path": [                   
            "${workspaceRoot}",                   
            "D:/SunnyPrivate/software_forwork/vscode_need/mingw64/include/**",                   
            "D:/SunnyPrivate/software_forwork/vscode_need/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
            "D:/SunnyPrivate/software_forwork/vscode_need/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
            "D:/SunnyPrivate/software_forwork/vscode_need/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
            "D:/SunnyPrivate/software_forwork/vscode_need/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
            "D:/SunnyPrivate/software_forwork/vscode_need/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
            "D:/SunnyPrivate/software_forwork/vscode_need/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
           ]        
        }       
     }   
  ],   
   "version": 4
}
   

.vscode\launch.json

{  
    "version": "0.2.0",
    "configurations": [     
        {           
            "name": "(gdb) Launch", // 配置名称,将会在启动配置的下拉菜单中显示            
            "type":"cppdbg", // 配置类型,这里只能为cppdbg           
            "request": "launch", //请求配置类型,可以为launch(启动)或attach(附加)          
            "program":"${workspaceFolder}/${fileBasenameNoExtension}.exe", // 将要进行调试的程序的路径
            "args": [], // 程序调试时传递给程序的命令行参数,一般设为空即可            
            "stopAtEntry":false, // 设为true时程序将暂停在程序入口处,一般设置为false            
            "cwd":"${workspaceFolder}", // 调试程序时的工作目录,一般为${workspaceRoot}即代码所在目录workspaceRoot已被弃用,现改为workspaceFolder            
            "environment": [],  
            "externalConsole": true, // 调试时是否显示控制台窗口,一般设置为true显示控制台           
            "MIMode": "gdb",            
            "miDebuggerPath":"D:/SunnyPrivate/software_forwork/vscode_need/mingw64/bin/gdb.exe", // miDebugger的路径,注意这里要与MinGw的路径对应  
            "preLaunchTask": "gcc", // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc      
            "setupCommands": [     
                {                   
                    "description":     "Enable pretty-printing for gdb",                   
                    "text": "-enable-pretty-printing",                   
                    "ignoreFailures": false  
                  }        
               ]     
            }  
         ]
      }

.vscode\tasks.json

{   
     "version": "2.0.0",   
     "command": "gcc", // 注意对应  
     "args":["-g","${file}","-o","${fileBasenameNoExtension}.exe"],    // 编译命令参数
     "problemMatcher": {       
         "owner": "cpp",
         "fileLocation":["relative","${workspaceFolder}"],        
         "pattern": {
         "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
         "file": 1,         
         "line": 2,         
         "column": 3,           
         "severity": 4,            
         "message": 5       
         }  
      }
   }

main.c

#include<stdio.h>
#include<stdlib.h>

int main(){
    printf("hello world! I\' m VSCode\n");
   int a, b, sum;
    printf("input two int nums\n");
    scanf("%d,%d",&a,&b);
    sum = a + b;
    printf("%d+%d=%d\n", a, b, sum);
 
    system("pause");
    return 0;
}

运行步骤

  1. 回到“hello.c”文件,单击鼠标右键选择“Run Code”即可运行代码。
  2. 设置断点后想要调试代码的话呢,点击“Run”菜单下的“Start Debugging”子菜单。在随后出现的弹出框中选择“C++(GDB/LLDB)”,再选择”gcc.exe“。

image.png 或者单击这个小三角。

image.png

运行结果截图

image.png