Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

including windows.h causes clashing with local variable name

I am including windows.h in one of my h files (in order to use CaptureStackBackTrace), in a Visual-Studio project. At first I got some compiler errors because of the use of min/max std methods and the macro with same name in windows.h, but this seems to be solved by #define NOMINMAX, as I read in other SO posts. (I say "seems" because I can't be sure till my whole project builds ok again).

The problem is that some local variable names now break the build. The line:

int grp1;

inside a class method, causes the following error:

error C2143: syntax error : missing ';' before 'constant'

while the cpp file compiles ok if I change the variable name to grp1_.

Of course I can just change the variable name, but nevertheless I've got a feeling that I am doing something wrong - am I? Or is this a known issue when including windows.h? Is there any other, more elegant solution other than changing the variable name?

like image 923
Itamar Katz Avatar asked Jul 13 '11 12:07

Itamar Katz


People also ask

What is #include Windows h in C++?

h is a Windows-specific header file for the C and C++ programming languages which contains declarations for all of the functions in the Windows API, all the common macros used by Windows programmers, and all the data types used by the various functions and subsystems.

Why is Windows H not working?

h'". That problem happens because the file, which is needed to compile programs that make calls to the Windows operating system, is not installed. To fix this, download and install the Microsoft Windows SDK for your system (it is free). Once the SDK is installed, add the file paths to Visual Studio.

Does Windows H work on Mac?

You cannot get Windows. h for mac, it is Windows OS specific. There are many alternatives to functions used in Windows. h on the other hand.


3 Answers

dlg.h contains the line

#define grp1        0x0430

You could exclude it by defining WIN32_LEAN_AND_MEAN.

like image 90
Henrik Avatar answered Nov 15 '22 18:11

Henrik


It is, to a certain degree, a problem with all library headers. C and C++ reserve names beginning with an _ for the implementation (of the standard library). Other libraries are on their own. One would hope for a namespace, or if the library must be compilable in C, some sort of naming convention, such as a prefix, but neither Windows nor Posix even do this. (In the early days, the headers for X contained a #define String, which caused no end of problems with other libraries.)

In the end, there isn't any good solution. You just have to wait until you're hit by it, and then change the name of your function. (windows.h is particularly bad, because most of the “functions” in it are in fact macros, and don't obey scope.)

like image 26
James Kanze Avatar answered Nov 15 '22 18:11

James Kanze


Well, grp1 is defined as so in dlgs.h:

#define grp1        0x0430

and dlgs.h is included in windows.h, hence the clash.

You can #undef grp1 after you #include <windows.h>. Not the prettiest site, but if you include windows.h only once, you can keep those fixes at one place.

like image 36
eran Avatar answered Nov 15 '22 17:11

eran