Why do I get an error while compiling this code? [duplicate] - c++

In my simple OpenGL program I get the following error about exit redefinition:
1>c:\program files\microsoft visual studio 8\vc\include\stdlib.h(406) : error C2381: 'exit' : redefinition; __declspec(noreturn) differs
1> c:\program files\microsoft visual studio 8\vc\platformsdk\include\gl\glut.h(146) : see declaration of 'exit'
I'm using Nate Robins' GLUT for Win32 and get this error with Visual Studio 2005 or Visual C++ 2005 (Express Edition). What is the cause of this error and how do I fix it?

Cause:
The stdlib.h which ships with the recent versions of Visual Studio has a different (and conflicting) definition of the exit() function. It clashes with the definition in glut.h.
Solution:
Override the definition in glut.h with that in stdlib.h. Place the stdlib.h line above the glut.h line in your code.
#include <stdlib.h>
#include <GL/glut.h>

or this...
To fix the error, right click on the project name in the Solution Explorer tab and select Properties -> C/C++ -> Preprocessor -> Preprocessor definitions and append GLUT_BUILDING_LIB to the existing definitions, seperated by semicolons.

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

Header for CStdioFile , if I am already including windows.h

Which header shall i use for CStdioFile?
The relevant doc is here
when I try including afx.h , i get following error( window.h is included before afx.h because of some other function):
Error 1 error C1189: #error : WINDOWS.H already included. MFC apps must not #include C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\atlmfc\include\afxv_w32.h
I wan to remain in native C++ world.
I am using visual studio 2013
If there is a basic setting in project properties that is relevant here, please let me know.

file writing in vs2010 (c++, win32)

I asked this question earlier and you told me to mention the errors,
so I will mention them now(I couldn't figure out how to continue the last thread I started,all I saw was an "add a comment" button and an "answer your question" button, so I had to correct the questio and ask it again, sorry about that):
My question is the following:
I'm using visual studio 2010 and I'm writing a win32 application(not a console application).
I need to know how to write to a file from this application.
I included these headers: windows.h, stdlib.h, string.h and tchar.h
I wrote a very simple hello world application and it ran just fine.
But when I tried to include iostream and fstream
in my project the compiler gave me the following errors.
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cstdlib(21): error C2039: 'abort' : is not a member of '`global namespace''
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cstdlib(21): error C2873: 'abort' : symbol cannot be used in a using-declaration
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cstdlib(24): error C2039: 'exit' : is not a member of '`global namespace''
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cstdlib(24): error C2873: 'exit' : symbol cannot be used in a using-declaration
IntelliSense: the global scope has no "abort" c:\program files (x86)\microsoft visual studio 10.0\vc\include\cstdlib 21 13
IntelliSense: the global scope has no "exit" c:\program files (x86)\microsoft visual studio 10.0\vc\include\cstdlib 24 13
And when I include fstream.h,I get:
error C1083: Cannot open include file: 'fstream.h': No such file or directory c:\users\user\documents\visual studio 2010\projects\helloworld\helloworld\main.cpp 5 1 helloworld
IntelliSense: cannot open source file "fstream.h" c:\users\user\documents\visual studio 2010\projects\helloworld\helloworld\main.cpp 5 1 helloworld
Same thing for iostream.h
Why are these errors occurring?
In C++, you should use <cstdlib> instead of <stdlib.h>, <cstring> instead of <string.h> (assuming you mean the C style strings. If you want C++ std::string, use <string> [without .h].
And you should use <fstream>, not <fstream.h>.
Please check that your :
#include "stdafx.h"
is the first include of you .cpp file.
Write other includes after this one :
#include "stdafx.h"
#include <iostream>
// ... and so on
int main(...) {
}
... this is a quite frequent mistake with Microsoft compilers (see C++ cout gives undeclared identifier).
Probably you wrote #include "iostream" instead of #include <iostream>
May be this helps .. Try to Check your errors reasons from
http://msdn.microsoft.com/en-us/library/et4zwx34%28v=vs.80%29.aspx

Identify which file has included some particular header file

Sometimes with a complex header structure it happens some header is included, but it is hard to tell where from.
Is there some tool (depedency viewer?) or a method how to find the "inclusion stack" (which source / which header / which header / ...) is including one particular header file?
If the header file is included multiple times, finding first inclusion is sufficient, finding all inclusions is a welcome bonus.
Someone has posted about it but I can't find this answer.
So, In VS, go to your project properties. Choose Configuration Properties / C/C++ / Advanced / Show Includes and set "yes".
then compile you cpp file. It looks like this:
cpp file:
#include <stdio.h>
int main()
{
return 0;
}
In the output window after compiling you will see:
1>------ Build started: Project: stlport_project, Configuration: Release Win32 ------
1>Compiling...
1>stlport_project.cpp
1>Note: including file: D:\src\hrs_rt_059.00\HRS\modules\src\libs\src\external\stlport\5.1.7\stdio.h
1>Note: including file: D:\src\hrs_rt_059.00\HRS\modules\src\libs\src\external\stlport\5.1.7\stl/_prolog.h
1>Note: including file: D:\src\hrs_rt_059.00\HRS\modules\src\libs\src\external\stlport\5.1.7\stl/config/features.h
and so on
EDIT: reference to the same question Displaying the #include hierarchy for a C++ file in Visual Studio
The header you are searching for may not be directly included into the source file. You need to 'preprocess_only' the code. This can be done in g++ by using the -E option; I don't know enough about visual C to know what the exact specification is there but if you look in the help for 'preprocess' you may come up with something.
A somewhat hacky approach (but one which should work on any platform/toolchain, without needing a separate dependency analyser) is simply to add a #error at the top of the included header - you will then get a compilation error from the first .cpp file which includes it.
Visual Studio /showIncludes
Directly in the Visual Studio I have found an option called /showIncludes - the output is textual only, but indented in a way which makes reading it quite easy:
Note: including file: /*..path.anonymized..*/\TCMalloc\windows\config.h
Note: including file: /*..path.anonymized..*/\memalloc\tcmalloc\windows/port.h
Note: including file: C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\include\windows.h
Note: including file: C:\Program Files\Microsoft Visual Studio 8\VC\include\excpt.h
Note: including file: C:\Program Files\Microsoft Visual Studio 8\VC\include\crtdefs.h
Note: including file: C:\Program Files\Microsoft Visual Studio 8\VC\include\sal.h
Note: including file: C:\Program Files\Microsoft Visual Studio 8\VC\include\vadefs.h
Note: including file: C:\Program Files\Microsoft Visual Studio 8\VC\include\stdarg.h
Note: including file: C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\include\windef.h
Note: including file: C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\include\winnt.h
Note: including file: C:\Program Files\Microsoft Visual Studio 8\VC\include\ctype.h
Note: including file: C:\Program Files\Microsoft Visual Studio 8\VC\include\crtdefs.h
ProFactor Include Manager
There is also a VS add-in called Include Manager which seems to provide the needed functionality in a very nice visual way.

GLUT exit redefinition error

In my simple OpenGL program I get the following error about exit redefinition:
1>c:\program files\microsoft visual studio 8\vc\include\stdlib.h(406) : error C2381: 'exit' : redefinition; __declspec(noreturn) differs
1> c:\program files\microsoft visual studio 8\vc\platformsdk\include\gl\glut.h(146) : see declaration of 'exit'
I'm using Nate Robins' GLUT for Win32 and get this error with Visual Studio 2005 or Visual C++ 2005 (Express Edition). What is the cause of this error and how do I fix it?
Cause:
The stdlib.h which ships with the recent versions of Visual Studio has a different (and conflicting) definition of the exit() function. It clashes with the definition in glut.h.
Solution:
Override the definition in glut.h with that in stdlib.h. Place the stdlib.h line above the glut.h line in your code.
#include <stdlib.h>
#include <GL/glut.h>
or this...
To fix the error, right click on the project name in the Solution Explorer tab and select Properties -> C/C++ -> Preprocessor -> Preprocessor definitions and append GLUT_BUILDING_LIB to the existing definitions, seperated by semicolons.