如何使用Pragma打印定义的宏以获取OS版本?
遵循这些相关问题( 1 , 2 , 3 ),我正在尝试从预处理器中获取OS信息,如下:
#include <boost/preprocessor/stringize.hpp>
#ifdef __MACH__
#pragma message ("MACH: " BOOST_PP_STRINGIZE(__MACH__) )
#endif
Clang 13.1 .6打印:
warning: MACH: 1 [-W#pragma-messages]
#pragma message ("MACH: " BOOST_PP_STRINGIZE(__MACH__) )
和GCC 11.3.0:
note: '#pragma message: MACH: 1'
4 | #pragma message ("MACH: " BOOST_PP_STRINGIZE(__MACH__) )
这告诉我宏 __马赫__
是定义的,但我没有获得有关OS版本的详细信息。
我想打印操作系统的版本,或打印使用预处理器定义的所有宏。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
即您的预处理器甚至在编译代码之前运行。
特定版本的操作系统执行在以后发生,不一定(甚至通常)在同一台计算机上。
因此,由于该宇宙受因果关系的束缚,您的预处理器无法知道您的软件的OS版本。
多亏了澄清的编辑,我知道您想弄清楚您当前使用的开发库是否包含特定功能。但这与检测您的操作系统无关!您可能会在旧操作系统上使用更多现代的标题,反之亦然。
您要寻找的通常称为A 功能测试宏。
在您的具体情况下,您需要检查 应为posix.1兼容性(
#if _posix_c_source&gt; = 199309l
,请参见OS X man page ),但我不知道OS X是否正确实现。Your preprocessor runs before your code is even compiled.
The execution on an OS of a specific version happens at a later time, not necessarily (or even usually) on the same machine.
Hence, for reasons of this universe being bound by causality, your preprocessor can't know the version of the OS your software is run on.
Thanks to the clarifying edit, I understand you want to figure out whether the development libraries that you're currently using contain a specific function. But that has nothing to do with detecting your OS! You could be using more modern headers on an old OS, and vice versa.
What you're looking for is usually called a feature test macro.
In your specific case, the feauture you need to check for should be POSIX.1 compatibility (
#if _POSIX_C_SOURCE >= 199309L
, see the bottom of the OS X man page), but I don't know whether OS X implemented that correctly.