Fix macro redefinition in C++ - 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.

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.

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.

Related

GDI+ library causes "error C2760: syntax error: unexpected token 'identifier', expected 'type specifier'" in VS2017 when compiled for XP

I'm trying to include the following definitions for GDI+ into my Win32 C++ project that is compiled under Visual Studio 2017:
#include <objidl.h>
#include <gdiplus.h>
#pragma comment (lib,"Gdiplus.lib")
I need to compile this project to support Windows XP. So in the project properies I selected: Platform Toolset as Visual Studio 2017 - Windows XP (v141_xp):
But when I compile it the GDI+ library gives me this:
1>c:\program files (x86)\microsoft sdks\windows\v7.1a\include\objbase.h(239): error C2760: syntax error: unexpected token 'identifier', expected 'type specifier'
1>c:\program files (x86)\microsoft sdks\windows\v7.1a\include\gdiplusheaders.h(891): error C4596: 'EmfToWmfBits': illegal qualified name in member declaration
1>c:\program files (x86)\microsoft sdks\windows\v7.1a\include\gdiplusstringformat.h(220): error C4596: 'GetTrimming': illegal qualified name in member declaration
Any idea how to fix this?
Add this line before the very first(!) #include of COM-related header to fix objbase.h(239): error C2760: syntax error: unexpected token 'identifier', expected 'type specifier' :
typedef struct IUnknown IUnknown;
This fix works, because the line in objbase.h(239) mentioned in the error contains static_cast<IUnknown*>(*pp); despite that IUnknown still haven't been declared in that place.
I kinda got it to compile, but this is definitely not a good solution. I'm posting it here as a temp workaround until Microsoft gets their heads out of their ___es. Also if anyone finds a better way, please let me know.
I basically had to downgrade the entire project to Visual Studio 2015 - Windows XP (v140_xp) just to compile one badly written library:
This created a problem of its own with the std libraries:
1>C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\cstdio(50): error C4995: 'sprintf': name was marked as #pragma deprecated
1>C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\cstdio(53): error C4995: 'vsprintf': name was marked as #pragma deprecated
1>C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\cstring(20): error C4995: 'strcat': name was marked as #pragma deprecated
1>C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\cstring(21): error C4995: 'strcpy': name was marked as #pragma deprecated
1>C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\cwchar(29): error C4995: 'swprintf': name was marked as #pragma deprecated
1>C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\cwchar(30): error C4995: 'vswprintf': name was marked as #pragma deprecated
1>C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\cwchar(32): error C4995: 'wcscat': name was marked as #pragma deprecated
1>C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\cwchar(34): error C4995: 'wcscpy': name was marked as #pragma deprecated
So I had to shunt those errors of unsafe functions:
#pragma warning( push )
#pragma warning( disable: 4995 )
#include <stdio.h>
#include <new>
#include <string>
#pragma warning( pop )
Which is far from ideal!
(You're basically sacrificing security of the app just to compile that damn GDI+ library.)
There's a way to get this to work if you're prepared to edit the Windows header files.
In objbase.h, comment out line 239 or change it to:
static_assert (std::is_base_of <IUnknown *, *pp>::value, "pp must derive from IUnknown");
In gdiplusheaders.h, line 891, remove the redundant qualifier (Metafile::).
In gdiplusstringformat.h, line 220, remove the redundant qualifier (StringFormat::).
Hopefully, that will fix things for you without breaking anything.
Although the question is old, just adding what worked for me.
In my case including windows.h and compiling with VS2017 v141_xp toolset was causing me error: syntax error: unexpected token 'identifier', expected 'type specifier'.
This resolved my issue link

Compiling Audacity with VisualStudio 2015 and C++ produces multiple C1189 & C4005 errors

I am using Visualstudio 2015. On Windows 10.I am trying to specifically use the latest VS and SDKs, I do not want to back track to VS2013.
I have gone through the entire code base of Audacity, and changed the instances of
#define snprintf _snprintf
to
#if _MSC_VER < 1900
#define snprintf _snprintf
#endif
However, on rebuilding - I'm still getting these errors.
22>C:\Program Files (x86)\Windows Kits\10\Include\10.0.10240.0\ucrt\stdio.h(1925): warning C4005: 'snprintf': macro redefinition
22> C:\Program Files (x86)\Windows Kits\10\Include\10.0.10240.0\ucrt\stdio.h(1925): note: command-line arguments: see previous definition of 'snprintf'
22>C:\Program Files (x86)\Windows Kits\10\Include\10.0.10240.0\ucrt\stdio.h(1927): fatal error C1189: #error: Macro definition of snprintf conflicts with Standard Library function declaration
I can not see how to get around this? Where is it colliding and thus producing the fatal error.
There are 24 Solutions in the build, and all but three of them build. Theo nes that don't build so far are:
libnyquist
libsndfile
lv2

Atlbase won't include properly in Unreal Engine 4

I'm using the full version of VS2013, and am trying to include atlbase into a class, along with sphelper, but I'm getting various types of errors.
I'm using a newly generated class, which will cleanly compile without these inclusions, and has pretty much nothing else inside of it.
The compiler is finding the libraries and seems to load them, but then I get around 20 errors that are all pretty much just like this (I omitted the rest, but they are all just like these ones)
1>E:\Programs\Microsoft Visual Studio 12.0\VC\ATLMFC\INCLUDE\atlcore.h(630): warning C4191: 'reinterpret_cast' : unsafe conversion from 'FARPROC' to 'BOOL (__cdecl *)(DWORD)'
1> Calling this function through the result pointer may cause your program to fail
1>E:\Programs\Microsoft Visual Studio 12.0\VC\ATLMFC\INCLUDE\atltransactionmanager.h(271): warning C4191: 'type cast' : unsafe conversion from 'FARPROC' to 'PFNCREATETRANSACTION'
1> Calling this function through the result pointer may cause your program to fail
1>E:\Programs\Microsoft Visual Studio 12.0\VC\ATLMFC\INCLUDE\atltransactionmanager.h(321): warning C4191: 'type cast' : unsafe conversion from 'FARPROC' to 'PFNCOMMITTRANSACTION'
1> Calling this function through the result pointer may cause your program to fail
1>E:\Programs\Microsoft Visual Studio 12.0\VC\ATLMFC\INCLUDE\atltransactionmanager.h(427): error C2039: 'DeleteFile' : is not a member of '`global namespace''
1>E:\Programs\Microsoft Visual Studio 12.0\VC\ATLMFC\INCLUDE\atltransactionmanager.h(448): warning C4191: 'type cast' : unsafe conversion from 'FARPROC' to 'PFNMOVEFILETRANSACTED'
1> Calling this function through the result pointer may cause your program to fail
1>E:\Programs\Microsoft Visual Studio 12.0\VC\ATLMFC\INCLUDE\atltransactionmanager.h(460): error C2039: 'MoveFile' : is not a member of '`global namespace''
1>E:\Programs\Microsoft Visual Studio 12.0\VC\ATLMFC\INCLUDE\atltransactionmanager.h(487): warning C4191: 'type cast' : unsafe conversion from 'FARPROC' to 'PFNGETFILEATTRIBUTESTRANSACTED'
1>E:\Programs\Microsoft Visual Studio 12.0\VC\ATLMFC\INCLUDE\atlbase.h(5766): warning C4191: 'type cast' : unsafe conversion from 'FARPROC' to 'LSTATUS (__cdecl *)(HKEY,LPCWSTR,REGSAM,DWORD)'
1> Calling this function through the result pointer may cause your program to fail
1>C:\Program Files (x86)\Windows Kits\8.1\include\um\sphelper.h(1333): warning C4191: 'type cast' : unsafe conversion from 'FARPROC' to 'LPFN_RegLoadMUIStringW'
1> Calling this function through the result pointer may cause your program to fail
These errors present themselves only after atlbase.h and/or sphelper.h are included. Half of them from the first, the other half from the second.
They are included as follows (beneath my project and class header inclusion):
#include <stdio.h>
#include <Windows.h>
#include "AllowWindowsPlatformTypes.h"
#include <atlbase.h>
#include "sphelper.h"
#include "HideWindowsPlatformTypes.h"
I have them within this 'platform types' block because the atlbase and sphelper libraries throw an obscene amount of errors each otherwise, pertaining to arbitrary declarations or something.
I have not edited the library files in any way, and completely deleted all of the libraries and reinstalled them from scratch.
It may be due to an oversight or something on my part, but can anyone explain why the atl and sphelper libraries won't include properly?
Edit:
To clarify, the solution to the problem in which I "resolved" that led to this problem, I found on "https://answers.unrealengine.com/questions/27560/trouble-using-windows-includes-with-dword-int.html"
I posted my problem on a more specific site, and got an answer there.
User Jamie Dale posted the following on UE4 AnswerHub
#include "AllowWindowsPlatformTypes.h"
#pragma warning(push)
#pragma warning(disable: 4191) // warning C4191: 'type cast' : unsafe conversion
#pragma warning(disable: 4996) // error C4996: 'GetVersionEx': was declared deprecated
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
// atltransactionmanager.h doesn't use the W equivalent functions, use this workaround
#ifndef DeleteFile
#define DeleteFile DeleteFileW
#endif
#ifndef MoveFile
#define MoveFile MoveFileW
#endif
#include <atlbase.h>
#undef DeleteFile
#undef MoveFile
#include <sphelper.h>
#pragma warning(pop)
#include "HideWindowsPlatformTypes.h"
This work around replaces the inclusions I used, and completely solved all of the problems I had. Full credit towards Jamie Dale on there.

What is causing this Visual Studio Pro 2013 Warning?

Every time I build my project my Build output window is filled (one instance for every cpp file) with
1>d:\program files\microsoft sdks\windows\v7.1\include\sal_supp.h(57): warning C4005: '__useHeader' : macro redefinition
1> d:\program files\microsoft visual studio 12.0\vc\include\sal.h(2886) : see previous definition of '__useHeader'
1>d:\program files\microsoft sdks\windows\v7.1\include\specstrings_supp.h(77): warning C4005: '__on_failure' : macro redefinition
1> d:\program files\microsoft visual studio 12.0\vc\include\sal.h(2896) : see previous definition of '__on_failure'
It's not really a problem except that it makes it hard to parse the actual build errors when I break something. How can I fix the root of this error? Or at least silence it?
From a comment under the original question:
VS2012 C++ warning C4005: '__useHeader': macro redefinition
Long story short, its a bug in VS, with no real workarounds.
I have chosen to just silence the specific warning code, which does silence all instances of macro re-definitions. But now I can actually read my build output so whatever I guess.
http://msdn.microsoft.com/en-us/library/jj715718.aspx

Inclusion of dshow.h results in definition errors

I am trying to do a few things using DirectShow for audio playback. I have a header file, at the top is:
#pragma once
#include <dshow.h>
#pragma comment(lib, "strmiids.lib")
and then it goes on to define a class.
When including dshow.h I get the following complilation errors:
C:\Program Files\Microsoft SDKs\Windows\v7.0\include\ddraw.h(703) : error C2011: '_DDPIXELFORMAT' : 'struct' type redefinition
c:\program files\microsoft sdks\windows\v7.0\include\ksmedia.h(5749) : see declaration of '_DDPIXELFORMAT'
C:\Program Files\Microsoft SDKs\Windows\v7.0\include\ddraw.h(2249) : error C2079: '_DDSURFACEDESC::ddpfPixelFormat' uses undefined struct '_DDPIXELFORMAT'
C:\Program Files\Microsoft SDKs\Windows\v7.0\include\ddraw.h(2292) : error C2079: '_DDSURFACEDESC2::ddpfPixelFormat' uses undefined struct '_DDPIXELFORMAT'
C:\Program Files\Microsoft SDKs\Windows\v7.0\include\strmif.h(12918) : error C2011: 'tagTIMECODE_SAMPLE' : 'struct' type redefinition
c:\program files\microsoft sdks\windows\v7.0\include\ksmedia.h(5274) : see declaration of 'tagTIMECODE_SAMPLE'
I can't figure out what would cause these errors in this case. The header file is part of an MFC project if that makes any difference. Any advice?
Fixed this by changing the order of the #include definitions. I moved the header file that the above code was defined in to the top and it works ok now. Must have been a clash with some code in another file, possibly some directSound related stuff.
I have faced this SDK integration error a couple times, most recently when integrating a win32 console app with a library that uses Windows CoreAudio and the error occurred with a stdafx.h:
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#ifndef _WIN32_WINNT // Allow use of features specific to Windows XP or later.
#define _WIN32_WINNT 0x0502 // Change this to the appropriate value to target other versions of Windows.
#endif
#include <stdio.h>
#include <tchar.h>
// TODO: reference additional headers your program requires here
#include <afx.h>
#include <afxwin.h>
Then to resolve the error, I added the following below the current includes:
#include <winioctl.h>
#if (MSC_VER < 1400)
#include <strmif.h>
#endif
Hopefully this will help someone in the future facing this issue.
EB