Today, when compiling OpenGL Red Book with the light.c file in the source code, you encounter a strange problem:
Light. c, compile OK without making any changes. However, if you write executable code in some places, you will not be able to compile through the compiler!
(These lines of code are OK if the first sentence in the main function is written)
I used the VS08. I sent the document to other friends (VS10), which is the same problem.
However, after I changed the file name to Light.cpp, the problem was solved.
The question now is, how will the compiler be affected by the suffix behind the code file?
You can do a simple test:
[CPP]View PlainCopy
- int main ()
- {
- #ifdef __cplusplus
- printf ("defined __cpluslus");
- #else
- printf ("undefined __cpluslus");
- #endif
- return 0;
- }
The test results are: If the CPP file is compiled, the compiler will automatically help you to join the __cplusplus macro and use the C + + compilation rules. This point of MSDN has in fact clearly stated:
Give the file a. c extension, such as mysource.c.
The Visual C + + compiler automatically assumes the band. The C extension file is a C file instead of a C + + file and rejects C + + syntax and keywords (such as public,private, and Class).
C + + files use the. cpp extension.
One more non-mainstream test. What if the compilation is neither a. c file nor a. cpp file?
The author changed the file extension to light. DD, the result is that the defined __cplusplus is displayed. That is, the VS default, except for. c files, is considered to be a C + + file to compile.
Oddly, this is what is described on MSDN:
The file name extension determines how the file is processed. C and C + + files with the extension. C,. cxx, or. cpp will be compiled. Other files, including. obj files, library (. lib), and module definition (. def) files, are passed to the linker without processing.
Now back to the original question.
Why does light.c add the execution code (even the empty code) to compile? If this is because of a compiler problem, why not add those lines of "devil Code" before the compile OK?
Is this just a compiler bug?
It's clear.
The C language can only declare variables at the top of the curly braces ...
Hang Daddy. http://blog.csdn.net/lsldd/article/details/6890943
The
Visual C + + compiler automatically assumes the band. The C extension file is a C file instead of a C + + file, and the C + + syntax and keywords are rejected (the C language can only declare variables at the front of the curly braces)