zoukankan      html  css  js  c++  java
  • VS Code 编译C++

    1.安装VS Code

    2.安装插件

      在左侧插件库

      必须:

        c/c++ 插件

      非必需:

        C++ Intellisense

        Include Autocomplete

    3.安装编译调试环境mingw

    MinGW是是将GCC编译器和GNU Binutils移植到Win32平台下的产物,包括一系列头文件(Win32API)、库和可执行文件。MinGW是从Cygwin(1.3.3版)基础上发展而来。GCC支持的语言大多在MinGW也受支持,其中涵盖C、C++、Objective-C、Fortran及Ada。

    建议离线安装MinGW  可参考

    配置好环境变量

    通过g++ xx.cpp -o xx 检测是否可以用命令行来编译c++文件,可以运行则安装成功

    4.调试环境配置

    新建文件夹Test

    新建.vscode文件夹

      在.vscode下创建配置文件c_cpp_properties.json、launch.json、tasks.json

      (1)c_cpp_properties.json

        配置文件指定mingw所在位置,方便vscode自动调用

    {
        "configurations": [
            {
                "name": "Win32",
                "includePath": [
                    "${workspaceRoot}",
                    "D:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
                    "D:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
                    "D:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
                    "D:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include",
                    "D:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/tr1",
                    "D:/mingw64/x86_64-w64-mingw32/include/"
                ],
                "defines": [
                    "_DEBUG",
                    "UNICODE",
                    "__GNUC__=6",
                    "__cdecl=__attribute__((__cdecl__))"
                ],
                "intelliSenseMode": "clang-x64",
                "browse": {
                    "path": [
                        "${workspaceRoot}",
                        "D:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
                        "D:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
                        "D:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
                        "D:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include",
                        "D:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/tr1",
                        "D:/mingw64/x86_64-w64-mingw32/include/"
                    ]
                },
                "compilerPath": "D:\mingw64\bin\gcc.exe",
                "cStandard": "c11",
                "cppStandard": "c++17"
            }
        ],
        "version": 4
    }

        (2)launch.json文件

    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "C++ Launch (GDB)", // 配置名称,将会在启动配置的下拉菜单中显示
                "type": "cppdbg", // 配置类型,这里只能为cppdbg
                "request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加)
                "targetArchitecture": "x86", // 生成目标架构,一般为x86或x64,可以为x86, arm, arm64, mips, x64, amd64, x86_64
                "program": "${file}.exe", // 将要进行调试的程序的路径
                "miDebuggerPath": "d:\mingw64\bin\gdb.exe", // miDebugger的路径,注意这里要与MinGw的路径对应
                "args": [
                    "blackkitty",
                    "1221",
                    "# #"
                ], // 程序调试时传递给程序的命令行参数,一般设为空即可
                "stopAtEntry": false, // 设为true时程序将暂停在程序入口处,一般设置为false
                "cwd": "${workspaceRoot}", // 调试程序时的工作目录,一般为${workspaceRoot}即代码所在目录
                "externalConsole": true, // 调试时是否显示控制台窗口,一般设置为true显示控制台
                "preLaunchTask": "g++" // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc
            }
        ]
    }

        (3)tasks.json

    {
        "version": "0.1.0",
        "command": "g++",
        "args": [
            "-g",
            "${file}",
            "-o",
            "${file}.exe"
        ], // 编译命令参数
        "problemMatcher": {
            "owner": "cpp",
            "fileLocation": [
                "relative",
                "${workspaceRoot}"
            ],
            "pattern": {
                "regexp": "^(.*):(\d+):(\d+):\s+(warning|error):\s+(.*)$",
                "file": 1,
                "line": 2,
                "column": 3,
                "severity": 4,
                "message": 5
            }
        }
    }

      (4)在Test下新建测试文件test1.cpp

    #include <iostream>
    #include <windows.h>
    using namespace std;
    
    int main() {
        std::cout << "hello, I love you!" << std::endl;
        system("pause");
        return 0;
    }

    运行调试

    左侧的debug

    输出结果

  • 相关阅读:
    vb.net FTP上传下载,目录操作
    vb.net导出CSV文件
    服务器内存总量
    定义数组
    监控键盘健代码
    C# FTp 上传,下载
    使用EasyUI中Tree
    微信web开发自定义分享
    mysql将时间戳格式化
    查询表时给字段赋默认值 sql
  • 原文地址:https://www.cnblogs.com/baby123/p/10924689.html
Copyright © 2011-2022 走看看