简体   繁体   English

VS 代码无法链接 Windows 10 SDK 库或包含 header 文件的问题

[英]Trouble with VS Code not linking the Windows 10 SDK libraries or included header files

I'm having some issues getting my installation of VS Code to compile even a basic C++ script.我在安装 VS Code 以编译基本的 C++ 脚本时遇到了一些问题。

The way I installed everything (and I have done this multiple times over) is by first installing the visual studio installer to the default location.我安装所有东西的方式(我已经多次这样做了)是首先将 Visual Studio 安装程序安装到默认位置。 Once that was complete, I installed Visual Studio 2019 Community with the desktop c++ development, and c++ game development modules into their default install directories as well (These include the most recent windows 10 sdk, and C runtime environment, etc). Once that was complete, I installed Visual Studio 2019 Community with the desktop c++ development, and c++ game development modules into their default install directories as well (These include the most recent windows 10 sdk, and C runtime environment, etc). I then installed VS Code to the default location too.然后我也将 VS Code 安装到了默认位置。

To open VS Code I use the developer command prompt and type 'code' then hit enter.要打开 VS Code,我使用开发人员命令提示符并输入“code”,然后按 Enter。 I then also installed the Microsoft C/C++ extension.然后我还安装了 Microsoft C/C++ 扩展。 I created a new file, let's call it 'example.cpp' which contains the following:我创建了一个新文件,我们称之为“example.cpp”,其中包含以下内容:

int main()
{
    return 0;
}

When I compile this from the VS Code terminal after navigating to the file's location and running the 'cl example.cpp' command, I get the following error:当我在导航到文件的位置并运行“cl example.cpp”命令后从 VS Code 终端编译它时,我收到以下错误:

LINK : fatal error LNK1104: cannot open file 'kernel32.lib'

As I've discovered this is due to the %LIB% and %LIBPATH% variables not referencing the Windows 10 SDK libraries installed on the system.正如我发现的那样,这是由于 %LIB% 和 %LIBPATH% 变量没有引用系统上安装的 Windows 10 SDK 库。 I had managed to get around this by manually setting 'LIB' and 'LIBPATH' system environment variables pointing to the correct folders, however I know that that's a pretty messy way to fix it and might mess things up once I move towards the Unreal engine.我已经设法通过手动设置指向正确文件夹的“LIB”和“LIBPATH”系统环境变量来解决这个问题,但是我知道这是一种非常混乱的修复方法,一旦我转向虚幻引擎可能会搞砸. With that being said, I was able to compile and run the above code without issue until I tried a simple Hello World program:话虽如此,在我尝试了一个简单的 Hello World 程序之前,我能够毫无问题地编译和运行上述代码:

#include <iostream>

int main()
{
    std::cout << 'Hello World!';
    return 0;
}

Where when compiled produced the following error:编译时在哪里产生以下错误:

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.25.28610\include\yvals.h(12):
fatal error C1083: Cannot open include file: 'crtdbg.h': No such file or directory

This leads me to believe that the %INCLUDE% path also hasn't been set to point to the Windows 10 SDK includes, and to confirm this I went into the Developer Command Prompt and ran a few echo commands, and this is my results:这让我相信 %INCLUDE% 路径也没有设置为指向 Windows 10 SDK 包括,为了确认这一点,我进入了开发人员命令提示符并运行了一些回显命令,这是我的结果:

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community>echo %INCLUDE%
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.25.28610\ATLMFC\include;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.25.28610\include;

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community>echo %LIBPATH%
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.25.28610\ATLMFC\lib\x86;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.25.28610\lib\x86;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.25.28610\lib\x86\store\references;C:\Windows\Microsoft.NET\Framework\v4.0.30319;

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community>echo %LIB%
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.25.28610\ATLMFC\lib\x86;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.25.28610\lib\x86;

As far as I'm currently aware, these paths should have a reference to the SDK's include folder, as well as the bin folders that contains the kernel32.lib file.据我目前所知,这些路径应该引用 SDK 的包含文件夹,以及包含 kernel32.lib 文件的 bin 文件夹。 Now without manually creating system environment variables again, I'm wondering if there's a command I can use within the developer console that will add the SDK paths that are needed to the %INCLUDE%, %LIB%, and %LIBPATH% variables?现在无需再次手动创建系统环境变量,我想知道是否可以在开发人员控制台中使用一个命令,将所需的 SDK 路径添加到 %INCLUDE%、%LIB% 和 %LIBPATH% 变量中? Or am I simply going about this completely wrong?还是我只是在做这完全错误的事情?

Any and all advice is more than appreciated, and I'm more than happy to provide further information if necessary.任何和所有建议都非常感谢,如有必要,我非常乐意提供更多信息。

Update: as requested, my VS Code JSON files.更新:根据要求,我的 VS Code JSON 文件。

c_cpp_properties.json c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.18362.0",
            "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.25.28610/bin/Hostx64/x64/cl.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "msvc-x64"
        }
    ],
    "version": 4
}

Make sure you run the Developer Command Prompt as an administrator.确保以管理员身份运行开发人员命令提示符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM