Page 1 of 1
Multiple of the same function?
Posted: Sat Dec 11, 2010 5:19 am
by MadPumpkin
Alright I am making an engine and API. I may be using different I/O such as GLUT, SDL, Win32 depending on the game I'm working on at the time, but I don't want to make an entirely different library and compile it just to change 3 lines of 5 functions. So does anyone know a way that I can put something in my code so that maybe something like defining a constant will make the compiler use different version of the function so that I can have a Win32 specific one, a SDL specific one and a GLUT specific one?
Much appreciated, Josh.
Re: Multiple of the same function?
Posted: Sat Dec 11, 2010 1:24 pm
by dandymcgee
Code: Select all
#ifdef MP_WIN32
void DoStuff()
{
//Win32 code
}
#endif
#ifdef MP_SDL
void DoStuff()
{
//SDL code
}
#endif
#ifdef MP_GLUT
void DoStuff()
{
//GLUT code
}
#endif
Then just put "#define MP_SDL" before including any of the files set up like this.
"MP" is for MadPumpkin.. call the variables whatever you like.

Re: Multiple of the same function?
Posted: Sat Dec 11, 2010 3:43 pm
by MadPumpkin
dandymcgee wrote:Code: Select all
#ifdef MP_WIN32
void DoStuff()
{
//Win32 code
}
#endif
#ifdef MP_SDL
void DoStuff()
{
//SDL code
}
#endif
#ifdef MP_GLUT
void DoStuff()
{
//GLUT code
}
#endif
Then just put "#define MP_SDL" before including any of the files set up like this.
"MP" is for MadPumpkin.. call the variables whatever you like.

Damn, exactly what I was looking for, I knew it had something to do with this because I recalled it from something Falco said about LibVorbis in a video. Thanks man great help xD
I guess it makes plenty of sense after looking at the fact that I put
Code: Select all
#define _WHATEVERSHIT_H_
#ifndef _WHATEVERSHIT_H_
#endif
In every header anyways. So what is the difference between #ifdef and #ifndef
Re: Multiple of the same function?
Posted: Sat Dec 11, 2010 4:06 pm
by dandymcgee
MadPumpkin wrote:
Damn, exactly what I was looking for, I knew it had something to do with this because I recalled it from something Falco said about LibVorbis in a video. Thanks man great help xD
I guess it makes plenty of sense after looking at the fact that I put
Code: Select all
#define _WHATEVERSHIT_H_
#ifndef _WHATEVERSHIT_H_
#endif
In every header anyways. So what is the difference between #ifdef and #ifndef
No problem and yes, this is exactly how header include guards work.
#ifdef means if defined, #ifndef means if not defined. So actually, your include guard example is backwards and that code will never be included.

Re: Multiple of the same function?
Posted: Sat Dec 11, 2010 5:36 pm
by MadPumpkin
Soo...
Code: Select all
#define _HEADER_H_
#ifndef _HEADER_H_
class someshit{
};
#endif
basically says,
Code: Select all
Define header as _HEADER_H_
If it wasn't defined, then do all of this code
end if
I'm confused then, shouldn't it only do the code if it WAS defined? Because with the #ifndef, it still includes my class.
Re: Multiple of the same function?
Posted: Sat Dec 11, 2010 5:52 pm
by dandymcgee
MadPumpkin wrote:
I'm confused then, shouldn't it only do the code if it WAS defined? Because with the #ifndef, it still includes my class.
I told you, your example header guard is backwards.
Code: Select all
#ifndef _HEADER_H_
#define _HEADER_H_
class someshit{
};
#endif
If not defined, define it and include header. If already defined skip to endif (do not include header twice).
Re: Multiple of the same function?
Posted: Sat Dec 11, 2010 6:10 pm
by MadPumpkin
dandymcgee wrote:MadPumpkin wrote:
I'm confused then, shouldn't it only do the code if it WAS defined? Because with the #ifndef, it still includes my class.
I told you, your example header guard is backwards.
Code: Select all
#ifndef _HEADER_H_
#define _HEADER_H_
class someshit{
};
#endif
If not defined, define it and include header. If already defined skip to endif (do not include header twice).
Thanks great help man.