简体   繁体   中英

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.

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. 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.

To open VS Code I use the developer command prompt and type 'code' then hit enter. I then also installed the Microsoft C/C++ extension. I created a new file, let's call it 'example.cpp' which contains the following:

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:

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. 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. With that being said, I was able to compile and run the above code without issue until I tried a simple Hello World program:

#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:

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. 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? 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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