2012-07-05 109 views
18

相關問題
How can I detect the operating system in C/C++?如何確定操作系統是否爲C中的POSIX?

我如何才能找到的是什麼操作系統,我下GCC或ANSI C運行?

我會好的如果我可以知道我是否在POSIX上運行。


UPDATE:

它不會有所作爲,以我無論是在編譯時或運行時。我在調試程序中使用它,所以性能並不那麼重要。

我正在尋找路徑分隔符。 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 $ 
+1

更多,現有大約是在編譯的時候,此人似乎是約在運行時檢測檢測。抱歉錯位的近距離投票。重新公開發表。 – dmckee 2012-07-05 19:14:03

+0

重新提出了問題:) – 2012-07-05 19:18:18

+0

你想確定在運行時還是編譯時? – netcoder 2012-07-05 19:27:03

回答

22

統一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__和從開和一堆其他的東西。


另一個有用的文件,你必須看看是this Wiki

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


編輯:既然你真的要做到這一切,因爲你要決定使用哪個目錄分隔符,看this URL。它說:

注意文件I /在Windows API轉換O功能「/」到「\」作爲名稱轉換爲NT風格的名稱,使用 「時除外的一部分 \ ?\「前綴,詳見下節。

我不計劃在Windows,或知道任何事情,所以我不能說我已經存入銀行這一點。

+3

那麼你知道UNIX是(主要是)POSIX,但這並不意味着非UNIX不是POSIX。 – netcoder 2012-07-05 19:27:48

+7

'unistd.h'定義了'_POSIX_ *'宏。 UNIX是POSIX,所以確實有一個'unistd.h'。我的觀點是,你可以有一個不符合UNIX的POSIX兼容系統,其中'__unix__'不會被定義(真的是猜測)。總之,POSIX不是UNIX。 ;-) – netcoder 2012-07-05 19:32:44

+0

@netcoder - 我以相反的方式去了解它。我想確定'unistd.h'是否存在*,然後才能包含它並查看POSIX版本。最終,我能看到的唯一「安全」方式就是尋找'__unix__'。但你是對的(POSIX!= UNIX),我不會說它是防彈的。 – ArjunShankar 2012-07-05 19:35:50

0

這裏是我結束了:

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

}

2

有可能使用autoconf tool發現的unistd.h存在解決你的問題,然後會在產生config.h添加#define HAVE_UNISTD_H 1線如果找到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的發電機=史詩般的勝利

如果你的操作系統是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; 
} 
相關問題