Windows 下载 MinG-W64,选最新版本中的 x86_64-posix-seh,然后解压,设置环境变量。

https://sourceforge.net/projects/mingw-w64/files/

https://github.com/GorvGoyl/MinGW64/releases

https://www.mingw-w64.org/downloads

x86_64-posix-sjlj
x86_64-posix-seh
x86_64-win32-sjlj
x86_64-win32-seh
i686-posix-sjlj
i686-posix-dwarf
i686-win32-sjlj
i686-win32-dwarf

释义 1:

  • DWARF:一种带调试信息的包,所以比一般的包尺寸大,仅支持 32 位系统
  • SJLJ:跨平台,支持 32、64 位系统。缺点是运行速度稍慢,GCC 不支持
  • SEH:调用系统机制处理异常,支持 32、64 位系统。缺点是:GCC 不支持

释义 2:

  • x86_64:64位操作系统
  • i686:32 位操作系统 (i386的子集)

释义 3:

  • posix:启用了 C++ 11 多线程特性
  • win32:未启用

 

Linux 下

yum install -y cmake gcc gcc-c++ gdb

# 新版 cmake
# 下载 https://cmake.org/download/
# https://github.com/Kitware/CMake/releases
wget -O /opt/cmake-3.17.3.tar.gz  https://github.com/Kitware/CMake/releases/download/v3.17.3/cmake-3.17.3.tar.gz
cd /opt/
tar -zxf cmake-3.17.3.tar.gz
mv cmake-3.17.3 cmake-3.17.3-src
cd cmake-3.17.3-src/
# 或者 Install an OpenSSL development package
vim CMakeLists.txt
添加 set(CMAKE_USE_OPENSSL OFF)
# 配置安装路径
./bootstrap --prefix=/opt/cmake-3.17.3
# 编译安装
make && make install

# 新版本 gdb
# 下载
curl -o /opt/gdb-8.3.1.tar.gz  https://mirrors.ustc.edu.cn/gnu/gdb/gdb-8.3.1.tar.gz
cd /opt/
tar -zxf gdb-8.3.1.tar.gz
mv gdb-8.3.1 gdb-8.3.1-src
cd gdb-8.3.1-src/
# 配置安装路径
./configure --prefix=/opt/gdb-8.3.1
# 编译安装
make && make install

 

下载 VsCode 相关插件

只有 c/c++ 是必须的。

VsCode C/C++ 环境

 

在工作区创建 .vscode 文件夹,用 VsCode 创建,系统自带文件管理无法创建以点开头的文件夹,然后创建下面两个配置文件

修改配置中的路径为自己的 Ming-W64 路径

VsCode C/C++ 环境

launch.json 用于调试运行

https://code.visualstudio.com/docs/editor/debugging#_launch-configurations

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

tasks.json 用于调试运行前的编译

https://code.visualstudio.com/docs/editor/tasks#_custom-tasks

{
    "version": "2.0.0",
    "command": "gcc",
    "args": [
        "-g",
        "${file}",
        "-o",
        ".target/${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
        }
    },
    "presentation": {
        "echo": true,
        "reveal": "always",
        "focus": false,
        "panel": "new", // 这里 shared 表示共享,改成 new 之后每个进程创建新的端口
        "showReuseMessage": true,
        "clear": false
    }
}

 

测试

#include <math.h>
#include <stdio.h>

void main() {
  double a = pow(2.0, 3.0);
  printf("res = %f.2\n", a);
  printf("hello\n");

  double b = 1.234;
  int c = b;
  printf("%d", c);
}

按 F5 即可调试运行

VsCode C/C++ 环境

 

想直接运行,也可以安装 code-runner 插件

附上 VsCode 全部设置 settings.json

{
    "editor.minimap.enabled": false,
    "editor.mouseWheelZoom": true,
    "files.autoSave": "afterDelay",
    "terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
    "liveServer.settings.donotShowInfoMsg": true,
    "workbench.startupEditor": "newUntitledFile",
    "code-runner.ignoreSelection": true,
    "code-runner.runInTerminal": true,
    "editor.fontSize": 18,
    "liveServer.settings.multiRootWorkspaceName": "",
    "git.path": "D:/PcAPP/PortableGit/bin/git.exe",
    "git.enabled": false,
    "[c]": {
        "editor.defaultFormatter": "ms-vscode.cpptools"
    },
    "C_Cpp.clang_format_fallbackStyle": "LLVM",
    "C_Cpp.intelliSenseEngine": "Tag Parser",
    "workbench.colorTheme": "IDEA like light Theme",
    "code-runner.executorMap": {
        // windows 默认为 gbk 编码,这里让 VsCode 中默认 shell 为 PowerShell,编译时指定 gbk 编码,避免 code-runner 插件运行 c/c++ 输出中文时乱码
        "c": "cd $dir && gcc -fexec-charset=gbk $fileName -o ..\\.target\\$fileNameWithoutExt.exe && ..\\.target\\$fileNameWithoutExt.exe",
        "cpp": "cd $dir && g++ -fexec-charset=gbk $fileName -o ..\\.target\\$fileNameWithoutExt.exe && ..\\.target\\$fileNameWithoutExt.exe"
    },
    "workbench.iconTheme": "vsclassic-icon-theme",
    "update.showReleaseNotes": false,
    "telemetry.enableTelemetry": false,
}

 


https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools

相关文章:

  • 2021-10-27
  • 2021-10-10
  • 2022-12-23
  • 2022-12-23
  • 2021-04-27
  • 2021-05-19
  • 2021-09-02
  • 2021-06-04
猜你喜欢
  • 2021-06-30
  • 2021-09-15
  • 2021-11-11
  • 2022-12-23
  • 2022-12-23
  • 2021-12-12
  • 2022-03-04
相关资源
相似解决方案