vscode: windows/linux下配置C/C++编译和

2020-11-23  本文已影响0人  lifanxin

作者:__lifanxin
链接:https://blog.csdn.net/A951860555/article/details/109175215
来源:CSDN
著作权归作者所有,任何形式的转载都请联系作者。

说在前面

  本文是从官方文档以及实际操作中总结而来,对于从事该行业的专业人员来说,推荐多看官方英文文档;当然对于初学者而言,先行阅读一个较好的中文指导博客也是无可厚非的。

链接: vscode C/C++环境配置官方文档.

正式起步

安装gcc组件

  windows下可以下载mingw-w64,关于mingw-w64的说明可以参见百度百科。而对于大多数linux系统而言,一般都自带有gcc、g++的编译环境,所有如果你正在使用常见的ubuntu系统、mac系统的话就不需要安装。

添加环境变量

  这一步是需要windows用户单独操作的,也就是并不自带gcc组件的系统。
  添加环境变量的步骤很简单,首先找到正确的gcc组件所在的bin目录,也就是说目录路径只能到bin为止,而不能明确到gcc,因为在bin目录下不止有gcc,还有g++、gdb等程序。然后在win10系统中可以直接搜索环境变量,添加到系统变量或者用户变量的path即中。
  在添加环境变量后,我们就可以在终端中直接输入gcc --version等命令,如果显示版本信息即可证明添加成功。这是因为本来在终端需要执行一个命令就是在path环境变量中去查找该命令程序的所在目录,一般系统都会自动配置一些程序路径在这里,所以像自带gcc组件的ubuntu和mac这些早已配置好了相应环境变量的系统,不用配置也能在终端执行相应命令。当然我们之所以需要在这里添加环境变量,只是为了方便后面vscode自动生成一些配置文件时可以找到gcc、g++的路径。

创建helloworld项目

  下面我们开始用helloworld项目来完善我们的配置。
  新建一个test文件夹,在文件下新建hello.c或者hello.cpp,一个用于C语言,一个用于C++。接下来就是最关键的配置文件生成部分。

hello.c

#include <stdio.h>


int main(void)
{
    printf("hello, world\n");
    return 0;
}

hello.cpp

#include <iostream>


int main(void)
{
    using namespace std;
    
    cout << "hello, world!" << endl;
    return 0;
}

生成编译配置文件

编译配置-->tasks.json

  首先,选中.p或者.cpp文件,也就是一定要在vscode中打开相应的源代码文件(否则不会出现如下第二张图的模板文件选项),然后按照下图一中进行操作,点击Terminal-->Configure Tasks或者Configure Default Build Task。

创建tasks.json
  接下来会出现vscode自带的模板文件选项,是C就选择gcc.exe,是C++就选择g++.exe
C++编译模板配置

  不同的系统展示会略有区别,如下是ubuntu系统所示,会展示详细的compiler信息来帮助我们进行选择。

ubuntu tasks.json

  选中相应的模板文件后,会在当前目录下生成.vscode文件夹,里面会出现tasks.json文件,该文件即对应执行程序编译的配置文件,其内容如下:

windows下tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

ubuntu下tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "/usr/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "compiler: /usr/bin/g++"
        }
    ]
}

关键键值名

  上面的两个文件大同小异,对于json文件中的健我们可以直接在vscode中通过鼠标的悬停来进行了解,不过这里我们着重了解一些关键的健值就行了。

以windows下tasks.json为例,其它系统也一样

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "type": "shell",
            "label": "C/C++: NEW g++.exe build active file",
            "command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

链接:tasks.json和launch.json文件中的变量说明

tasks.json完整配置

  windows下tasks.json完整配置如下图所示,ubuntu见上图第一次给出的tasks.json文件:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "C/C++: g++.exe build active file",
            // 使用自己电脑上的路径
            "command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\bin\\g++.exe",
            "args": [
                "-g",
                // 想编译当前文件夹下所有.cpp文件可以这样写
                // "${fileDirname}\\*.cpp"
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

启动编译

  如下图所示,点击Terminal-->Run Build Task执行默认的编译选项(Run Task 会列出所有的编译配置选项),此操作会使用我们之前配置好的tasks.json文件来对源代码进行编译,在windows下生成hello.exe文件,ubuntu下生成的程序则没有后缀名。

启动编译
  至此,我们的C/C++的编译环境已经配好,windows下在终端输入./hello.exe,ubuntu下输入./hello即可运行程序。下面进入调试环境的配置。

生成调试配置文件

调试配置-->launch.json

  同上面一样,选中需要进行debug调试的文件,按照下图所示点击Run-->Add Configuration即可在.vscode文件夹下生成launch.json文件。

创建launch.json

windows下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) 启动",
            "type": "cppdbg",
            "request": "launch",
            "program": "输入程序名称,例如 ${workspaceFolder}/a.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "/path/to/gdb",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

ubuntu下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": "g++ - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++ build active file",
            "miDebuggerPath": "/bin/gdb"
        }
    ]
}

关键键值名

  如上图所示,在ubuntu下可能已经自动生成好了完整的debug调试配置,如果没有也没关系,按照下面的操作进行就行。

launch.json完整配置

  下面给出windows下完整的launch.json配置,ubuntu完整配置见上图第一次给出的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) 启动",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            // 替换为自己电脑上的gdb路径
            "miDebuggerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\bin\\gdb.exe`",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            // 要和tasks.json中的label标签名对应
            "preLaunchTask": "C/C++: g++.exe build active file"
        }
    ]
}

启动调试

  如下图所示,点击Run-->Start Debugging,即会按照launch.json文件配置来对相应的程序进行debug。


启动调试

  在调试的时候给程序打好断点,如下图所示,我对代码略微进行了修改,声明了num变量,运行debug调试后,点击有个虫子(bug)样式的图标,然后在左侧栏就可以看到变量的值。


断点调试

结束语

  经过上面一番折腾,我们已经配置好C/C++程序的编译和调试环境,当然如果需要在其它项目中应用相同的环境配置,可以直接复制.vscode到其它项目的根目录下即可。那么,现在就开始愉快的编程吧!

上一篇 下一篇

猜你喜欢

热点阅读