C 语言

关注公众号 jb51net

关闭
首页 > 软件编程 > C 语言 > VS Code C++ 环境配置

VS Code C++ 环境配置及 HelloWorld 程序(保姆教程)

作者:水天需010

文章详细介绍了在Windows、macOS和Linux上配置VSCode的C++开发环境,并提供了创建、编译、调试和使用CMake的步骤,还涵盖了常见问题的解决方法和快速模板项目的获取,感兴趣的朋友跟随小编一起看看吧

VS Code C++ 环境配置及 HelloWorld 程序

一、环境配置(Windows/macOS/Linux)

1. 安装必要软件

Windows:

# 1. 安装 Visual Studio Code
# 下载地址:https://code.visualstudio.com/
# 2. 安装 MSVC 或 MinGW
# 方案A:安装 Visual Studio Build Tools
# 下载:https://visualstudio.microsoft.com/zh-hans/downloads/
# 勾选 "使用C++的桌面开发"
# 方案B:安装 MinGW-w64
# 下载:https://sourceforge.net/projects/mingw-w64/

macOS:

# 安装 Xcode Command Line Tools
xcode-select --install
# 或使用 Homebrew 安装 GCC
brew install gcc

Linux (Ubuntu/Debian):

sudo apt update
sudo apt install build-essential gdb

2. 安装 VS Code 扩展

打开 VS Code,安装以下扩展:

  1. C/C++ (Microsoft) - 代码补全、调试
  2. C/C++ Extension Pack - 扩展包(推荐)
  3. CMake Tools (可选) - CMake 支持

二、第一个 HelloWorld 程序

1. 创建项目目录结构

my-cpp-project/
├── .vscode/
│   ├── tasks.json      # 编译任务配置
│   ├── launch.json     # 调试配置
│   └── c_cpp_properties.json # 编译器配置
├── src/
│   └── main.cpp        # 源代码
└── build/              # 编译输出目录(自动生成)

2. 编写 HelloWorld 程序

src/main.cpp:

#include <iostream>
int main() {
    std::cout << "Hello, VS Code C++ World!" << std::endl;
    // 一些额外的示例
    std::cout << "================" << std::endl;
    std::cout << "C++ 版本: " << __cplusplus << std::endl;
    int a = 10, b = 20;
    std::cout << "a + b = " << a + b << std::endl;
    // 等待用户输入
    std::cout << "\n按回车键退出...";
    std::cin.get();
    return 0;
}

三、VS Code 配置文件

1..vscode/c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "${default}"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.19041.0",
            "compilerPath": "C:/msys64/mingw64/bin/g++.exe",  // 根据实际路径修改
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "windows-gcc-x64"
        }
    ],
    "version": 4
}

2..vscode/tasks.json- 编译任务

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe 构建活动文件",
            "command": "g++",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${workspaceFolder}/src/main.cpp",
                "-o",
                "${workspaceFolder}/build/main.exe"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": ["$gcc"],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "编译器: g++.exe"
        },
        {
            "label": "Clean Build",
            "command": "rm",
            "args": ["-rf", "${workspaceFolder}/build/*"],
            "type": "shell",
            "group": "build"
        }
    ]
}

3..vscode/launch.json- 调试配置

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) 启动",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/main.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:/msys64/mingw64/bin/gdb.exe", // 根据实际路径修改
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe 构建活动文件"
        }
    ]
}

四、分平台配置方案

Windows 配置(MinGW)

// tasks.json (Windows MinGW)
{
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g",
                "-std=c++17",
                "${workspaceFolder}/src/*.cpp",
                "-I${workspaceFolder}/include",
                "-o",
                "${workspaceFolder}/build/main.exe"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

macOS/Linux 配置

// tasks.json (macOS/Linux)
{
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g",
                "-std=c++17",
                "${workspaceFolder}/src/*.cpp",
                "-I${workspaceFolder}/include",
                "-o",
                "${workspaceFolder}/build/main"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

五、编译和运行步骤

方法1:使用任务运行(推荐)

  1. Ctrl+Shift+B - 编译程序
  2. F5 - 编译并调试运行
  3. Ctrl+F5 - 运行程序(不调试)

方法2:使用终端

# 进入项目目录
cd my-cpp-project
# 创建build目录
mkdir -p build
# 编译
g++ -g -std=c++17 src/main.cpp -o build/main
# 运行 (Linux/macOS)
./build/main
# 运行 (Windows)
.\build\main.exe

六、调试功能使用

基础调试操作

  1. F9 - 设置/取消断点
  2. F5 - 开始调试
  3. F10 - 单步跳过
  4. F11 - 单步进入
  5. Shift+F5 - 停止调试

调试窗口

七、使用 CMake(高级)

CMakeLists.txt

cmake_minimum_required(VERSION 3.10)
project(HelloWorld)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(main src/main.cpp)

配置步骤

  1. 安装 CMake Tools 扩展
  2. 创建 CMakeLists.txt
  3. 按 Ctrl+Shift+P,输入 "CMake: Configure"
  4. 按 F7 构建项目

八、常见问题解决

1. "g++ not found" 错误

# Windows: 确保 MinGW 已加入 PATH
# 检查方法:
g++ --version
# 在终端设置 PATH
set PATH=C:\mingw64\bin;%PATH%

2. 中文乱码问题

// 在 main.cpp 开头添加:
#ifdef _WIN32
#include <windows.h>
#endif
int main() {
#ifdef _WIN32
    SetConsoleOutputCP(65001); // UTF-8
#endif
    std::cout << "中文测试" << std::endl;
}

3. 调试不工作

  1. 检查 launch.json 中的路径
  2. 确保编译时使用 -g 参数
  3. 检查 GDB 是否正确安装

九、快速模板项目

可以从 GitHub 下载模板:

git clone https://github.com/yourusername/vscode-cpp-template.git
cd vscode-cpp-template
code .

现在你已经配置好了完整的 C++ 开发环境!开始编写代码吧!

到此这篇关于VS Code C++ 环境配置及 HelloWorld 程序(保姆教程)的文章就介绍到这了,更多相关VS Code C++ 环境配置内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文