Problem with include guard - c++

When I add an include guard to my header file for a Visual C++ project, it gives me the following warning and error:
warning C4603: '_MAPTEST_H' : macro is not defined or definition is different after precompiled header use
Add macro to precompiled header instead of defining here
.\MapTest.cpp(6) : use of precompiled header** // the precompiled header stdafx.h is included in this line
.\MapTest.cpp(186) : fatal error C1020: unexpected #endif
but when I add the precompiled header before the include guard, no warning or error is emitted. What is the reason for this?

Two problems I can think of:
According to this, Visual C++ won't compile anything before the line where you include stdafx.h - so that line needs to be the very first one in the file. If you put it after the macro definition, it gets skipped, hence the errors you're seeing.
Identifiers starting with a leading underscore and a capital letter (or double leading underscores) are reserved, which might be causing a name conflict. see this answer for more details.

Try opening stdafx.cpp and add your macro definition there!
I hope your problem is solved

Related

Why does force including precompiled headers work at all?

Apparently it is recommended to force include precompiled headers, so that the source may be used with and without precompiled headers. Even CMake uses the force include method on precompiled headers.
However for example Microsofts MSVC documentation says:
The compiler treats all code occurring before the .h file as precompiled. It skips to just beyond the #include directive associated with the .h file, uses the code contained in the .pch file, and then compiles all code after filename.
and for the /FIoption the documentation says:
This option has the same effect as specifying the file with double quotation marks in an #include directive on the first line of every source file specified on the command line, in the CL environment variable, or in a command file.
So to summarize this: All includes that are above/before an include directive of a corresponding precompiled header, will be precompiled into the PCH file. Force-including that file will put this in the first line of a file.
My question now is: How does this work together? Is there some special logic for PCH files during build process or am I missing something?
Update: I checked the gcc documentation as well and it looks like that gcc searches for precompiled headers for each include directive it finds and uses precompiled headers if possible. So do I understand it correctly that MSVC kind of summarizes all includes before (including the header itself) an include that should be used to generate a precompiled header, where gcc instead generates a precompiled header for that specific header?
If that's right it arises the question: Is gcc really way more flexible regarding preocmpiled header usage and how easy it is to gain a performance boost during compilation out of it?

How do I stop an IntelliSense PCH Warning?

A few of my header files have no includes, so I receive this message in Visual Studio 2010:
IntelliSense: PCH warning: cannot find a suitable header stop location. An intellisense PCH file was not generated.
If I add a single header, for instance:
#include <iostream>
It disappears. How can I stop this error from showing without adding (potentially unused) include>
When adding a .cpp file it inherits the PCH settings of the project.
More detailed explanation of the problem here
Solutions:
Add #pragma once at the start of the file.
It will cause your source file to be included only once in a single compilation, therefore the compiler will be satisfied and won't require additional #include
Setting your project to not use precompiled headers
Disable PCH usage for that one cpp file you've added, which will clear both IntelliSense and compiler warning/error.
Note! I'm including num 2, and 3 because some say it helped, but it only num 1 that did solve my case.
I suppose the problem is that you have precompiled header in your project (by default "stdafx.h") and to correctly solve the problem you should add
#include "stdafx.h"
at start of your header/source file.
Go to project's property and under C/C++ => Precompiled Headers, find the option "Precompiled header".
Change it to "Not Using Precompiled Headers".
Restart Visual Studio (close all active projects).
Nothing helped me except this

including secExt.h causes errors

I'm trying to include secExt.h in my C++ project and it gives me the error:
error C2086: 'BOOLEAN SEC_ENTRY' : redefinition
How can I fix it?
EDIT: from microsoft's docs, include security.h, not [secext.h].
original answer (before I googled the header name myself, no info given by OP):
if the header is yours, add
#pragma once
at the top.
if that doesn't work (there is reportedly a compiler on some IBM system or something that doesn't support #pragma once), then use a header guard.
if the header isn't yours, create a header wrapper like this:
#pragma once
#include <secExt.h>
then include your wrapper header instead of including [secExt.h] directly.

How to fix header file error?

So, my question is how to fix some error in header file, to run program normally? For example I use c++ builder 2010 and when winuser.h file is included, the program always get error like this
Checking project dependencies...
Compiling Project7.cbproj (Debug
configuration) [BCC32 Error]
winuser.h(47): E2257 , expected Full
parser context
File6.cpp(4): #include c:\program files (x86)\embarcadero\rad
studio\7.0\include\winuser.h [BCC32
Error] winuser.h(48): E2257 , expected
Full parser context
i try to replace that file with original from default installation, but that still get same error, how to fix that?
The error is almost certainly caused by whatever code appears before line 4 of File6.cpp. Most likely that is another header file, in which case it is likely that the code therein is malformed - a missing semicolon or brace for example.
The quickest way to verify that winuser.h is not the issue is to change the order of inclusion so that winuser.h is included first.
Another possibility is that something in winuser.h is dependent on some other header not previously included or directly included in winuser.h. Most Win32 API headers are included by windows.h, and it is generally advisable to include windows,h rather than either of its children.
The message is hard to read but the actual error is "E2257 , expected" (coma expected)
From the RAD studio documentation:
A comma was expected in a list of declarations, initializations, or parameters.
This problem is often caused by a missing syntax element earlier in the file
or one of its included headers.
The error message give you the line where it happened and you should probably look before that. There is probably some '}', ')' or ';' or other syntaxic closer missing in your code just before the error (likely before inclusion of the header file in your code). Full error message (you truncated it) or actual code would make it easier to spot.
It is also possible, even if unlikely, that the error is in one of the headers included in winuser.h.

my code won't compile

i get an error "missing ; before identifier".
no ; is missing.
this error appears only when i include one of the files:
MAPIDefS.h
Windows.h
wtypes.h
WinBase.h
what could be the problem?
Thanks a whole lot. I tried everything everybody suggested and it worked. The problem was I used a name in my file which was also defined in these header files.
All of these include files, except for MAPIDefs.h, are already included by Windows.h.
Manually including them will cause obscure errors.
You should remove all of the includes except for Windows.h and MAPIDefs.h.
Check the file which includes that header file, or any previous includes. Sometimes the error may propagate from previous files.
The most common mistake which gives such strange errors is omitting the semicolon ; after a class definition in one of the headers.
That's all I can say without seeing any code, hope that helps.
include windows.h before other windows api files.
If you have other include files or #define statements before the windows api files then you probably have define a symbol that is used in the API files. You should check that.