CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Threaded View

  1. #1
    Join Date
    Jul 2009
    Posts
    15

    Using #define to check operating system

    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 07:53 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured