Jump to content
  • Advertisement
Sign in to follow this  
CodeZero

#define not working

This topic is 2562 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
#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

Share this post


Link to post
Share on other sites
Typically such a situation would be handled by setting up the #define across the whole project, e.g. in your toolchain/project settings.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!