soledad penadés
repeat 4[fd 100 rt 90]

Mental note about #ifndef's

When using #ifndef for #include guards:

#ifndef BLAH_H
#define BLAH_H

// code

#endif

make 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).

// 15 responses to Mental note about #ifndef's

pplux
pplux
20080408

Oh my…, That's the first or the second Commandment ? XD

Jcl
Jcl
20080408

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?

sole
sole
20080408

Definitely the first one! or you won't get the program to compile :-P

Madieta
Madieta
20080408

Nena! Que esto lo enseñan en la primera clase de C!!!

sole
sole
20080408

No! Ensenyan los includes :P

Madieta
Madieta
20080408

No, enseñan a no poner nada nada nada antes de los #includes, #defines o cualquier otra #cosa

sole
sole
20080408

De hecho, lo mas importante es el printf() en la primera clase :P

Madieta
Madieta
20080408

Y el #include stdio.h y el conio.h que siempre da mucha risa

sole
sole
20080408

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!

slack
slack
20080408

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

sole
sole
20080408

Yes:

/************************************
 A certain class

@author blabla

(C) My company

************************************/

#ifndef CERTAINCLASS_H
#define CERTAINCLASS_H

class CertainClass {};

#endif

That does not work.

slack
slack
20080409

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 :)

sole
sole
20080409

:-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

Jcl
Jcl
20080410

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.

Luanatic con features » disléxicos del mundo unisors!!!
Luanatic con features » disléxicos del mundo unisors!!!
20080413

[...] del primer mandamiento, viene el de "evitarás la [...]

Feel free to leave a reply

Comments are moderated: Rude and offtopic ones are out!