【问题标题】:Visual Studio Code C++: unordered_map not foundVisual Studio Code C++:未找到 unordered_map
【发布时间】:2019-02-16 02:37:26
【问题描述】:

我得到了一些需要编译的 C++ 文件。我在 Windows 10 上使用带有 C/C++ 和 Code Runner 扩展的 Visual Studio Code。使用以下“包含”语句:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <queue>
#include <unordered_map>

我收到以下错误:

unordered_map: No such file or directory

我对 C++ 很陌生,一直无法找到解决此问题的方法。我已经更新了我的 c_cpp_properties.json 文件中的“includePath”,如下所示。我也尝试过使用 Cygwin 和 Visual Studio Community 进行编译,但我得到了同样的错误。我知道 unordered_map .hpp 文件存在,但编译器似乎没有找到它。

"configurations": [
    {
        "name": "Win32",
        "includePath": [
            "${workspaceFolder}/**",
            "C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.15.26726/include"
        ],
        "defines": [
            "_DEBUG",
            "UNICODE",
            "_UNICODE"
        ],
        "windowsSdkVersion": "10.0.17134.0",
        "cStandard": "c11",
        "cppStandard": "c++17",
        "intelliSenseMode": "msvc-x64"
    }
],
"version": 4

如果相关,这就是我的 tasks.json 文件的样子:

"version": "2.0.0",
"tasks": [
    {
        "label": "build",
        "type": "shell",
        "command": "msbuild",
        "args": [
            // Ask msbuild to generate full paths for file names.
            "/property:GenerateFullPaths=true",
            "/t:build"
        ],
        "group": "build",
        "presentation": {
            // Reveal the output only if unrecognized errors occur.
            "reveal": "silent"
        },
        // Use the standard MS compiler pattern to detect errors, warnings and infos
        "problemMatcher": "$msCompile"
    }
]

我的 .json 文件配置正确吗?如果我遗漏了一些基本的东西,我深表歉意;我已经做了很多关于如何在 Windows 上编译 C++ 的搜索,但没有任何成功。提前感谢您的帮助。

编辑: 这是我正在尝试编译的完整文件。该可执行文件旨在由 python 脚本调用。

https://github.com/jorpjomp/sierra-hotel/blob/master/location_routing.cpp

【问题讨论】:

  • 如果您创建了标准 c++ -> Win32 控制台项目,Visual Studio 社区版应该可以立即使用。我不知道为什么有人想在 Windows 上使用 VS 代码,但我可能很天真。你能显示你正在尝试编译的整个 .cpp 文件吗?
  • @ChristopherPisz 使用 Github 链接编辑了帖子。感谢您的帮助。
  • 所以,如果我下载全新安装的 VS 社区,并创建一个新的控制台项目,然后将代码放入其中,则需要 node.h 和 routingEntry 的定义。只需将带有解决方案文件的整个项目和所有内容上传到 github,或者在此处放置 MCVE:stackoverflow.com/help/mcve
  • 不要将整个项目/解决方案在线转储并在此处链接。 提供minimal reproducible example
  • 请添加工程文件和完整的编译器输出(需要看调用参数和编译器输出)

标签: c++ visual-studio-code include-path


【解决方案1】:

您目前正在使用 msbuild 来构建您的项目。这是故意的吗?如果你只是想编译“一些 C++ 文件”,那么 msbuild 就有点过头了,直接使用 Mingw 的 g++ 或 Microsoft CL.exe 编译器编译源代码。

所以我推荐:

1) 转到http://mingw-w64.org/doku.php/download,下载并安装mingw,并将g++的路径添加到PATH环境变量中。

2) 在 Visual Studio Code 中创建一个包含以下内容的 task.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileBasenameNoExtension}"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

c_cpp_properties.json(假设你将mingw存放在这里:C:\mingw\mingw64\bin):

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "C:/mingw/mingw64/bin",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "msvc-x64"
        }
    ],
    "version": 4
}

【讨论】:

    【解决方案2】:

    默认情况下,VS Code 不支持无序映射 Microsoft ms-vscode.cpptools。请按照以下步骤解决问题:

    1. 从 (https://sourceforge.net/projects/mingw/) 下载 MinGW。 MinGW 是 GNU 编译器集合 (GCC) 的原生 Windows 端口,具有可免费分发的导入库和头文件,用于构建原生 Windows 应用程序。

      标记所有要安装的软件包。 ss to mark all the packages for installation

      单击安装选项卡下的应用更改选项 ss of where to click on apply changes

    2. 现在,环境变量的路径将被更新。转到高级系统设置->环境变量。

      在系统变量选项卡中编辑路径。 ss of how to edit Path

      复制MinGW的bin文件夹路径。默认路径为:C:\MinGW\bin

      将此新路径粘贴到列表中,然后单击“确定”。 ss after pasting the bin path into the list

    3. 在 VS Code 中运行 C++ 代码。它会正常工作。 ss of vs code working fine at last

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-09
      • 1970-01-01
      • 2018-03-26
      • 1970-01-01
      • 2020-07-30
      • 1970-01-01
      • 2022-12-06
      • 2020-02-27
      相关资源
      最近更新 更多