我知道这并不简单,我已经研究了一段时间,现在我几乎可以肯定,没有完全安全的方法。但是就在这里。我正在寻找一种方法来知道我的应用程序在哪个操作系统上运行。到现在为止,我得到了:
#if defined(__WIN64__) || defined(__WIN64) || defined(WIN64)
#define NLF_OS_WINDOWS
#define NLF_OS_64BITS
#elif defined(__WIN32__) || defined(__WIN32) || defined(WIN32)
#define NLF_OS_WINDOWS
#define NLF_OS_32BITS
#elif defined(unix) || defined(__unix__) || defined(__unix)
#define NLF_OS_UNIX
#if defined(__APPLE__)
#define NLF_OS_APPLE
#define NLF_OS_BITS_UNIDENTIFIED
#include <TargetConditionals.h>
#if TARGET_OS_IPHONE && TARGET_IPHONE_SIMULATOR
#define NLF_OS_SIMULATOR
#elif TARGET_OS_IPHONE
#define NLF_OS_IPHONE
#else
#define NLF_OS_OSX
#endif
#elif defined(__linux__) || defined(__linux) || defined(linux)
#define NLF_OS_LINUX
#if defined(i386) || defined(__i386) || defined(__i386__)
#define NLF_OS_32BITS
#elif defined(amd64) || defined(__amd64) || defined(__amd64__)
#define NLF_OS_64BITS
#endif
#elif defined(__FreeBSD__) || defined(__FreeBSD) || defined(FreeBSD)
#define NLF_OS_FREEBSD
#if defined(i386) || defined(__i386) || defined(__i386__)
#define NLF_OS_32BITS
#elif defined(amd64) || defined(__amd64) || defined(__amd64__)
#define NLF_OS_64BITS
#endif
#endif
#else
#define NLF_OS_UNIDENTIFIED
#define NLF_OS_BITS_UNIDENTIFIED
#endif
它已经有所帮助,但是仅在编译应用程序时才告诉我什么系统。但是,由于我想交叉编译我正在做的事情,因此如果我可以向应用程序提供一种方法来知道哪个是当前正在运行的系统,那将非常有用。得到它?
可能是我可能使用sys /“ something”库(例如sys / types.h和sys / stat.h)来做一些难看的事情,但我几乎不了解这些东西。
PS .: C ++解决方案并不是我想要的,但是在这个高水平上,我很满意^^“
终于回答了这个问题。
答案是:您不需要。我注意到了。定义已经可以正常工作了。如果您要为另一个平台编译C代码,则交叉编译器将负责重新定义定义,因此,如果您使用预处理程序指令,则系统将始终知道该系统是什么。
但是,如果出于某种原因您确实需要它,则必须做一些丑陋的事情。就像使用系统一样(“ uname -a> my_file”);然后读取my_file内容,如果该命令不起作用,则尝试另一命令。
主要是,您不需要它=)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句