Hi again, got a new one here. Kind of surprised I've never hit up against this before.
I'd like to be able to specify a #define for the OS used in the build. That way, in various included header files, I can specify different versions of functions which contain OS-dependent code. Here's a non-working example of what I'd like the code to look like, though I suspect that this kind of thing is impossible using #if checks, even if I were to cloak them in macros. Is there another way?
Code:
//This is what I would like the code to resemble.
#include <iostream>
#include <string>
using std::cout;
using std::cerr;
using std::endl;
using std::string;
int OS_Table(const string& str)
{
if(str == "windows")
return 0;
else if(str == "mac")
return 1;
else if(str == "*nix")
return 2;
}
#define OS_BUILD OS_Table("windows")
#if OS_BUILD == OS_Table("windows")
void Function() { cout << "Windows" << endl; }
#elif OS_BUILD == OS_Table("mac")
void Function() { cout << "Mac" << endl; }
#elif OS_BUILD == OS_Table("*nix")
void Function() { cout << "*nix variant" << endl; }
#else
void Function() { cerr << "Invalid operating system." << endl; }
#endif
int main()
{
Function();
return 0;
}
Last edited by sarano; December 26th, 2009 at 06:53 AM.
You have to determine the OS based on preprocessor symbols defined by the compiler. If you're primarily worried about just windows, mac and linux, you can usually do something like:
Bookmarks