Visual studio 2015 gives me errors upon creating a simple test console program

前端 未结 10 824
轮回少年
轮回少年 2020-12-05 07:57

Here is the code I am using.

#include \"stdafx.h\"
#include 

int main() {
    std::cout << \"hi\";

    return 0;
}

相关标签:
10条回答
  • 2020-12-05 08:14

    I had a similar problem upgrading an existing C project from Visual Studio 2013 to VS2017 (I'd skipped VS2015); none of the standard headers were found there either.

    The accepted answer (by Cezar Azevedo de Faveri) did work for me, but it's inelegant to just jam an absolute path in the settings, especially considering someone can change the install path of both Visual Studio and the SDKs; I'd like to write code that "just works" where possible.

    So I spent a little time studying how VS2017 generates a new project, and I eventually found an answer, which is that when VS2017 upgrades an existing C project, it forgets to upgrade one critical project value, and that incorrect value — the Windows SDK Version — makes the headers unable to be found:

    By default, VS2017 installs the headers only for the Windows 10 UWP SDK, but it doesn't change the "Windows SDK Version" in any projects it upgrades to a version of the SDK that was actually installed! Mine were set to "8.1" after the upgrade, and there are no headers installed for Windows 8.1

    So if you're upgrading an existing project, you'll have to change this setting manually to whichever version of the headers you actually have: In my case, that was by explicitly adding 10.0.14393.0 to the list (that's the version number for the Windows 10 UWP SDK headers that come with VS2017).

    (The list of installed versions can be found in the C:\Program Files (x86)\Windows Kits\10\Include folder, and in the similar folders near it.)

    0 讨论(0)
  • 2020-12-05 08:16

    #include "stdafx.h"

    There is a well-known difference between the <...> and "..." includes: briefly, that the former is for library includes and the latter is for local includes.

    You mention that you were looking around for stdafx.h but couldn't find it in the compiler installation. This suggests that:

    1. You think stdafx.h is a library file (it is not, unless it's some MS-specific extension, which I doubt, although it is traditionally used as a default filename for precompiled headers by the same--if you have made one, which you almost certainly haven't).

    2. Because of 1., you haven't made a local file stdafx.h, and therefore this include directive should fail. If it hasn't, then something fishy is happening.


    As to your actual problem, I have some notes:

    1. <stdio.h> is the C header, not the C++ one. If you're including from a C++ file (extension .cpp, probably, for MSVC), then you should use the C++ header <cstdio>. However, this shouldn't actually cause the problem.

    2. You aren't using the stdio anyway (at least not directly). You're using iostream, which you're properly including. If that include is the one that's causing the error, then iostream is trying to include it, can't, and your compiler installation is borked.

    3. Try the similar program:


    #include <iostream>
    int main() {
        std::cout << "hi" << std::endl;
        return 0;
    }
    

    I have just checked myself that this compiles and executes properly under Visual Studio 2015 Professional.

    If this program does not compile, I suggest reinstalling Visual Studio. In my experience, this often fixes these tricky setup issues.

    0 讨论(0)
  • 2020-12-05 08:17

    I had the same problem in Visual Studio Community 2015 after installing the current Windows SDK and as Signa already wrote earlier, this can be fixed for all projects within a "Toolset.props" file (at least for VS2015) and I find this to be the most convenient solution, because this has to be done only once. I've got a few side notes, because there is something to watch out for.

    For each build platform there is an own "Toolset.props" file, so both need to be modified if you want to build for 32 and 64 bit targets:

    C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Platforms\Win32\PlatformToolsets\v140\Toolset.props
    
    C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Platforms\x64\PlatformToolsets\v140\Toolset.props
    

    The files are write-protected and you need to remove the write protection before you can change those files (remember to put it back on after you're done).

    As of now the current SDK version is "10.0.15063.0" and you need to adjust that to the version you want to use (or to the SDK version you have installed).

    Look out for the IncludePath and LibraryPath lines in those props files and add the following paths to them:

    IncludePath: $(ProgramFiles)\Windows Kits\10\Include\10.0.15063.0\ucrt

    LibraryPath: $(ProgramFiles)\Windows Kits\10\Lib\10.0.15063.0\ucrt\$(PlatformTarget)

    Here a sample how this looks like for the 32 bit version:

    // ... some XML before that ...
    <PropertyGroup>
        // ... executable path .....
        <IncludePath Condition="'$(IncludePath)' == ''">$(VC_IncludePath);$(WindowsSDK_IncludePath);$(ProgramFiles)\Windows Kits\10\Include\10.0.15063.0\ucrt;</IncludePath>
        // ... reference path ...
        <LibraryPath Condition="'$(LibraryPath)' == ''">$(VC_LibraryPath_x86);$(WindowsSDK_LibraryPath_x86);$(NETFXKitsDir)Lib\um\x86;$(ProgramFiles)\Windows Kits\10\Lib\10.0.15063.0\ucrt\$(PlatformTarget);</LibraryPath>
        // ... more XML ...
    </PropertyGroup>
    // ... even more XML ....
    
    0 讨论(0)
  • 2020-12-05 08:19

    Above there is provided solution is per project. But if you don't want to reinstall VS from scratch or set the include directories and libraries on every solution you can modify Toolset.props found in:

    C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Platforms\Win32\PlatformToolsets\v140\Toolset.props

      <PropertyGroup>
    ....................
        <IncludePath Condition="'$(IncludePath)' == ''">$(VC_IncludePath);$(WindowsSDK_IncludePath);**C:\Program Files (x86)\Windows Kits\10\Include\10.0.10150.0\ucrt**</IncludePath>
    .......................
        <LibraryPath Condition="'$(LibraryPath)' == ''">$(VC_LibraryPath_x86);$(WindowsSDK_LibraryPath_x86);$(NETFXKitsDir)Lib\um\x86;**C:\Program Files (x86)\Windows Kits\10\Lib\10.0.10150.0\ucrt\**</LibraryPath>
    ...........................
      </PropertyGroup>
    
    0 讨论(0)
提交回复
热议问题