Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fix macro redefinition in C++

Since intsafe.h and stdint.h both define INT8_MIN. Thus VS2010 generate a warning that says :

    1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\stdint.h(72): warning C4005: 'INT8_MIN' : macro redefinition
1>          C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\intsafe.h(144) : see previous definition of 'INT8_MIN'

Is there a way to fix that warning in VS2010.

like image 250
Moez Rebai Avatar asked Aug 08 '14 10:08

Moez Rebai


People also ask

Can you redefine a macro in C?

Once you've defined a macro, you can't redefine it to a different value without first removing the original definition. However, you can redefine the macro with exactly the same definition. Thus, the same definition may appear more than once in a program. The #undef directive removes the definition of a macro.

Can we redefine a macro?

A macro is a piece of code in a program that is replaced by the value of the macro. Macro is defined by #define directive. Whenever a macro name is encountered by the compiler, it replaces the name with the definition of the macro.

How do I Undefine a macro?

Macros can be undefined from the command line using the /U option, followed by the macro names to be undefined.

What is the use of #define directive?

The #define directive causes the compiler to substitute token-string for each occurrence of identifier in the source file. The identifier is replaced only when it forms a token. That is, identifier is not replaced if it appears in a comment, in a string, or as part of a longer identifier.


2 Answers

Apparently it's a bug in VS2010. You can avoid it in general but in MFC applications it's basically impossible to ever include stdint.h in any of your other code without hitting it.

I just did this at the top of the file that was complaining:

#pragma warning (push)
#pragma warning (disable : 4005)
#include <intsafe.h>
#include <stdint.h>
#pragma warning (pop)

It gets those headers 'out of the way' so to speak and lets you get on with your day.

like image 162
Chris Warkentin Avatar answered Sep 22 '22 02:09

Chris Warkentin


In order simply to make the message go away, you can add the line

#pragma warning (disable : 4005)

before your first #include statement

But that doesn't mean you shouldn't heed the warning. See if you can do without one of the two header files, and if not, be very certain of which definition your program is using.

like image 33
Logicrat Avatar answered Sep 19 '22 02:09

Logicrat