目录
1. Install a C Compiler
2. Install the C/C Extension in VSCode
3. Create a Project Folder and Files
4. Configure Build Task (tasks.json)
5. Set Up Debugging (launch.json)
6. (Optional) Configure IntelliSense (c_cpp_properties.json)
7. Build and Run
首页 开发工具 VSCode 如何为C设置VSCODE

如何为C设置VSCODE

Jul 30, 2025 am 04:20 AM

安装C 编译器:Windows用户推荐安装MinGW-w64并将其添加到PATH,macOS用户安装Xcode命令行工具,Linux用户通过包管理器安装g ;2. 在VSCode中安装Microsoft提供的C/C 扩展以获得IntelliSense和调试功能;3. 创建项目文件夹结构并编写简单的main.cpp测试代码;4. 配置tasks.json文件定义g 编译任务,指定源文件和输出路径;5. 生成launch.json文件设置调试配置,确保program路径与编译输出一致,并正确设置调试器路径;6. 可选配置c_cpp_properties.json以指定编译器路径、标准和IntelliSense模式;7. 使用Ctrl Shift B构建项目,F5启动调试,确认输出成功显示“Hello, C in VSCode!”,至此完成VSCode的C 开发环境搭建。

How to set up VSCode for C

Setting up VSCode for C development is straightforward, but it requires a few key components: a C compiler, the C/C extension for VSCode, and proper configuration files. Here’s how to get everything working smoothly.

How to set up VSCode for C

1. Install a C Compiler

Before using VSCode, you need a C compiler installed on your system.


2. Install the C/C Extension in VSCode

  1. Open VSCode.
  2. Go to the Extensions view (Ctrl Shift X or Cmd Shift X on Mac).
  3. Search for "C/C " by Microsoft.
  4. Install it.

This extension adds IntelliSense, debugging support, and code navigation.


3. Create a Project Folder and Files

Organize your project like this:

my_cpp_project/
├── .vscode/
│   ├── tasks.json
│   ├── launch.json
│   └── c_cpp_properties.json
└── main.cpp
  • Create the folder and open it in VSCode.
  • Create a simple main.cpp:
    #include <iostream>
    int main() {
        std::cout << "Hello, C   in VSCode!" << std::endl;
        return 0;
    }

4. Configure Build Task (tasks.json)

This tells VSCode how to compile your code.

  1. Press Ctrl Shift P → type "Tasks: Configure Task" → choose "Create tasks.json file from template" → select "Others".
  2. Replace the content with:
{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "g   build",
            "command": "g  ",
            "args": [
                "-g",
                "${workspaceFolder}/*.cpp",
                "-o",
                "${workspaceFolder}/bin/app"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "echo": true,
                "reveal": "always",
                "panel": "new"
            },
            "problemMatcher": ["$gcc"]
        }
    ]
}

? This task compiles all .cpp files in your project and outputs an executable in a bin/ folder. You may want to create the bin directory manually or adjust the path.


5. Set Up Debugging (launch.json)

  1. Go to Run and Debug view (Ctrl Shift D).
  2. Click "create a launch.json file" → choose "C (GDB/LLDB)" → then "g - Build and debug active file".
  3. It generates a launch.json. Example:
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g   - Build and Debug",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/bin/app",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "/usr/bin/gdb",  // Change if needed (e.g., "gdb" on Windows)
            "setupCommands": [
                {
                    "description": "Enable pretty printing",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "g   build",
            "logging": {
                "engineLogging": false
            }
        }
    ]
}

? Make sure program points to the same output path as in tasks.json. On Windows, miDebuggerPath might be "C:\\mingw64\\bin\\gdb.exe".


6. (Optional) Configure IntelliSense (c_cpp_properties.json)

  1. Press Ctrl Shift P"C/C : Edit Configurations (UI)".
  2. Set:
    • Compiler path: g or full path like C:\mingw64\bin\g .exe
    • IntelliSense mode: Choose based on your compiler (e.g., gcc-x64, clang-x64)
    • Standard: c 17 or higher

This helps with code suggestions and error detection.


7. Build and Run

  • Save main.cpp.
  • Press Ctrl Shift P"Tasks: Run Build Task" (or Ctrl Shift B) to compile.
  • Press F5 to compile and start debugging.

You should see output in the Debug Console.


That’s it! You now have a fully working C setup in VSCode. It's lightweight, fast, and great for learning or small to medium projects. Just remember to check compiler paths and output locations if things don’t run.

以上是如何为C设置VSCODE的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

PHP教程
1535
276
VSCODE设置。JSON位置 VSCODE设置。JSON位置 Aug 01, 2025 am 06:12 AM

settings.json文件位于用户级或工作区级路径,用于自定义VSCode设置。1.用户级路径:Windows为C:\Users\\AppData\Roaming\Code\User\settings.json,macOS为/Users//Library/ApplicationSupport/Code/User/settings.json,Linux为/home//.config/Code/User/settings.json;2.工作区级路径:项目根目录下的.vscode/settings

Java开发的VSCODE设置 Java开发的VSCODE设置 Jul 27, 2025 am 02:28 AM

InstallJDK,setJAVA_HOME,installJavaExtensionPackinVSCode,createoropenaMaven/Gradleproject,ensureproperprojectstructure,andusebuilt-inrun/debugfeatures;1.InstallJDKandverifywithjava-versionandjavac-version,2.InstallMavenorGradleoptionally,3.SetJAVA_HO

Vscode和Visual Studio之间有什么区别 Vscode和Visual Studio之间有什么区别 Jul 30, 2025 am 02:38 AM

VSCodeisalightweight,cross-platformcodeeditorwithIDE-likefeaturesviaextensions,idealforwebandopen-sourcedevelopment;2.VisualStudioisafull-featured,Windows-onlyIDEdesignedforcomplex.NET,C ,andenterpriseapplications;3.VSCodeperformsfasteronlower-endma

VS代码快捷方式,用于在所有文件中查找 VS代码快捷方式,用于在所有文件中查找 Jul 21, 2025 am 12:50 AM

ToquicklysearchacrossallfilesinVSCode,usethebuilt-inshortcutCtrl Shift F(Windows/Linux)orCmd Shift F(macOS)toopenthe"FindinAllFiles"panel,whichscansallfilesinthecurrentworkspace.Ensureyou'renotineditmodebypressingEsconcebeforeusingtheshortc

如何在Windows上安装VSCODE 如何在Windows上安装VSCODE Jul 27, 2025 am 03:16 AM

Gotohttps://code.visualstudio.comanddownloadtheWindowsUserInstaller.2.Runthe.exefile,allowchanges,andselectrecommendedoptionsincludingaddingtoPATHandcreatingadesktopshortcut.3.ClickFinishtolaunchVSCodeafterinstallation.4.Optionallyinstallusefulextens

vs代码中的keybindings.json文件在哪里? vs代码中的keybindings.json文件在哪里? Jul 23, 2025 am 01:32 AM

要找到或编辑VSCode的keybindings.json文件,可通过操作系统特定路径或命令面板操作。Windows路径为C:\Users\\AppData\Roaming\Code\User\keybindings.json;macOS为/Users\/Library/ApplicationSupport/Code/User/keybindings.json;Linux为/home\/.config/Code/User/keybindings.json。若不确定路径,可按Ctrl Shift

如何忽略VSCODE文件观察器中的某些文件? 如何忽略VSCODE文件观察器中的某些文件? Jul 26, 2025 am 03:46 AM

要解决VSCode因频繁变动文件导致资源管理器不断刷新的问题,可配置忽略文件监视行为。具体步骤为:1.在项目根目录下的.vscode/settings.json文件中添加files.watcherExclude配置项;2.使用glob模式匹配规则设置需排除的文件或目录,如"**/*.log":true忽略所有位置的.log文件,"**/tmp/**":true忽略任意层级下的tmp文件夹及其内容;3.若配置未生效,排查路径是否正确、插件监听影响及多配置冲突

如何将VSCODE与WSL(Linux的Windows子系统)一起使用 如何将VSCODE与WSL(Linux的Windows子系统)一起使用 Aug 01, 2025 am 06:26 AM

InstallWSLandaLinuxdistributionbyrunningwsl--installinPowerShellasAdministrator,thenrestartandsetuptheLinuxdistribution.2.Installthe"Remote-WSL"extensioninVSCodetoenableintegrationwithWSL.3.OpenaprojectinWSLbylaunchingtheWSLterminal,navigat

See all articles