平台 C 预处理器定义
我正在用 C++ 编写一个小型库,我需要能够在多种不同的平台上构建它,包括 iPhone、Windows、Linux、Mac 和 Symbian S60。 我已经编写了大部分代码,使其与平台无关,但有些部分必须根据每个平台编写。
目前,我通过根据当前平台包含不同的标头来实现此目的,但我在充实这一点时遇到了麻烦,因为我不确定为所有平台定义了哪些预处理器定义。 对于 Windows,我通常可以依赖看到 WIN32 或 _WIN32。 对于 Linux,我可以信赖看到 _UNIX_,但我不太确定其他平台或其 64 位变体。 有谁有平台上找到的不同定义的列表,还是我必须求助于配置文件或 gcc 参数?
I'm writing a small library in C++ that I need to be able to build on quite a few different platforms, including iPhone, Windows, Linux, Mac and Symbian S60. I've written most of the code so that it is platform-agnostic but there are some portions that must be written on a per-platform basis.
Currently I accomplish this by including a different header depending on the current platform but I'm having trouble fleshing this out because I'm not sure what preprocessor definitions are defined for all platforms. For windows I can generally rely on seeing WIN32 or _WIN32. For Linux I can rely on seeing _UNIX_ but I am less certain about the other platforms or their 64-bit variants. Does anyone have a list of the different definitions found on platforms or will I have to resort to a config file or gcc parameter?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我的书签中有这个sourceforge预编译器页面。
I have this sourceforge pre-compiler page in my bookmarks.
这些定义完全取决于您的编译器供应商。 如果您在所有平台上使用相同的编译器(例如 gcc),那么您会更轻松一些。
您可能还想尝试组织您的项目,以便大多数 .h 文件不依赖于平台。 将您的实现(cpp 文件)拆分为单独的文件; 一个用于非特定内容,一个用于每个平台。 特定于平台的标头可以包含仅对该平台有意义的“私有”标头。 您可能必须制作适配器函数才能使类似的功能 100% 工作(当系统库采用略有不同的参数时),但我发现它最终确实很有帮助,并且引入新平台要容易得多将来。
The definitions are going to be purely up to your compiler vendor. If you are using the same compiler (say, gcc) on all your platforms then you will have a little bit easier time of it.
You might also want to try to instead organize your project such that most of the .h files are not platform dependent. Split your implementation (cpp files) into separate files; one for the nonspecific stuff and one for each platform. The platform specific ones can include 'private' headers that only make sense for that platform. You may have to make adapter functions to get something like this to work 100% (when the system libs take slightly differed arguments) but I have found it to be really helpful in the end, and bringing on a new platform is a whole lot easier in the future.
C 和 C++ 标准都没有定义此类符号,因此您将受到特定 C 或 C++ 实现的支配。 拥有一份常用符号列表会很有用,但不幸的是我没有。
Neither the C nor the C++ standards define such symbols, so you are going to be at the mercy of specific C or C++ implementations. A list of commonly used symbols would be a useful thing to have, but unfortunately I haven't got one.
我认为不存在通用的平台定义列表,因为我见过的每个跨平台库都有一个充满这些内容的临时 config.h。 但是您可以考虑查看相当可移植的库(例如 libpng、zlib 等)使用的库。
这是 libpng
I don't think there exists a universal list of platform defines judging by the fact that every cross-platform library I have seen has an ad-hoc config.h full of these stuff. But you can consider looking at the ones used by fairly portable libraries like libpng, zlib etc.
Here's the one used by libpng
如果您想查看具有 GCC 的给定系统(例如 Mac OS X、iOS、Linux)的默认预处理器符号,您可以从命令行获取完整列表:但是,
这些符号的用途通常有限,在预处理器运行的编译阶段,大多数符号仅标识托管编译器的系统的操作系统和CPU类型,而不是目标系统(例如,当交叉时) -针对 iOS 进行编译)。 在 Mac OS X 和 iOS 上,确定目标系统的编译时特征的正确方法是
这将从当前使用的平台和 SDK 中获取 TargetConditionals.h,然后您可以确定(例如)字节序和一些一些宏的其他特征。 (查看 TargetConditionals.h 以了解您可以收集哪些类型的信息。)
If you want to look through the default preprocessor symbols for a given system on which you have GCC (e.g. Mac OS X, iOS, Linux), you can get a complete list from the command-line thus:
These are often of limited use however, as at the stage of the compilation at which the preprocessor operates, most of the symbols identify the operating system and CPU type of only the system hosting the compiler, rather than the system being targeted (e.g. when cross-compiling for iOS). On Mac OS X and iOS, the right way to determine the compile-time characteristics of the system being targeted is
This will pick up TargetConditionals.h from the Platform and SDK currently in use, and then you can determine (e.g.) endianness and some other characteristics from some of the Macros. (Look through TargetConditionals.h to see what kinds of info you can glean.)