#if #include

Flaviu_ 1,031 Reputation points
2021-07-06T17:07:00.633+00:00

Which one is the correct way to do:

#if _MSC_VER > 1900 // VS2017 or above
    #include "pch.h"
#else // <-- Error
    #include "stdafx.h"
#endif

I got: fatal error C1019: unexpected #else

How can I write condition to compile a file for VS2017 or above, and below 2017 ?

Developer technologies C++
{count} votes

5 answers

Sort by: Most helpful
  1. Michael Taylor 60,161 Reputation points
    2021-07-06T17:59:53.053+00:00

    This is valid irrelevant of VS version. C++ preprocessor symbols haven't changed in this regard.

    I'm guessing at what could be wrong here. Note that I'm also assuming this code is in your .cpp file and not a .h file which would make this even harder to diagnose.

    The first thing that comes to mind is "pch.h". The "pch.h" file has a bad preprocessor directive causing the compiler to fail. You can test this by temporarily replacing this #include with a #include of windows.h or something. If it then gets past this error then the issue is inside pch.h.

    The second thing is precompiled headers. You are using precompiled headers in project. PCHs work funny in VS. It has been a long time since I've used them but IIRC when you enable PCHs then you specify the PCH file to look for (default is "stdafx.h". The preprocessor has to look for the PCH first so it literally skips all code until it finds the #include "stdafx.h" line. Everything before that is effectively skipped. This causes errors. The workaround historically has been to ensure that the PCH include is the very first line in the .cpp file.


  2. RLWA32 49,536 Reputation points
    2021-07-06T18:04:41.6+00:00

    Seems to me it has something to do with the way VC++ handles precompiled headers. VS2017 threw up the following warning before compilation failed with C1019.

    112274-warning.png

    0 comments No comments

  3. Sam of Simple Samples 5,546 Reputation points
    2021-07-06T18:10:31.727+00:00

    For VS2017 or above rename the pch.h file to stdafx.h. You will need to change all the .cpp files to use the stdafx.h name when relevant but you will not need any preprocessor conditions. You will need to make the change in the Project Properties under Configuration Properties -> C/C++ -> Precompiled Headers. I tested that in a simple console project using VS2017 and it worked for me.

    Depending on the version of VS being used, you will have problems with VS generating new files with the wrong name for the PCH but that can be fixed in a couple of seconds.

    0 comments No comments

  4. Flaviu_ 1,031 Reputation points
    2021-07-07T08:49:36.92+00:00

    I have a MFC control, spread in .h and .cpp file, and have been generated with

    #include "pch.h"
    

    Of course, I don't have stdafx.h in my project, but when I used this control in older project, this cpp file request stdafx.h at the first line.

    I thought I can write a conditional code to use it without any modification in new projects and old projects as well, but I guess I have to moditfy it accordingly.


  5. ridilculous 1 Reputation point
    2022-12-15T19:35:14.727+00:00

    This isn't possible.

    The problem is this:

    The compiler treats all code occurring before the .h file as precompiled. It skips to just beyond the #include directive associated with the .h file, uses the code contained in the .pch file, and then compiles all code after filename.

    This means that the following is valid (msv)c++:

    this isn't valid c++ but valid msvcpp  
    #include "stdafx.h"  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.