VSCode编写C++后不能编译运行

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了VSCode编写C++后不能编译运行相关的知识,希望对你有一定的参考价值。

参考技术A
visual
studio
code中编译运行C/C++代码方法:
可以使用
Code
Runner
-
Visual
Studio
Marketplace
。安装好这个插件,不用进行任何配置就能直接编译并运行,默认情况下使用的是GCC和G++。有需要的话也可以更改配置文件来使用其他编译器,比如clang。

编译器2022-11-VSCode配置编译与调试C++程序(含输入输出重定向)

文章目录

1、最终效果

  • 在VSCode中运行c++程序,可以采用Coderunner(安装插件并配置)
    https://gwj1314.blog.csdn.net/article/details/100607554

  • 在VSCode中运行c++程序,也可以采用CPH(安装插件即可)

  • 也可以采用官方的C++插件(安装插件并配置)

2、环境配置(官方C++插件支持)

  • 首先安装微软官方的C++插件支持

  • 然后在工作区文件夹的.vscode目录中进行配置(4个文件)

c_cpp_properties.json(修改对应的目录,c++版本和编译模式)


    "configurations": [
        
            "name": "Win32",
            "includePath": [
                "$workspaceFolder/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.19041.0",
            "compilerPath": "C:/Development/x86_64-8.1.0-release-posix-seh-rt_v6-rev0/mingw64/bin/g++.exe",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "gcc-x64"
        
    ],
    "version": 4

launch.json(修改输入输出重定向的文件in1.txt, out1.txt)


    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        
            "name": "C++调试", // 这个调试任务的名字,当你开始调试的时候待选项中的名字
            "type": "cppdbg",
            "request": "launch",
            "program": "$fileDirname/$fileBasenameNoExtension",
            "args": [ //文件重定向功能,如果不需要使用请移除下面所有参数
                "<",
                "$fileDirname/in1.txt",
                ">",
                "$fileDirname/out1.txt",
            ],
            "stopAtEntry": false,
            "cwd": "$workspaceFolder",
            "environment": [],
            "externalConsole": false, // 这个选项控制是否在新窗口中运行
            "MIMode": "gdb",
            "miDebuggerPath": "gdb", // 你的MinGW编译器下gdb的目录,请根据实际情况调整
            "setupCommands": [
                
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                
            ],
            "preLaunchTask": "C/C++: g++.exe 生成活动文件" // task.json中的任务名称,请根据实际情况调整
        
    ]

tasks.json(修改g++编译器的位置)


    "tasks": [
        
            "type": "cppbuild",
            "label": "C/C++: g++.exe 生成活动文件",
            "command": "C:\\\\Development\\\\x86_64-8.1.0-release-posix-seh-rt_v6-rev0\\\\mingw64\\\\bin\\\\g++.exe",
            "args": [
                "-g",
                "$file",
                "-o",
                "$fileDirname\\\\$fileBasenameNoExtension.exe"
            ],
            "options": 
                "cwd": "$fileDirname"
            ,
            "problemMatcher": [
                "$gcc"
            ],
            "group": 
                "kind": "build",
                "isDefault": true
            ,
            "detail": "调试器生成的任务。"
        
    ],
    "version": "2.0.0"

settings.json(一般不用改)


    "files.associations": 
        "iostream": "cpp",
        "array": "cpp",
        "atomic": "cpp",
        "*.tcc": "cpp",
        "bitset": "cpp",
        "cctype": "cpp",
        "cfenv": "cpp",
        "charconv": "cpp",
        "chrono": "cpp",
        "cinttypes": "cpp",
        "clocale": "cpp",
        "cmath": "cpp",
        "codecvt": "cpp",
        "complex": "cpp",
        "condition_variable": "cpp",
        "csetjmp": "cpp",
        "csignal": "cpp",
        "cstdarg": "cpp",
        "cstddef": "cpp",
        "cstdint": "cpp",
        "cstdio": "cpp",
        "cstdlib": "cpp",
        "cstring": "cpp",
        "ctime": "cpp",
        "cuchar": "cpp",
        "cwchar": "cpp",
        "cwctype": "cpp",
        "deque": "cpp",
        "forward_list": "cpp",
        "list": "cpp",
        "unordered_map": "cpp",
        "unordered_set": "cpp",
        "vector": "cpp",
        "exception": "cpp",
        "algorithm": "cpp",
        "functional": "cpp",
        "iterator": "cpp",
        "map": "cpp",
        "memory": "cpp",
        "memory_resource": "cpp",
        "numeric": "cpp",
        "optional": "cpp",
        "random": "cpp",
        "ratio": "cpp",
        "regex": "cpp",
        "set": "cpp",
        "string": "cpp",
        "string_view": "cpp",
        "system_error": "cpp",
        "tuple": "cpp",
        "type_traits": "cpp",
        "utility": "cpp",
        "fstream": "cpp",
        "future": "cpp",
        "initializer_list": "cpp",
        "iomanip": "cpp",
        "iosfwd": "cpp",
        "istream": "cpp",
        "limits": "cpp",
        "mutex": "cpp",
        "new": "cpp",
        "ostream": "cpp",
        "scoped_allocator": "cpp",
        "shared_mutex": "cpp",
        "sstream": "cpp",
        "stdexcept": "cpp",
        "streambuf": "cpp",
        "thread": "cpp",
        "typeindex": "cpp",
        "typeinfo": "cpp",
        "valarray": "cpp"
    ,
    "java.dependency.syncWithFolderExplorer": false,
    "jupyter.jupyterServerType": "local"

最后推荐一个在线C++编译器

以上是关于VSCode编写C++后不能编译运行的主要内容,如果未能解决你的问题,请参考以下文章

C/C++不能运行时动态编译代码,编写公式编辑器,用户输入循环判断语句保存后执行该怎么实现啊?

如何在VSCode内编译运行C++

如何在VSCode内编译运行C++

如何在VSCode内编译运行C++

vscode怎么编译python

vscode C语言编译?