是否有任何宏可以确定我的代码是否被编译到 Windows?

发布于 2024-07-10 18:41:34 字数 53 浏览 10 评论 0原文

我想检测我正在编译的操作系统是否是Windows。 我可以检查一个简单的宏来验证这一点吗?

I would like to detect whether the OS I'm compiling on is Windows. Is there a simple macro I can check to verify that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

若相惜即相离 2024-07-17 18:41:34

[编辑:我假设您想使用编译时宏来确定您所在的环境。 也许您想确定您是否在 Linux 下的 Wine 上运行或在 Windows 下运行,但一般来说,您的编译器针对特定环境,要么是 Windows (DOS),要么不是,但很少(从不) ?) 两者。]

一些编译器提供宏来指示 Windows 构建环境。 但这些会因编译器的不同而有所不同,甚至在 Windows 上的同一编译器上(如果目标环境不完全是 Windows)也会有所不同。 通常是 __WIN32__,但并非总是如此。

#if defined (__WIN32__)
  // Windows stuff
#endif

有时它可以是 _WIN32__CYGWIN32__,或者可能只是编译器指示符 (_MSC_VER)。

如果您知道要构建的环境(从 makefile 中),则通常可以在命令行上传入 #define,例如“g++ -D __WIN32__ yourfile.c< /代码>”。

这里有更多信息

[Edit: I assume you want to use compile-time macros to determine which environment you're on. Maybe you want to determine if you're running on Wine under Linux or something instead of Windows, but in general, your compiler targets a specific environment, and that is either Windows (DOS) or it isn't, but it's rarely (never?) both.]

Some compilers offer macros to indicate a Windows build environment. But these will vary from compiler to compiler, and even on the same compiler on Windows if the target environment is not exclusively windows. Usually it's __WIN32__, but not always.

#if defined (__WIN32__)
  // Windows stuff
#endif

Sometimes it can be _WIN32, __CYGWIN32__, or possibly just the compiler indicator (_MSC_VER).

If you know the environment you'll be building in (from the makefile) then you can usually pass in the #define on the command line, like "g++ -D __WIN32__ yourfile.c".

A little more info here

旧竹 2024-07-17 18:41:34

有多种不同的方法可以检测编译、主机和运行时环境。 一切都取决于您想知道什么。 环境分为三大类:

  • 构建:这是编译程序的环境。
  • 主机:这是程序运行的环境。
  • 目标:对于代码生成工具(例如编译器),这是生成的代码将运行的位置。

如果您进行交叉编译,则构建环境和主机环境可能完全不同(这在构建嵌入式应用程序时很常见,但在构建桌面/服务器应用程序时并不常见),并且您通常无法运行在用于编译它的系统上编译的二进制文件。 否则,主机环境必须与构建环境兼容:例如,在 XP 上构建将在 Vista 上运行的应用程序。

C 预处理器宏不能用于告诉您主机系统的详细信息(即您正在运行的系统); 他们只能告诉您代码的编译目的。 在 Windows 中,两个最重要的宏是:

  • _WIN32 表示 Win32 API 可用。 它不会告诉您正在使用哪个编译器,事实上 _WIN32 在使用 Cygwin 的 GCC 和 MinGW 的 GCC 时都被定义。 因此,不要使用 _WIN32 来确定您是否正在使用 Visual Studio 进行编译。
  • _MSC_VER 告诉您该程序正在使用 Microsoft Visual C/C++ 进行编译。 嗯,差不多了。 _MSC_VER 在使用 Intel 的 C++ 编译器时定义,该编译器旨在成为 Visual C++ 的直接替代品。

Visual Studio 文档中描述了许多其他宏。

如果您想知道您正在使用的 Windows 的确切版本,则必须使用运行时函数,例如 GetVersion() (如其他答案中所述)。

如果您确切地告诉我们您想要检查的内容,您可能会得到更具体的答案。

There are a number of different ways to detect compilation, host, and runtime environments. All depending on exactly what you want to know. There are three broad types of environments:

  • Build: This is the environment in which the program is compiled.
  • Host: This is the environment in which the program is being run.
  • Target: In case of code-generation tools (such as compilers), this is where the generated code will run.

If you are cross-compiling, the build and host environment can be completely different (this is common when building embedded applications, but not very common when building desktop/server apps), and you typically cannot run the compiled binary on the system used to compile it. Otherwise, the host environment must be compatible with the build environment: for example, building an application on XP which will run on Vista.

C preprocessor macros can not be used to tell you the details of the host system (i.e. what you are running on); they can only tell you what the code was compiled for. In the windows case, the two most important macros are:

  • _WIN32 signifies that the Win32 API is available. It does not tell you which compiler you are using, in fact _WIN32 is defined both when using Cygwin's GCC and MinGW's GCC. So, do not use _WIN32 to figure out if you're being compiled with Visual Studio.
  • _MSC_VER tells you that you the the program is being compiled with Microsoft Visual C/C++. Well, almost. _MSC_VER is also defined when using Intel's C++ compiler which is intended to be a drop-in replacement for Visual C++.

There are a bunch of other macros described in the Visual Studio documentation.

If you want to know which exact version of Windows you are using, you'll have to use runtime functions such as GetVersion() (as described in other answers).

You might get more specific answers if you told us exactly what you want to check for.

薄情伤 2024-07-17 18:41:34
% touch foo.C ; g++ -dM -E foo.C

将很好地列出由您的[机器特定] g++ 编译器自动定义的所有宏(#define's)。

微软的编译器可能有类似的东西......

% touch foo.C ; g++ -dM -E foo.C

Will do a nice job of listing all the macros (#define's) automagically defined for you by your [machine specific] g++ compiler.

There might be something similar for Microsoft's compilers...

×眷恋的温暖 2024-07-17 18:41:34

这三行将帮助您进行检测,首先我们或大多数预定义的窗口提示一起放入一个 OS_WIN 宏定义中:

#if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
#define OS_WIN
#endif

然后您可以使用预处理器 ifdef 检查它:

#ifdef OS_WIN
//Windows specific stuff
#else
//Normal stuff
#endif

These three lines will help you with detection, first we or most of the predefined windows hints together into one OS_WIN macro definition:

#if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
#define OS_WIN
#endif

Then you can check for it using the preprocessor ifdef:

#ifdef OS_WIN
//Windows specific stuff
#else
//Normal stuff
#endif
木緿 2024-07-17 18:41:34

MSVC++ 编译器(不是 windows.h)为所有 版本定义了 _WIN32,因此它是一个更安全的宏来检查。 MinGW gcc 编译器也是如此。 任何针对 Win32 的交叉编译环境都应该设置此项。

The MSVC++ compiler (not windows.h) defines _WIN32 for all builds, so it is a safer macro to check. The MinGW gcc compiler does too. Any cross-compilation environment targeting Win32 should set this too.

三寸金莲 2024-07-17 18:41:34

如果您正在运行 MS Windows 目标代码,您可以调用 GetVersion() 或 GetVersionEx() 获取更多信息,并识别您正在运行的 Windows 变体。

有关详细信息,请参阅 MSDN 信息。

http://msdn.microsoft.com/en-us /library/ms724451(VS.85).aspx

If you're running MS Windows targeted code you can call GetVersion() or GetVersionEx() for more info, and to identify the variant of Windows you are running on.

For more info scope out the MSDN info.

http://msdn.microsoft.com/en-us/library/ms724451(VS.85).aspx

鸠魁 2024-07-17 18:41:34

这东西在 Visual Studio 2012 中工作,并且
其他 cgwin 的 g++ 编译器。
剪切并粘贴它
但这通常是最薄的
它所做的只是检测 Windows 操作系统。
只需应用量化即可:
如果没有赢
然后*inux
:D 享受

#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>
Using namespace std;


int main(){    
    char chCap;    int i = 0;
    const int MAX_BUFFER = 70; 
    char buffer[MAX_BUFFER];

    string cmd="ver";   
    FILE *stream = _popen(cmd.c_str(), "r");

    if (0 != stream){
        while (!feof(stream)){  
            //One way is here
            //chCap = fgetc(stream);
            //cout << chCap;
            //buffer[i++] = chCap;

            //This one seams better
            fgets(buffer, MAX_BUFFER, stream);      
        }
        _pclose(stream);
    }       
    cout << endl;
    cout << buffer << endl;

    //The op sys detection starts here
    string opSys(buffer);   //char array to string
    if("Microsoft" == opSys.substr(0,9)){
        cout << "You are in a Microsoft envornment " << endl;
    }
    system("pause");
    return 0;    
}

This thing works in visual studio 2012, and
other cgwin's g++ compiler.
Cut and paste it around
but this is generally as thin as it gets
All it does is detect windows op systems.
Just apply quantification:
If not win
Then *inux
:D enjoy

#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>
Using namespace std;


int main(){    
    char chCap;    int i = 0;
    const int MAX_BUFFER = 70; 
    char buffer[MAX_BUFFER];

    string cmd="ver";   
    FILE *stream = _popen(cmd.c_str(), "r");

    if (0 != stream){
        while (!feof(stream)){  
            //One way is here
            //chCap = fgetc(stream);
            //cout << chCap;
            //buffer[i++] = chCap;

            //This one seams better
            fgets(buffer, MAX_BUFFER, stream);      
        }
        _pclose(stream);
    }       
    cout << endl;
    cout << buffer << endl;

    //The op sys detection starts here
    string opSys(buffer);   //char array to string
    if("Microsoft" == opSys.substr(0,9)){
        cout << "You are in a Microsoft envornment " << endl;
    }
    system("pause");
    return 0;    
}
染柒℉ 2024-07-17 18:41:34

到目前为止提到的链接指示编译时的信息。 您可以在编译时在这些代码段中设置标志。

但是,我认为您问的更多是“我在哪个版本的 Windows 下运行?”而不是“我是否编译为在 Windows 下运行?” 我希望这是一个正确的假设。

在C#下,比较简单。 您可以参考 System.Environment.OSVersion,然后在“平台”下查看。

但是,您问的是 C++。 你使用什么编译器? 这对于如何检查操作系统的版本有很大的不同,因为没有单一的标准来获取此信息(我发现)。

在 Visual C++ 下,使用 Google 查找有关 GetVersion/GetVersionEx 的信息。 两者都会为您提供包含程序运行所在的当前 Windows 版本信息的结构。

The links mentioned so far indicate information at compile time. You can set flags within those code segments at compile time.

However, I think what you are asking is more along the lines of "Which version of windows am I running under?"not "Am I compiled to run under windows?" I hope that's a correct assumption.

Under C#, it is relatively simple. You can reference System.Environment.OSVersion then look under "Platform".

However, you are asking about C++. What compiler are you using? THis makes a big difference as to how you check for a version of the operating system, as there is no single standard for getting this information (that I've found).

Under Visual C++, use Google to find info on GetVersion/GetVersionEx. Both will give you structures that contain information on the current version of Windows that the program is running under.

指尖凝香 2024-07-17 18:41:34

事实上我会选择环境。 getenv() 是一个标准库函数,因此可能是执行此操作的唯一潜在可移植方法。 诀窍是找出哪些变量对于所有操作系统都是唯一的。 Windows 有 ComSpec、appdata、Windir、systemroot 和 systemdrive 需要设置。 您还可以 strstr WINDOWS 或 WINNT 的路径变量,但这可能不准确。 我不是 Linux shell/Solaris/MacOS 专家,因此其他人可能能够告诉您这些操作系统中存在哪些“标准”环境变量。 CygWin 也可能会给您带来一些问题。

Actually I would go with the Environment. getenv() is a standard library function and so is probably the only potentially portable way to do this. The trick is figuring out what variables are unique to all OSes. Windows has ComSpec, appdata, Windir, systemroot and systemdrive which should be set. You could also strstr the path variable for WINDOWS or WINNT but that might not be accurate. I'm not Linux shell/Solaris/MacOS expert so someone else might be able to tell you what "standard" environment variables exist in those OSes. CygWin may cause you some issues, too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文