是否有任何宏可以确定我的代码是否被编译到 Windows?
我想检测我正在编译的操作系统是否是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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
[编辑:我假设您想使用编译时宏来确定您所在的环境。 也许您想确定您是否在 Linux 下的 Wine 上运行或在 Windows 下运行,但一般来说,您的编译器针对特定环境,要么是 Windows (DOS),要么不是,但很少(从不) ?) 两者。]
一些编译器提供宏来指示 Windows 构建环境。 但这些会因编译器的不同而有所不同,甚至在 Windows 上的同一编译器上(如果目标环境不完全是 Windows)也会有所不同。 通常是
__WIN32__
,但并非总是如此。有时它可以是
_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.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
有多种不同的方法可以检测编译、主机和运行时环境。 一切都取决于您想知道什么。 环境分为三大类:
如果您进行交叉编译,则构建环境和主机环境可能完全不同(这在构建嵌入式应用程序时很常见,但在构建桌面/服务器应用程序时并不常见),并且您通常无法运行在用于编译它的系统上编译的二进制文件。 否则,主机环境必须与构建环境兼容:例如,在 XP 上构建将在 Vista 上运行的应用程序。
C 预处理器宏不能用于告诉您主机系统的详细信息(即您正在运行的系统); 他们只能告诉您代码的编译目的。 在 Windows 中,两个最重要的宏是:
_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:
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:
_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.
将很好地列出由您的[机器特定] g++ 编译器自动定义的所有宏(#define's)。
微软的编译器可能有类似的东西......
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...
这三行将帮助您进行检测,首先我们或大多数预定义的窗口提示一起放入一个 OS_WIN 宏定义中:
然后您可以使用预处理器 ifdef 检查它:
These three lines will help you with detection, first we or most of the predefined windows hints together into one OS_WIN macro definition:
Then you can check for it using the preprocessor ifdef:
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.如果您正在运行 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
这东西在 Visual Studio 2012 中工作,并且
其他 cgwin 的 g++ 编译器。
剪切并粘贴它
但这通常是最薄的
它所做的只是检测 Windows 操作系统。
只需应用量化即可:
如果没有赢
然后*inux
:D 享受
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
到目前为止提到的链接指示编译时的信息。 您可以在编译时在这些代码段中设置标志。
但是,我认为您问的更多是“我在哪个版本的 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.
事实上我会选择环境。 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.