Why mandatory to include stdafx.h at the start? - c++

I have this extremely simple main function
#include "stdafx.h"
#include "abc.h"
int _tmain(int argc, _TCHAR* argv[])
{
abc obj;
obj.show();
return 0;
}
Everything is compiling normally...but when i am writing
#include "abc.h"
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
abc obj;
obj.show();
return 0;
}
The compiler is going haywire..
error C2065: 'abc' : undeclared identifier
error C2146: syntax error : missing ';' before identifier 'obj'
error C2065: 'obj' : undeclared identifier
error C2228: left of '.show' must have class/struct/union
type is ''unknown-type''
Why is it mandatory to include
stdafx.h
at the start?
I am new in C++ ...maybe I am making a silly mistake. Please help :(
(Using: VS2005 with C++ 98)

The issue that you're seeing is the fact that MS Visual C++ uses a feature called precompiled headers by default (other compilers on other platforms have a similar feature, I recall GCC for example having this feature). This ensures that all the code referenced up to the point of the precompiled header is pre-compiled, thus the compiler has less work to do at compilation time.
What happened when you switched that around was that it assumed that the contents of "abc.h" were already pre-compiled when they actually weren't. One easy solution would be to put #include "abc.h" inside stdafx.h. See here for a more details explanation of how the stdafx.h header works.
The precompiled headers option can be easily turned off in the compiler options. There should be a "precompiled headers/precompilation" category in the options, was in earlier Visual C++ IDE's I've encountered.

Related

C++ Xcode Library - Undeclared math.h features

I have recently attempted to switch my C++ project's target from an executable to a dynamic library (.dylib), and as soon as I rebuilt with the new target, I get a few errors saying that some defines and declarations in math.h are undeclared, such as M_PI and the sqrt() function.
The error message is as follows: error: use of undeclared identifier 'M_PI', and error: use of undeclared identifier 'sqrt'
This only occurs when I am building my project as a library, and I cannot figure out why it is doing this.
If anyone can help me out on this, it would be much appreciated!
Edit:
Also, if I try to change my include to #include <cmath>, I get more errors, such as: No member named 'signbit' in the global namespace.
Make sure #include <cmath> is at the top of the code, above int main(). I had the same problem. moving the #include <cmath> below <iostream> and above int main() solved the problem for me.

Compile error in a short OpenGL code: 'PFNGLBINDPROGRAMARB' : undeclared identifier

The short code is as follows:
#include <windows.h>
#include <GL/gl.h>
#include <GL/glext.h>
int main(int argc, char** argv)
{
PFNGLBINDPROGRAMARBPROC glBindProgramARB;
glBindProgramARB=(PFNGLBINDPROGRAMARB)wglGetProcAddress("glBindProgramARB");
}
When I compile, I got a compile error: error C2065: 'PFNGLBINDPROGRAMARB' : undeclared identifier.
I have checked the header file glext.h but PFNGLBINDPROGRAMARBPROC is in it (Line 2922). I have no idea so post my question here for help.
I'm using Visual Studio 2012 and I have checked the include directory settings. Other system info: Windows 7 64 bit, nVidia Geforce card with latest driver installed. If you need any other related info please ask.
It's a compile error, not a link error so it has nothing to do with the existence of the extension function glBindProgramARB (It does exist in my system, I checked using glutGetProcAddress).
I know FreeGlut and GLEW library functions can help me do the same thing. But I'm just wondering why the above code can not pass the compiler and how to fix it.
PFNGLBINDPROGRAMARBPROC is defined, but PFNGLBINDPROGRAMARB is not. You use the first of these two in the declaration, but the second in the type cast:
PFNGLBINDPROGRAMARBPROC glBindProgramARB;
glBindProgramARB=(PFNGLBINDPROGRAMARB)wglGetProcAddress("glBindProgramARB");

Namespace compilation issues

I am new to working in Visual Studio (am using version 2005). I am running into a problem with namespaces that I am not able to figure out.
I am trying to create a static library which I will link to an Application later.
So, I have a XXX.h file with the following code
#ifndef _XXX_X_H
#define _XXX_X_H
namespace LLL_NWK
{
void lllInit();
}
#endif
I include XXX.h in XXX.c and the code looks like
#include "XXX.h"
using namespace LLL_NWK;
void lllInit()
{
}
However, when I build the Library I encounter the following errors
error C2061: syntax error : identifier 'LLL_NWK'
error C2059: syntax error : ';'
error C2449: found '{' at file scope (missing function header?)
error C2059: syntax error : '}'
I am unable to figure out the cause of this error. Would appreciate some help and pointers.
First, using namespace LLL_NWK is not appropriate here. You are declaring and defining a function void lllInit() outside of namespace LLL_NWK. You need to place the definition inside of the namespace, which can be done like this:
void LLL_NWK::lllInit()
{
}
or like this:
namespace LLL_NWK
{
void lllInit()
{
}
}
Second, make sure you compile the code as C++.
That code is not supported by the C compiler - makes sure to rename the filename to .cpp instead .c. In this case, namespace is not supported. See this post: Namespaces in C

Is it possible to force visual studios to throw an error if an identifiers is used when its library isn't directly included in the source?

When compiling the following source:
int main()
{
exp(1.0);
return 0;
}
the copiler gives the following error: error C3861: 'exp': identifier not found because I didn't have the line: #include <iostream> above main()
However, visual studios won't display the error if a library is indirectly included. For example, the following code compiles without a problem even though the dependency of exp is in <cmath>.
#include <istream>
int main()
{
exp(1.0);
return 0;
}
This is because <iostream> includes <istream> which include <ostream> which includes <ios> which includes <xlocnum> which includes <cmath>.
Is there a way to make visual studios throw an error if I don't explicitly include a library yet try to use one of its identifiers?
You may want to have a look at include-what-you-use. It is a clang-based tool trying to detect missing and superfluous include directives.

Strange C2065 error in extern library

I have an extern code written in C in my C++ project in MS VS 2010. It comes with .h and .c files and I use include like this:
extern "C"{
#include "Extern\libname.h"
}
It worked nice. Then I read Google C++ style guide and removed all entries of
using namespace std;
replaced them with just
std::whatINeed
everywhere.
And the error showed up -
error C2065: 'FILE' : undeclared identifier
in the header of extern library. And then I move cursor to the word "FILE" in that code, there is
tydef _iobuf FILE
What should I add into my code to fix it? I don't want to change extern code because it can be updated and I will be forced to add my fixes on every update. Also, I am sure that there is a simple bug caused by me.
add
#include <cstdio>
above you include.