Why do I get compilation errors after I conditionally include stdafx.h?
Asked Answered
N

4

9

I'm trying to write a program that compiles in Borland C++ and Visual C++. To do this, I add #ifdef _MSC_VER to include the stdafx.h file when the source is compiled under VS. The code compiles and executes OK in Borland C++, but in VS, it fails:

error C1020: unexpected #endif

#ifdef _MSC_VER //I'm in VS
#include "stdafx.h"
#endif //this line doesn't compile in VS


#ifdef __BORLANDC__   //I'm in Borland
#pragma hdrstop
#pragma argsused
#endif

#include <iostream>

int main(int argc, char* argv[])
{
    std::cout << "Hello" << std::endl;
    std::cin.get();
    return 0;
}

How I can fix this error?

Nonunion answered 18/10, 2011 at 2:7 Comment(0)
I
13

The way MSVC implements precompiled headers, is basically that the compiler ignores everything up to the line that brings in the precompiled header, and starts over from scratch there. So when you compile your program, it doesn't "remember" the #ifdef line, so the #endif line makes no sense to it.

The thing is, there's nothing inherently "MS" about stdafx.h, beyond the peculiar naming scheme [which you can change anyhow]. So why bother blocking out the inclusion in Borland in the first place? Worst case scenario, you move the _MSC_VER block into the header, and you end up in the same situation you're in now, except there's a single wasted include file. Or you let your build system redirect the #include to a Borland-specific stdafx.h file.

Isidora answered 18/10, 2011 at 3:8 Comment(0)
A
2

That must be precompiled header feature effect. The simple solution is to turn it off in VS project settings. The problem is, compiler resets its state when it encounters the

#include "stdafx.h"

line to the state saved in precompiled header file. So the #ifdef... line becomes ignored and #endif line causes compiler error

Alexandrite answered 18/10, 2011 at 2:36 Comment(0)
P
0

Others commented on the PCH issue. On a separate note, in Borland, #pragma argsused has to be attached to a function. Its purpose is to single to the compiler that the arguments of the function are not used by the function body so it does not emit "unused argument" warnings. As such, you cannot group #pragma hdrstop and #pragma argsused in the same #ifdef block. You need to separate them, eg:

#include "stdafx.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#include <iostream>

#ifdef __BORLANDC__
#pragma argsused
#endif
int main(int argc, char* argv[])
{
  std::cout << "Hello" << std::endl;
  std::cin.get();
  return 0;
}

A simplier solution is to simply comment out the argument names instead:

#include "stdafx.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#include <iostream>

int main(int /*argc*/, char* /*argv[]*/)
{
  std::cout << "Hello" << std::endl;
  std::cin.get();
  return 0;
}
Pipes answered 18/10, 2011 at 6:23 Comment(0)
M
0

If we switch compiler conditional in to stdafx.h we can avoid this error. for example following code lines can be defined in stdafx.h

    ********************* stdafx.h **************************
    #pragma once

    #if defined (_MSC_VER)

    #include "targetver.h"

    #define WIN32_LEAN_AND_MEAN             
    // Windows Header Files:
    #include <windows.h>
    #endif 
    *********************************************************

you can switch pragma once or traditional header guards within this macro conditional as you expect. hence,we able build stdafx.h with any C/C++ compiler since we port our macro conditional statement into the stdafx.h

Mild answered 26/2, 2018 at 9:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.