gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > Preprocessor #ifndef not working?

#12084 - Ruiner - Wed Oct 29, 2003 4:01 am

Correct me if i'm wrong but isn't #ifndef supposed to prevent you from getting multiple definition errors? When I include my "reusable" code across diferent classes that need it I get tons of multiple definition errors. Now I know extern is supposed to fix it and I have used it in various places but I have books and books on c++. I've been defining needed variables and typedefs this way for years and I've never had to use extern before now. Is this somethign specific to devkitadv? And sadly if this is poor programming practice then how do I get ahold of sams publishing and some of the other companies that I buy books from that dont mention this? (chuckle) I checked my fave c++ book and it mentions the extern command 3 times in the entire 846 page book (including source).

#12085 - tepples - Wed Oct 29, 2003 4:09 am

#ifdef is for multiple declaration errors emitted by the compiler. Multiple definition errors emitted by the linker are a different thing entirely. What you'll want to do is put your class's non-inline methods into a separate .cpp file and link that.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#12090 - Ruiner - Wed Oct 29, 2003 6:50 am

Man, im used to visual studio and just hitting the compile button so I never think to separate that its a compiler or linker section of the process. Sorry about the stupid post.

And yes my methods are declared in diferent cpp files. Its all these tutorial sites with their own headers that are set up for 'C'. I've been adding them in with what I have of an object oriented structure so far just to test them and learn. My little rant was because I have declared variables outside of classes inside my headers before. Ive never had to use extern. Anyone know if visual studio just catches this on its own? I always assumed it was the #ifndef that handled it.