bestsource

C에서 운영체제가 POSIX인지 확인하려면 어떻게 해야 합니까?

bestsource 2023. 9. 21. 20:41
반응형

C에서 운영체제가 POSIX인지 확인하려면 어떻게 해야 합니까?

관련질문
C/C++에서 운영 체제를 감지하려면 어떻게 해야 합니까?

GCC 또는 ANSIC에서 실행 중인 운영 체제를 어떻게 알 수 있습니까?

포식스에서 작동하는지 알 수 있다면 괜찮을 겁니다


업데이트:

컴파일 타임이든 실행 타임이든 저에게는 아무런 차이가 없습니다.이것을 디버그 루틴에 사용하기 때문에 성능은 그렇게 중요하지 않습니다.

경로 구분자를 찾고 있습니다.윈도우 & 유닉스/리눅스/BSD면 됩니다.

그리고, 길에서 기본 이름을 찾으려고 합니다.저는 몇 가지 해결책을 찾았지만, 해결책에는 제가 원하지 않는 많은 것들이 포함되어 있습니다.는 이 해결책을 수정할 것입니다.


저는 Mac OS X 10.4.11을 사용하고 있으며, 이 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 $

Single UNIX Specification은 POSIX 버전을 알려줄 수 있는 의 존재를 요구합니다._POSIX_VERSION매크로).

그러나 어떻게 당신이 포함할 수 있습니까?unistd.hUNIX에서 컴파일하고 있다는 사실을 아직 모르신다면요?

그것이 바로GCC 문서가 도움이 되는 부분입니다.그에 따르면, 그 존재에 대한 테스트, 또는 평가 대 진실.__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__그리고 여러 가지 다른 것들이 있습니다.


여러분이 꼭 봐야 할 또 다른 유용한 문서는 이 위키입니다.

에서 설명한 것과 유사한 방식으로 POSIX의 존재와 버전을 검출하는 방법을 제시합니다.


편집: 어떤 디렉토리 구분자를 사용할지 결정하고 싶어서 이 모든 것을 정말로 수행하고 싶으니 이 URL을 참조하십시오.다음과 같이 적혀있습니다.

참고 Windows API에서 "/"를 "\"로 변환하는 파일 I/O 기능은 "\"을 사용하는 경우를 제외하고 이름을 NT 스타일 이름으로 변환하는 일부입니다.\" 접두사는 다음 절에 자세히 나와 있습니다.

저는 윈도우 프로그램을 사용하지도 않고, 윈도우에 대해 잘 알지도 못하기 때문에 이를 믿고 있다고 말할 수는 없습니다.

자동 컨프 도구를 사용하여 문제를 해결하여 다음을 발견할 수 있습니다.unistd.h그 다음에 한 개를 추가할 겁니다.#define HAVE_UNISTD_H 1에 줄을 긋다config.h한다면unistd.h발견됐지만, 오토콘프는 사용하기가 좀 어렵고, 매우 구식이라는 것을 발견했습니다.

혹시 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 = epic win

된 가 POSIXconfig.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;
}

이것이 일종의 사문화된 질문이라는 것을 알고 있지만, 이것을 추가할 수 밖에 없었습니다.

C++17(및 C를 GCC클랑 확장으로 사용)에는 이제__has_include할 수 있는 #if훈령 이는 할 수 합니다.이는 다음과 같이 posix 준수 여부를 검사할 수 있음을 의미합니다.

#if __has_include(<unistd.h>)
    // System is posix-compliant
#else
    // System is not posix-compliant
#endif

제가 생각한 결과는 다음과 같습니다.

#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;

}

@ArjunShankar에서 영감을 받아 unistd.h와 test macro_POSIX_VERSION을 포함합니다.

#include <unistd.h>
#include <stdio.h>

int main()
{
    #ifdef _POSIX_VERSION
    printf("_POSIX_VERSION:%ld\n", _POSIX_VERSION);
    #else
    printf("Not support posix.\n");
    #endif
    return 0;
}

내 Ubuntu에서는 다음을 출력합니다.

_POSIX_VERSION:200809

언급URL : https://stackoverflow.com/questions/11350878/how-can-i-determine-if-the-operating-system-is-posix-in-c

반응형