Skip to main content
GameDev.net gamedev.net
🔒 Locked

#define not working

Started by CodeZero Dec 7, 2011 at 11:45 PM 2 replies 1.1k views
Original Post
CodeZero
CodeZero
I have 3 files:

main.cpp

#define TESTING123
#include "other.h"

int main ( ) {

x++;

return 0;
}



other.h

#ifndef OTHER_H
#define OTHER_H

#ifdef TESTING123
extern int x;
#endif

#endif



other.cpp


#include "other.h"

#ifdef TESTING123
int x= 0;
#endif



I get a linker error for x being unresolved. Why?

Thanks,
Jake
Hodgman
Hodgman
#include just means "copy and paste this file here", so if you do the C&P manually, you get:
main.cpp#define TESTING123
#ifndef OTHER_H
# define OTHER_H
# ifdef TESTING123
extern int x;
# endif
#endif
int main ( ) {
x++;
return 0;
}
other.cpp#ifndef OTHER_H
# define OTHER_H
# ifdef TESTING123
extern int x;
# endif
#endif
#ifdef TESTING123
int x= 0;
#endif

And if you then resolve the #if conditions manually, you get:
main.cppextern int x;
int main ( ) {
x++;
return 0;
}
other.cpp
rip-off
rip-off
Typically such a situation would be handled by setting up the #define across the whole project, e.g. in your toolchain/project settings.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.