Windows下Vs Code 配合MinGW 编译项目

1.首先需要配置MinGW的环境变量,如下图所示

配置MinGW的位置。

这个bin目录下面包含了gcc/g++等mingw编译工具

2.编写一个简单的应用程序

非常简单的一个应用程序,有兴趣,可以实现一个复杂的。

3. 编写CMakeLists.txt 文件

cmake_minimum_required(VERSION 3.5)
project(test)

aux_source_directory(. main_lists)

add_executable(main ${main_lists})

最简单的cmake文件

4.使用cmake 命令生成makefile

cmake -G "MinGW Makefiles" ..

 当然,我们也可以使用cmake-gui.exe 来运行cmake 客户端来生成makefile

我没有使用过,所以就不贴图了

5.现在已经有了makefile,  那么现在使用make生成执行文件。

注意,这里生成执行文件需要使用mingw来的make来进行编译

mingw32-make.exe -j16

 6.ok,这个时候执行文件已经编译成功了,现在开始进行调试。

在一次编译都没有进行的时候,是不存在launch.json文件的,  那么该怎么办呢?  按下F5,那么Vs Code会自动进入到launch.json文件中,

7.配置launch.json文件

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/main.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

我们需要注意一下几个参数,

1> name :  指定选择什么进行调试,这里我们选择(gdb)

2> program:需要调试的程序的路径

3> miDebuggerPath:gdb所在的路径,由于之前我们已经配置了环境变量,所以这块直接写gdb.exe就可以了。

其他的参数大家可以自行进行搜索

8.CMakeLists.txt的重新编写,由于我们需要debug,所以需要编译debug版本

9.按F5进入调试,如下

ok,这个就是基本的Vs Code + MinGW进行编译调试了。

猜你喜欢

转载自blog.csdn.net/qq_15024587/article/details/88316815