如何确定操作系统是否为C中的POSIX? [英] How can I determine if the operating system is POSIX in C?

查看:63
本文介绍了如何确定操作系统是否为C中的POSIX?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相关问题
如何在C/C ++中检测操作系统?

如何找出我在GCC或ANSI C中运行的操作系统?

如果我能确定自己是否在POSIX上运行,我会没事的.


更新:

无论是在编译时还是在运行时,这对我都没有影响.我在调试例程中使用它,因此性能并不是那么重要.

我正在寻找路径分隔符.Windows&Unix/Linux/BSD就可以了.

而且,我正在尝试在路径上找到基本名称.我找到了一些解决方案,但是这些解决方案包含很多我不想要的包含项.我将修改此解决方案.


我使用的是Mac OS X 10.4.11,并且遵循此URL ,我执行并得到以下输出:

  mac $ touch myfile.cmac $ gcc -std = c99 -E -dM myfile.c#定义__DBL_MIN_EXP__(-1021)#定义__FLT_MIN__ 1.17549435e-38F#定义__CHAR_BIT__ 8#定义__WCHAR_MAX__ 2147483647#define __DBL_DENORM_MIN__ 4.9406564584124654e-324#定义__FLT_EVAL_METHOD__ 0#定义__DBL_MIN_10_EXP__(-307)#定义__FINITE_MATH_ONLY__ 0#定义__GNUC_PATCHLEVEL__ 1#定义__SHRT_MAX__ 32767#define __LDBL_MAX__ 1.79769313486231580793728971405301e + 308L#定义__APPLE_CC__ 5367#define __UINTMAX_TYPE__ long long unsigned int#定义__LDBL_MAX_EXP__ 1024#定义__SCHAR_MAX__ 127#定义__USER_LABEL_PREFIX__ _#定义__STDC_HOSTED__ 1#定义__LDBL_HAS_INFINITY__ 1#定义__DBL_DIG__ 15#定义__FLT_EPSILON__ 1.19209290e-7F#定义__LDBL_MIN__ 2.00416836000897277799610805135016e-292L#定义__ppc__ 1#define __strong#定义__苹果__ 1#定义__DECIMAL_DIG__ 33#定义__LDBL_HAS_QUIET_NAN__ 1#定义__动态__ 1#定义__GNUC__ 4#define __DBL_MAX__ 1.7976931348623157e + 308#定义__DBL_HAS_INFINITY__ 1#定义__STRICT_ANSI__ 1#define __weak#定义__DBL_MAX_EXP__ 1024#define __LONG_LONG_MAX__ 9223372036854775807LL#定义__GXX_ABI_VERSION 1002#定义__FLT_MIN_EXP__(-125)#define __DBL_MIN__ 2.2250738585072014e-308#定义__DBL_HAS_QUIET_NAN__ 1#定义__REGISTER_PREFIX__#定义__NO_INLINE__ 1#定义_ARCH_PPC 1#定义__FLT_MANT_DIG__ 24#define __VERSION__"4.0.1(Apple Computer,Inc. build 5367)"#定义__BIG_ENDIAN__ 1#define __SIZE_TYPE__ long unsigned int#定义__FLT_RADIX__ 2#define __LDBL_EPSILON__ 4.94065645841246544176568792868221e-324L#定义__NATURAL_ALIGNMENT__ 1#定义__FLT_HAS_QUIET_NAN__ 1#定义__FLT_MAX_10_EXP__ 38#定义__LONG_MAX__ 2147483647L#定义__FLT_HAS_INFINITY__ 1#定义__STDC_VERSION__ 199901L#定义_BIG_ENDIAN 1#定义__LDBL_MANT_DIG__ 106#定义__WCHAR_TYPE__ int#定义__FLT_DIG__ 6#define __INT_MAX__ 2147483647#定义__LONG_DOUBLE_128__ 1#定义__FLT_MAX_EXP__ 128#定义__DBL_MANT_DIG__ 53#定义__WINT_TYPE__ int#定义__LDBL_MIN_EXP__(-968)#定义__MACH__ 1#定义__LDBL_MAX_10_EXP__ 308#定义__DBL_EPSILON__ 2.2204460492503131e-16#define __INTMAX_MAX__ 9223372036854775807LL#定义__FLT_DENORM_MIN__ 1.40129846e-45F#定义__PIC__ 1#定义__FLT_MAX__ 3.40282347e + 38F#定义__FLT_MIN_10_EXP__(-37)#define __INTMAX_TYPE__ long long int#定义__GNUC_MINOR__ 0#定义__DBL_MAX_10_EXP__ 308#define __LDBL_DENORM_MIN__ 4.94065645841246544176568792868221e-324L#定义__PTRDIFF_TYPE__ int#定义__LDBL_MIN_10_EXP__(-291)#定义__LDBL_DIG__ 31#定义__POWERPC__ 1mac $ 

解决方案

单一UNIX规范要求存在此GCC文档派上用场的地方.据此,测试 __ unix __ 的存在或真实评估应该告诉您该系统是UNIX.所以:

  #ifdef __unix__/*是的,因为定义了__unix__,所以它是UNIX.*/#include< unistd.h>/*您可以使用_POSIX_VERSION查找版本..... */#万一 

在Mac OS X上未定义

__ unix __ ,因此,为了解决这个问题,您可以执行以下操作:

  #if已定义(__unix__)||(已定义(__APPLE__)和已定义(__MACH__)) 

要获取系统上系统特定的预定义宏的列表,可以执行:

  cpp -dM/dev/null 

例如,除了 __ unix __ 和许多其他内容之外,我的GNU/Linux系统还另外定义了 __ linux __ __ gnu_linux __ .


另一个必须查看的有用文档是此Wiki ./p>

继续提出一种检测POSIX存在和版本的方法与上述方法类似.


编辑:由于您确实想做所有这些事情,因为您想决定使用哪个目录分隔符,因此请查看

我没有在Windows上编程,或者对此不甚了解,所以我不能说我有赖于此.

Related questions
How can I detect the operating system in C/C++?

How can I find out what operating system I am running under in GCC or in ANSI C?

I'd be fine If I can know if I'm running on POSIX.


UPDATE:

It doesn't make a difference to me whether it is at compile time or run time. I'm using this in a debug routine, so performance isn't that important.

I'm looking for the path separator. Windows & Unix/Linux/BSD would be fine.

And, I'm trying to find the basename on a path. I found some solutions, but the solutions include a lot of includes which I do not want. I'm going to mod up this solution.


I'm on Mac OS X 10.4.11, and following this URL, I executed and got the following output:

mac $ touch myfile.c
mac $ gcc -std=c99 -E -dM myfile.c
#define __DBL_MIN_EXP__ (-1021)
#define __FLT_MIN__ 1.17549435e-38F
#define __CHAR_BIT__ 8
#define __WCHAR_MAX__ 2147483647
#define __DBL_DENORM_MIN__ 4.9406564584124654e-324
#define __FLT_EVAL_METHOD__ 0
#define __DBL_MIN_10_EXP__ (-307)
#define __FINITE_MATH_ONLY__ 0
#define __GNUC_PATCHLEVEL__ 1
#define __SHRT_MAX__ 32767
#define __LDBL_MAX__ 1.79769313486231580793728971405301e+308L
#define __APPLE_CC__ 5367
#define __UINTMAX_TYPE__ long long unsigned int
#define __LDBL_MAX_EXP__ 1024
#define __SCHAR_MAX__ 127
#define __USER_LABEL_PREFIX__ _
#define __STDC_HOSTED__ 1
#define __LDBL_HAS_INFINITY__ 1
#define __DBL_DIG__ 15
#define __FLT_EPSILON__ 1.19209290e-7F
#define __LDBL_MIN__ 2.00416836000897277799610805135016e-292L
#define __ppc__ 1
#define __strong 
#define __APPLE__ 1
#define __DECIMAL_DIG__ 33
#define __LDBL_HAS_QUIET_NAN__ 1
#define __DYNAMIC__ 1
#define __GNUC__ 4
#define __DBL_MAX__ 1.7976931348623157e+308
#define __DBL_HAS_INFINITY__ 1
#define __STRICT_ANSI__ 1
#define __weak 
#define __DBL_MAX_EXP__ 1024
#define __LONG_LONG_MAX__ 9223372036854775807LL
#define __GXX_ABI_VERSION 1002
#define __FLT_MIN_EXP__ (-125)
#define __DBL_MIN__ 2.2250738585072014e-308
#define __DBL_HAS_QUIET_NAN__ 1
#define __REGISTER_PREFIX__ 
#define __NO_INLINE__ 1
#define _ARCH_PPC 1
#define __FLT_MANT_DIG__ 24
#define __VERSION__ "4.0.1 (Apple Computer, Inc. build 5367)"
#define __BIG_ENDIAN__ 1
#define __SIZE_TYPE__ long unsigned int
#define __FLT_RADIX__ 2
#define __LDBL_EPSILON__ 4.94065645841246544176568792868221e-324L
#define __NATURAL_ALIGNMENT__ 1
#define __FLT_HAS_QUIET_NAN__ 1
#define __FLT_MAX_10_EXP__ 38
#define __LONG_MAX__ 2147483647L
#define __FLT_HAS_INFINITY__ 1
#define __STDC_VERSION__ 199901L
#define _BIG_ENDIAN 1
#define __LDBL_MANT_DIG__ 106
#define __WCHAR_TYPE__ int
#define __FLT_DIG__ 6
#define __INT_MAX__ 2147483647
#define __LONG_DOUBLE_128__ 1
#define __FLT_MAX_EXP__ 128
#define __DBL_MANT_DIG__ 53
#define __WINT_TYPE__ int
#define __LDBL_MIN_EXP__ (-968)
#define __MACH__ 1
#define __LDBL_MAX_10_EXP__ 308
#define __DBL_EPSILON__ 2.2204460492503131e-16
#define __INTMAX_MAX__ 9223372036854775807LL
#define __FLT_DENORM_MIN__ 1.40129846e-45F
#define __PIC__ 1
#define __FLT_MAX__ 3.40282347e+38F
#define __FLT_MIN_10_EXP__ (-37)
#define __INTMAX_TYPE__ long long int
#define __GNUC_MINOR__ 0
#define __DBL_MAX_10_EXP__ 308
#define __LDBL_DENORM_MIN__ 4.94065645841246544176568792868221e-324L
#define __PTRDIFF_TYPE__ int
#define __LDBL_MIN_10_EXP__ (-291)
#define __LDBL_DIG__ 31
#define __POWERPC__ 1
mac $

解决方案

The Single UNIX Specification requires the existence of unistd.h, which can tell you the POSIX version (via the _POSIX_VERSION macro).

But how can you include unistd.h if you don't know yet that you are in fact compiling on a UNIX?

That is where this GCC document comes handy. According to it, testing for the presence, or evaluation-to-true of __unix__ should tell you that the system is a UNIX. So:

#ifdef __unix__
/* Yes it is a UNIX because __unix__ is defined.  */

#include <unistd.h>

/* You can find out the version with _POSIX_VERSION.
..
..  */

#endif

__unix__ is not defined on Mac OS X, so to account for that, you could instead do:

#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))

To get a list of system specific predefined macros on your system, you may execute:

cpp -dM /dev/null

For example, my GNU/Linux system also additionally defines __linux__ and __gnu_linux__ apart from __unix__ and a bunch of other stuff.


Another useful document that you must look at is this Wiki.

It goes on to present a way of detecting the presence and version of POSIX in a way similar to the one I described above.


EDIT: Since you really want to do all this because you want to decide which directory separator to use, look at this URL. It says:

Note File I/O functions in the Windows API convert "/" to "\" as part of converting the name to an NT-style name, except when using the "\?\" prefix as detailed in the following sections.

I don't program on Windows, or know much anything about it, so I can't say I've banked on this.

这篇关于如何确定操作系统是否为C中的POSIX?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆