When using #ifndef for #include guards:
#ifndef BLAH_H
#define BLAH_H
// code
#endifmake sure that #ifndef and #define are the first lines of the archive!
I had placed some comments before them and that made me lose several hours, trying to find out why class forward declarations didn’t work as they should. One could expect the compiler to ignore comments automatically but that’s not the case.
I also looked for something as #pragma once for GCC, since I remembered that from when I used Visual C++, but although it was supported at some time, it is now deprecated, so the only solution is placing the #ifndef where it must be (at the very beginning).
Oh my…, That’s the first or the second Commandment ? XD
Uhm…. I can’t think of any example where writing comments above an include guard can cause a class forward declaration not work (it’s something I used to do a lot back when I did C/C++). Can you show an example?
Definitely the first one! or you won’t get the program to compile :-P
Nena! Que esto lo enseñan en la primera clase de C!!!
No! Ensenyan los includes :P
No, enseñan a no poner nada nada nada antes de los #includes, #defines o cualquier otra #cosa
De hecho, lo mas importante es el printf() en la primera clase :P
Y el #include stdio.h y el conio.h que siempre da mucha risa
Si si. Esto me recuerda en la uni, una chica que no estaba “muy enterada” de C, vio el codigo de nuestra practica y pensaba que conio.h era una broma.
Vino totalmente indignada: Oye, pase que llameis “chufa” a las funciones, pero un fichero llamado conio.h? Creo que os habeis pasado!
Can you give some details about the problem? (a test case would be great). I can’t think of any situation where comments might cause a problem like that, and preprocessor directives are supposed to work anywhere in the file :P
Yes:
/************************************
A certain class
@author blabla
(C) My company
************************************/
#ifndef CERTAINCLASS_H
#define CERTAINCLASS_H
class CertainClass { ... };
#endif
That does not work.
It seems to work here:
#include guard after comment
.
The only reason the code above does not work should be the missing ‘;’ after the class definition, but you wouldn’t have lost hours in THAT (yes, I know I’m being pedantic here, but that’s required when talking about compiler quirks ;), so there must be another cause. This is an interesting bug :)
:-P
I had the ‘;’ after the class, that was an example only. I’ll fix that!
I’ll check again just in case it’s an Apple+GCC bug O_O
Or maybe an Apple+GCC+Sole bug :D
My first comment seemed to get lost somehow (I asked for a test case too, like slack)… I’ve been doing exactly that (comments before include guards) many times and have never had a problem with that, either with gcc, borland c, or visual c (all under Windows/DOS).
Can’t figure what the problem with comments before include guards could be. Even if the compiler did not ignore the comments (which it does), I can’t see how that could change even a bit of it. #including a file is just embedding that file on the stream that goes to the code compiler… I can’t see how having comments -anywhere- could make it fail, unless it’s certainly a bug in the compiler.
[...] del primer mandamiento, viene el de “evitarás la [...]