2

My program uses Qt and OpenGL. It compiles correctly under Linux and Mac. When compiled on windows, I need to #include windows.h in order to use OpenGL, the code is like following,

#if defined(_WIN32) || defined(_WIN64) || defined(WIN32) || defined(WIN64)
#include <windows.h>
#endif
#ifdef __APPLE__
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
#else
#include <GL/glu.h>
#include <GL/glut.h>
#endif

However, there are a lot of error messages like

 C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(167) : error C2146: syntax error : missing ')' before identifier 'PRPC_MESSAGE'
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(167) : error C2165: 'left-side modifier' : cannot modify pointers to data
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(167) : error C2513: 'void *' : no variable declared before '='
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(167) : error C2440: 'initializing' : cannot convert from 'std::ostream' to 'void *'
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(168) : error C2059: syntax error : ')'
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(226) : error C2146: syntax error : missing ')' before identifier 'RPC_MESSAGE'
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(226) : warning C4229: anachronism used : modifiers on data are ignored
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(226) : error C2491: 'I_RpcNegotiateTransferSyntax' : definition of dllimport data not allowed
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(227) : error C2059: syntax error : ')'
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(235) : error C2146: syntax error : missing ')' before identifier 'RPC_MESSAGE'
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(235) : warning C4229: anachronism used : modifiers on data are ignored
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(235) : error C2491: 'I_RpcGetBuffer' : definition of dllimport data not allowed
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(236) : error C2059: syntax error : ')'
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(242) : error C2146: syntax error : missing ')' before identifier 'RPC_MESSAGE'
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(242) : warning C4229: anachronism used : modifiers on data are ignored
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(242) : error C2491: 'I_RpcGetBufferWithObject' : definition of dllimport data not allowed
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(244) : error C2059: syntax error : ')'
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(251) : error C2146: syntax error : missing ')' before identifier 'RPC_MESSAGE'
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(251) : warning C4229: anachronism used : modifiers on data are ignored
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(251) : error C2491: 'I_RpcSendReceive' : definition of dllimport data not allowed
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(252) : error C2059: syntax error : ')'
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(259) : error C2146: syntax error : missing ')' before identifier 'RPC_MESSAGE'
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(259) : warning C4229: anachronism used : modifiers on data are ignored
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(259) : error C2491: 'I_RpcFreeBuffer' : definition of dllimport data not allowed
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(260) : error C2059: syntax error : ')'
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(267) : error C2146: syntax error : missing ')' before identifier 'PRPC_MESSAGE'
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(267) : warning C4229: anachronism used : modifiers on data are ignored
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(267) : error C2491: 'I_RpcSend' : definition of dllimport data not allowed
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(268) : error C2059: syntax error : ')'
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(275) : error C2146: syntax error : missing ')' before identifier 'PRPC_MESSAGE'
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(275) : warning C4229: anachronism used : modifiers on data are ignored
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(275) : error C2491: 'I_RpcReceive' : definition of dllimport data not allowed
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(277) : error C2059: syntax error : ')'
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(284) : error C2146: syntax error : missing ')' before identifier 'RPC_MESSAGE'
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(284) : warning C4229: anachronism used : modifiers on data are ignored
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(284) : error C2491: 'I_RpcFreePipeBuffer' : definition of dllimport data not allowed
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(285) : error C2059: syntax error : ')'
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(302) : error C2146: syntax error : missing ')' before identifier 'I_RPC_MUTEX'
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(302) : warning C4229: anachronism used : modifiers on data are ignored
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(302) : error C2182: 'I_RpcRequestMutex' : illegal use of type 'void'
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(302) : error C2491: 'I_RpcRequestMutex' : definition of dllimport data not allowed
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(303) : error C2059: syntax error : ')'
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(438) : error C2061: syntax error : identifier 'cout'
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(453) : error C2061: syntax error : identifier 'cout'
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(479) : error C2061: syntax error : identifier 'cout'
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(487) : error C2061: syntax error : identifier 'cout'
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(534) : error C2061: syntax error : identifier 'cout'
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(542) : error C2061: syntax error : identifier 'cout'
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(728) : error C2061: syntax error : identifier 'cout'
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(744) : error C2061: syntax error : identifier 'cout'
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(754) : error C2146: syntax error : missing ')' before identifier 'UUID'
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(754) : warning C4229: anachronism used : modifiers on data are ignored
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(754) : error C2491: 'I_UuidCreate' : definition of dllimport data not allowed
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(755) : error C2059: syntax error : ')'
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(763) : error C2061: syntax error : identifier 'cout'
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(771) : error C2061: syntax error : identifier 'cout'
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(779) : error C2061: syntax error : identifier 'cout'
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(838) : error C2061: syntax error : identifier 'cout'
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(848) : error C2061: syntax error : identifier 'cout'
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(873) : error C2144: syntax error : 'unsigned long' should be preceded by ')'
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(873) : error C2448: 'I_RpcConnectionInqSockBuffSize' : function-style initializer appears to be a function definition
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(874) : error C2144: syntax error : 'unsigned long' should be preceded by ';'
C:\Program Files (x86)\Windows Kits\8.1\include\shared\rpcdcep.h(874) : fatal error C1903: unable to recover from previous error(s); stopping compilation

If I don't #include windows.h, then those errors will not appear. I am using VS2013 with Qt 5.3.

user2621037
  • 326
  • 1
  • 3
  • 13
  • might be related: http://stackoverflow.com/questions/4913922/possible-problems-with-nominmax-on-visual-c – SleuthEye Jun 12 '14 at 13:59
  • Why don't you just `#include ` if you need to pull in GL? Also note that *GL* != *GLUT*. – peppe Jun 12 '14 at 14:02
  • I tried #ifdef NOMINMAX #include #else #define NOMINMAX #include #undef NOMINMAX #endif in http://stackoverflow.com/questions/4913922/possible-problems-with-nominmax-on-visual-c, the problem still exists – user2621037 Jun 12 '14 at 14:17
  • My program uses some glut functions that's why I include glut.h – user2621037 Jun 12 '14 at 14:18
  • @peppe: `#include ` indirectly includes ``. I think it is bad form to rely on that, but that is how the OP is currently getting away with this. Also, there are a handful of GLUT functions that are not related to context management, so it is *sometimes* meaningful to us Qt and GLUT together. I am guessing that is the case here. – Andon M. Coleman Jun 12 '14 at 17:42
  • @AndonM.Coleman: OP said "My program uses Qt and OpenGL", that doesn't include GLUT out of the box, hence why I asked for clarification. My point rests: use `#include ` if you need OpenGL, Qt will do the right thing on all platforms, including pulling in `windows.h` (under `NOMINMAX`). Then you can include GLU/GLUT. – peppe Jun 12 '14 at 20:07
  • I just do not see what the point of the GL != GLUT statement is though. Where was that implied? – Andon M. Coleman Jun 12 '14 at 20:11

2 Answers2

4

OpenGL on Microsoft Windows is tied to WGL, which is in turn tied to GDI.

As a result, you cannot #include <gl/gl.h> (you are indirectly doing this by including <GL/glu.h>) without first including some Windows-specific header that defines GDI/Windows pre-processor tokens such as WINGDIAPI and APIENTRY. But that is actually the extent to which any OpenGL program on Windows is tied to anything Windows-specific (header wise).

WinDef.h defines APIENTRY and WinGDI.h defines WINGDIAPI - including Windows.h brings in both of those headers (and a lot of other garbage unfortunately). So including Windows.h is mostly a convenience; to minimally compile OpenGL software on Windows you should #include <WinDef.h> followed by #include <WinGDI.h> and then finally #include <GL/gl.h>.

Alternatively, you can #define WIN32_LEAN_AND_MEAN just prior to #include <Windows.h> and it will significantly reduce the number of unrelated things that are brought in by including that header. Many Visual C++ projects actually define that pre-processor definition by default when they are first created, you might want to see if your project is configured that way.

Community
  • 1
  • 1
Andon M. Coleman
  • 42,359
  • 2
  • 81
  • 106
  • #define WIN32_LEAN_AND_MEAN before #include solves the my problem. And I also need to #include before #include , otherwise it says there is conflicting definition of the 'exit' function – user2621037 Jun 13 '14 at 03:10
  • Ah, yes. That second problem is documented in a few other questions on Stack Overflow, apparently that popped up around Visual Studio 2005 (8.0), but since GLUT is never updated you pretty much have to work around it yourself. – Andon M. Coleman Jun 13 '14 at 03:12
  • Thank you. I couldn't get glfw, glew & ENet to play nice together... finally figured out it was because ENet requires windows.h, which impacts glfw based on the inclusion order... but couldn't see how to avoid including it. Setting `WIN32_LEAN_AND_MEAN`, then including windows.h, then enet, then glew then glfw, finally did the trick. FWIW, I was getting "error: expected identifier or '(' before numeric constant" in some random location as a result. Not simple to solve! Most likely due to some astounding incompetence of my own, but just in case this helps someone... – Engineer Apr 13 '15 at 20:18
0

windows.h clutter the global namespace with tons of bad things like ERROR, Rectangle ...

One way around the problem is to declare needed functions your self (I guess this is not too mutch work since you will not need more than 10 functions or so). You can call the include file wgl.h or something.

__declspec(dllimport) void* __stdcall wglCreateContext(void* hdc);
//... Other functions you need

The OpenGL API is defined outside windows.h so no problem there

user877329
  • 6,717
  • 8
  • 46
  • 88