如何使用Pragma打印定义的宏以获取OS版本?

发布于 2025-02-02 13:17:10 字数 974 浏览 3 评论 0 原文

遵循这些相关问题( 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版本的详细信息。

我想打印操作系统的版本,或打印使用预处理器定义的所有宏。

Following these related questions (1,2,3), I'm trying to get OS information from the preprocessor as follows:

#include <boost/preprocessor/stringize.hpp>
#ifdef __MACH__
#pragma message ("MACH: " BOOST_PP_STRINGIZE(__MACH__) )
#endif

clang 13.1.6 prints:

warning: MACH: 1 [-W#pragma-messages]
#pragma message ("MACH: " BOOST_PP_STRINGIZE(__MACH__) )

and gcc 11.3.0 :

note: '#pragma message: MACH: 1'
4 | #pragma message ("MACH: " BOOST_PP_STRINGIZE(__MACH__) )

This tells me that the macro __MACH__ is defined, but I don't get details about the OS version.

I want to print the version of the operating system or print all the macros that are defined using the preprocessor.

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

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

发布评论

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

评论(1

飘落散花 2025-02-09 13:17:10

我正在尝试从预处理器获取OS信息

即您的预处理器甚至在编译代码之前运行。

特定版本的操作系统执行在以后发生,不一定(甚至通常)在同一台计算机上。

因此,由于该宇宙受因果关系的束缚,您的预处理器无法知道您的软件的OS版本。

多亏了澄清的编辑,我知道您想弄清楚您当前使用的开发库是否包含特定功能。但这与检测您的操作系统无关!您可能会在旧操作系统上使用更多现代的标题,反之亦然。

您要寻找的通常称为A 功能测试宏

在您的具体情况下,您需要检查 应为posix.1兼容性( #if _posix_c_source&gt; = 199309l ,请参见OS X man page ),但我不知道OS X是否正确实现。

I'm trying to get OS information from the preprocessor

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.

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