如何确定操作系统是否为C中的POSIX?

时间:2012-07-05 19:04:38

标签: c gcc posix

  

相关问题
  How can I detect the operating system in C/C++?

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

我会好的如果我能知道我是否在POSIX上运行。


更新:

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

我正在寻找路径分隔符。 Windows& Unix / Linux / BSD会好的。

而且,我正在尝试在路径上找到基本名称。我找到了一些解决方案,但解决方案包括许多我不想要的包含。我要修改this solution


我在Mac OS X 10.4.11上,在this URL之后,我执行并得到以下输出:

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 $

4 个答案:

答案 0 :(得分:28)

单一UNIX规范要求存在unistd.h,它可以告诉您POSIX版本(通过_POSIX_VERSION宏)。

但如果您还不知道自己实际上在UNIX上进行编译,那么如何包含unistd.h

这就是this GCC document派上用场的地方。根据它,测试__unix__的存在或评估应该告诉您系统是UNIX。所以:

#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__未在Mac OS X上定义,因此要考虑到这一点,您可以改为:

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

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

cpp -dM /dev/null

例如,我的GNU / Linux系统还另外定义了除__linux__之外的__gnu_linux____unix__以及其他一些东西。


您必须查看的另一个有用文档是this Wiki

goes on to present a way of detecting the presence and version of POSIX的方式类似于我上面描述的方式。


编辑:由于您想要决定使用哪个目录分隔符,因此您真的想要完成所有这些操作,请查看this URL。它说:

  

注意 Windows API中的文件I / O函数将“/”转换为“\”作为一部分   将名称转换为NT样式的名称,除非使用   “\?\”前缀详见以下部分。

我不在Windows上编程,或者知道很多有关它的任何内容,所以我不能说我已经对此进行了解释。

答案 1 :(得分:4)

使用autoconf tool来发现unistd.h的存在可以解决您的问题,如果#define HAVE_UNISTD_H 1,则会在生成的config.h中添加unistd.h行找到了,但我发现autoconf有点难以使用,并且已经过时了。

如果你使用 cmake ,你可以用同样的方法解决它。您可以创建一个包含以下内容的config.h.in

#ifndef CONFIG_H
#define CONFIG_H

#cmakedefine HAVE_UNISTD_H 1

#endif

您的项目CMakeLists.txt将如下所示:

project(MyApp)

include(CheckIncludeFiles)
check_include_file("unistd.h" HAVE_UNISTD_H)

configure_file(config.h.in config.h @ONLY)

add_executable(${PROJECT_NAME} main.c)

然后从命令行生成:

cmake . -G"Unix Makefiles"

或生成Xcode项目(仅限OSX):

cmake . -G"Xcode"

或生成visual studio 2013解决方案项目(仅限Windows):

cmake . -G"Visual Studio 12 2013 Win64"

cmake generators =史诗般的胜利

如果您的操作系统是POSIX,那么您生成的config.h应如下所示:

#ifndef CONFIG_H
#define CONFIG_H

#define HAVE_UNISTD_H 1

#endif

否则,它看起来像那样:

#ifndef CONFIG_H
#define CONFIG_H

/* #undef HAVE_UNISTD_H */

#endif

然后您可以自由使用您信任的config.h

#include "config.h"

#if HAVE_UNISTD_H
#   include <unistd.h>
#endif

int main (int argc, const char * argv[])
{
#if defined(_POSIX_VERSION)
     /* POSIX code here */
#else
     /* non-POSIX code here */
#endif
    return 0;
}

答案 2 :(得分:1)

以下是我最终的结果:

#include <stdio.h>
/*
NAME
    basename -- return pointer to last component of a pathname

Distribution: This function is in the public domain. 

Origin of function:
http://www.koders.com/c/fidEB79B7607A210C3BB7B813E793465F9D469AE912.aspx


SYNOPSIS
    char *basename (const char *name)

DESCRIPTION
    Given a pointer to a string containing a typical pathname
    (/usr/src/cmd/ls/ls.c for example), returns a pointer to the
    last component of the pathname ("ls.c" in this case).

Restrictions:
    Presumes a UNIX or DOS/Windows style path with UNIX or DOS/Windows 
    style separators.
*/

/*
NAME:
    basename: 

Function:
    return pointer to last component of a pathname

Distribution: 
    This function is in the public domain. 

Origin of function:
    http://www.koders.com/c/fidEB79B7607A210C3BB7B813E793465F9D469AE912.aspx


SYNOPSIS:
    char *basename (const char *name)

DESCRIPTION:
    Given a pointer to a string containing a typical pathname
    (/usr/src/cmd/ls/ls.c for example), returns a pointer to the
    last component of the pathname ("ls.c" in this case).

Restrictions:
    Presumes a UNIX or DOS/Windows style path with UNIX or
    DOS/Windows style separators.
    Windows volumes are only a-zA-Z.
    The original code suggests ISALPHA.

*/

char * basename (const char *name)
{
const char *base;
// predefined OS symbols
// http://sourceforge.net/apps/mediawiki/predef/index.php?title=Operating_Systems#UNIX_Environment

#ifndef DIR_SEPARATOR
#  define DIR_SEPARATOR '/'
#endif

#ifndef DIR_SEPARATOR_2 
#  define DIR_SEPARATOR_2 '\\'
#endif

// Check if we are running Unix like os
//   else assume Windows.  Note if we guess wrong, it's not
//   so bad because Windows includes the Unix separator.
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
#  define IS_DIR_SEPARATOR(ch) ((ch) == DIR_SEPARATOR)
#else
#  define IS_DIR_SEPARATOR(ch) \
(((ch) == DIR_SEPARATOR) || ((ch) == DIR_SEPARATOR_2))
/* Skip over the disk name in MSDOS pathnames. */
if (isalpha(name[0]) && name[1] == ':') 
    name += 2;
#endif


for (base = name; *name; name++)
    {
    if (IS_DIR_SEPARATOR (*name))
        {
        base = name + 1;
        }
    }
return (char *) base;
}




int main (int argc, const char * argv[]) {
    /* Return the basename of a pathname. */


#define S1 "/usr/src/cmd/ls/ls.c"
#define S2 "/usr/src/cmd/ls/abc"
#define S3 "a:/usr/src/cmd/ls/def"
#define S4 "ghi"
#define S5 "jkl.txt"
#define S6 "d:\\usr\\src\\cmd\\mno.txt"
#define S7 "d:pqm.txt"

printf(S1 "  \t is %s\n",basename(S1));
printf(S2 "  \t is %s\n",basename(S2));
printf(S3 "  \t is %s\n",basename(S3));
printf(S4 "  \t is %s\n",basename(S4));
printf(S5 "  \t is %s\n",basename(S5));
printf(S6 "  \t is %s\n",basename(S6));
printf(S7 "  \t is %s\n",basename(S7));
    return 0;

}

答案 3 :(得分:0)

我知道这是一个死问题,但我必须添加这个。

在 C++17 中,现在有一个 TypeError: null is not an object (evaluating 'AgoraRtcChannelModule.prefix') at node_modules\react-native\Libraries\LogBox\LogBox.js:148:8 in registerError at node_modules\react-native\Libraries\LogBox\LogBox.js:59:8 in errorImpl at node_modules\react-native\Libraries\LogBox\LogBox.js:33:4 in console.error at node_modules\expo\build\environment\react-native-logs.fx.js:27:4 in error at node_modules\react-native\Libraries\Core\ExceptionsManager.js:104:6 in reportException at node_modules\react-native\Libraries\Core\ExceptionsManager.js:171:19 in handleException at node_modules\react-native\Libraries\Core\setUpErrorHandling.js:24:6 in handleError at node_modules\expo-error-recovery\build\ErrorRecovery.fx.js:12:21 in ErrorUtils.setGlobalHandler$argument_0 at node_modules\@babel\runtime\node_modules\regenerator-runtime\runtime.js:62:36 in tryCatch at node_modules\@babel\runtime\node_modules\regenerator-runtime\runtime.js:288:29 in invoke at node_modules\@babel\runtime\node_modules\regenerator-runtime\runtime.js:62:36 in tryCatch at node_modules\@babel\runtime\node_modules\regenerator-runtime\runtime.js:152:27 in invoke at node_modules\@babel\runtime\node_modules\regenerator-runtime\runtime.js:162:18 in Promise.resolve.then$argument_0 at node_modules\react-native\node_modules\promise\setimmediate\core.js:37:13 in tryCallOne at node_modules\react-native\node_modules\promise\setimmediate\core.js:123:24 in setImmediate$argument_0 at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:130:14 in _callTimer at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:181:14 in _callImmediatesPass at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:441:30 in callImmediates at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:387:6 in __callImmediates at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:135:6 in __guard$argument_0 at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:364:10 in __guard at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:134:4 in flushedQueue at [native code]:null in flushedQueue at [native code]:null in invokeCallbackAndReturnFlushedQueue Invariant Violation: "main" has not been registered. This can happen if: * Metro (the local dev server) is run from the wrong folder. Check if Metro is running, stop it and restart it in the current project. * A module failed to load due to an error and `AppRegistry.registerComponent` wasn't called. at node_modules\react-native\Libraries\LogBox\LogBox.js:148:8 in registerError at node_modules\react-native\Libraries\LogBox\LogBox.js:59:8 in errorImpl at node_modules\react-native\Libraries\LogBox\LogBox.js:33:4 in console.error at node_modules\expo\build\environment\react-native-logs.fx.js:27:4 in error at node_modules\react-native\Libraries\Core\ExceptionsManager.js:104:6 in reportException at node_modules\react-native\Libraries\Core\ExceptionsManager.js:171:19 in handleException at node_modules\react-native\Libraries\Core\setUpErrorHandling.js:24:6 in handleError at node_modules\expo-error-recovery\build\ErrorRecovery.fx.js:12:21 in ErrorUtils.setGlobalHandler$argument_0 at node_modules\@babel\runtime\node_modules\regenerator-runtime\runtime.js:62:36 in tryCatch at node_modules\@babel\runtime\node_modules\regenerator-runtime\runtime.js:288:29 in invoke at node_modules\@babel\runtime\node_modules\regenerator-runtime\runtime.js:62:36 in tryCatch at node_modules\@babel\runtime\node_modules\regenerator-runtime\runtime.js:152:27 in invoke at node_modules\@babel\runtime\node_modules\regenerator-runtime\runtime.js:162:18 in Promise.resolve.then$argument_0 at node_modules\react-native\node_modules\promise\setimmediate\core.js:37:13 in tryCallOne at node_modules\react-native\node_modules\promise\setimmediate\core.js:123:24 in setImmediate$argument_0 at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:130:14 in _callTimer at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:181:14 in _callImmediatesPass at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:441:30 in callImmediates at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:387:6 in __callImmediates at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:135:6 in __guard$argument_0 at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:364:10 in __guard at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:134:4 in flushedQueue at [native code]:null in flushedQueue at [native code]:null in invokeCallbackAndReturnFlushedQueue 宏可用于 __has_include 指令。这意味着您可以像这样检查 posix 合规性:

#if

干杯:D!