5

Are there any C++ compile time macros which exists to detect which Windows OS the code is being compiled on. I basically want to support certain functions only on Win7. So I am interested in doing something like this

#if <os_macro> = WIN7
  // This function would do something valid only on Win7 builds.
  bool myfunction {
        // do something here
  }
#else
  // This function would typically return false, since its not supported on OS below win7
  bool myfunction {
       return false;
  }
#endif 

Is there any other better way to do this?

2
  • 5
    How is which OS it's compiled on helpful? Doesn't it need to know which OS it's running on? Commented Apr 11, 2012 at 18:49
  • 2
    In Visual Studio you can use #ifdef WIN32 or #ifdef _WINDOWS for cross-platform. If you just want windows version you normally define WINVER yourself. For Windows 7 #define WINVER 0x0601 Commented Apr 11, 2012 at 18:56

6 Answers 6

11

The OS that it's getting compiled on is not all that important; what matters more is the OS that the code is running on, which you obviously cannot detect at compile time. But if you want your code to run on older versions of Windows, you can set WINVER and _WIN32_WINNT to certain values, which will cause newer functions not to be available etc. (just search the Windows header files for where those macros get tested to get an idea).

To test for functionality at runtime, use GetProcAddress (and possibly also LoadLibrary, if it's in a newer DLL) to test if the function is available. If it is, call it, if not, don't.

See also the predefined macros used by the Visual Studio compiler if you want to detect the compiler version etc.

Sign up to request clarification or add additional context in comments.

2 Comments

Basically I should be able to say, this is a Win7 build which has certain capability, and this is a WinXP build which does not. Here I would be defining what the capability should be.
Why do you want separate builds? Why not have one build that behaves differently depending on what OS it's running on?
1

There are a set of standard windows header macros to tell you the exact version of the OS

1 Comment

How do I those macros to do the comparisons? I tried with Winver like this #if WINVER >= _WIN32_WINNT_WIN7, but it didn't work very well.
1

There are certain things that do require knowing which OS version at compile time.

For example import "winhttp.dll" will compile under Windows 7, but cause compile time error (C1083) under Windows 10.

But if you switch to import "winhttpcom.dll". It would compile under Windows 10 but fail under Windows 7.

So an OS macro is needed here to import appropriate dll.

1 Comment

how to fix this?
0

At least with MS VC++, WIN32, _WIN32, and _WIN32_WINNT, for starters. Unless you need to control it at compile time, you might consider using something like GetVersionEx to detect at run-time, so the same build runs on older versions, but takes advantage of new features when they're available.

Comments

0

My response is 10 yrs late but posting the answer for upcoming yrs.

For detecting the version c/c++ compiler version can be used. Suitable macro is mentioned below:

_MSC_VER 

_MSC_FULL_VER

Reference: MS macros

1 Comment

No, you can compile with just about anything else than MSVC on Windows, for Windows. (Note: the OP said "I basically want to support certain functions only on Win7".)
-1

My two cents for people that want to compile / include differently between CONSOLE and WIN32 App under visual Studio (2017) in my case.

You you use wizard to create a Console App, You will have:

WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)

under: enter image description here

If You use wizard for win32 GUI App:

WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions) so NO _CONSOLE.

enter image description here

so You can write:

#ifdef _CONSOLE
// for Console
#else
// for GUI


#endif // _CONSOLE



int main()
{
#ifdef _CONSOLE
    // for Console
#else
    // for GUI


#endif // _CONSOLE


    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.