2011-09-06 48 views
2

我敢肯定有這樣的問題,但我不能找到它:\總之,這裏的問題:差/等待

是什麼在stdlib.hwait之間的區別和sys/wait.h oO?


在詳細信息 - 我剛遇到這個問題,我不能編譯一個簡單的C程序。我孤立的問題,這裏就是我的了:

#include <stdlib.h> 
//#include <sys/wait.h> 

int main() 
{ 
    int status; 
    wait(&status); 

    return 0; 
} 

如果stdlib.h包括,我得到:

$ gcc asd.cpp 
asd.cpp: In function ‘int main()’: 
asd.cpp:9:16: error: conflicting declaration ‘wait& status’ 
asd.cpp:8:6: error: ‘status’ has a previous declaration as ‘int status’ 

什麼聲明? O.o這裏的wait是什麼,與int status衝突?

我在網上發現了一個線程,在那裏用sys/wait.h代替stdlib.h解決了這個問題,但是爲什麼會這樣呢,又有什麼區別呢?


編輯:感謝sidyll的評論,我改變了文件擴展 - 從.cpp.c和它的工作!我很震驚:)這是如此不同?還有同樣的問題 - 這兩個wait -s之間有什麼不同?

+0

gcc for .cpp文件? – sidyll

+0

@sidyll:你可以使用gcc很好地編譯一個.cpp文件。使用gcc和g ++的唯一區別是,如果你正在進行鏈接,那麼g ++將鏈接到C++標準庫(libstdC++),但gcc不會。 –

+0

OMG !!!!!!!!! Pfffffff ..那是因爲我直接編輯了一箇舊的C++文件.. WTF o.O這怎麼重要? –

回答

2

的不同之處在於wait()<sys/wait.h>是你應該使用的那個。

wait(3)手冊頁:

SYNOPSIS 
     #include <sys/types.h> 
     #include <sys/wait.h> 

     pid_t wait(int *status); 

wait功能不是由ISO C標準定義的,所以標準的C實現不允許聲明它<stdlib.h>(因爲它是合法的程序爲了自己的目的使用名稱wait)。與glibc gcc顯然是這樣做在其默認不符合模式,但如果您調用它與gcc -ansi -pedanticgcc -std=c99 -pedantic,它不能識別功能名稱wait或類型pid_t

3

我做了gcc -E wait.cpp來轉儲發生的實際預處理器擴展。我發現在Linux上,頭文件/usr/include/bits/waitstatus.h包含在union wait { ... }中,但sys/wait.h中的函數wait()永遠不會被引入。c編譯也會發生同樣的情況,但無論出於何種原因編譯器在這種情況下不會抱怨。

爲了證實這一點,你可以改變你的主要申報等待一個變量,而不是一個函數調用,編譯器不會抱怨:

int main() { 
    int status; 
    wait w; 
    return 0; 
} 
+0

不錯,這真的很有趣:) +1 –

1

注意,GCC代表GNU編譯器集,不是GNU C編譯器(作爲被帶有前綴許多 其他工具)。它不是隻有C的編譯器。並且 許多語言被文件擴展名檢測到。亞當羅森菲爾德在他的評論中是正確的 。是的,g++會在鏈接器階段添加C++庫, 但這不是唯一的區別(稍後會有更多內容)。

爲了解釋如何更改擴展解決它,請把這個文本 看看直接從GCC手冊:

Compiling C++ Programs

C++ source files conventionally use one of the suffixes.C, .cc, .cpp,
.CPP, .c++, .cp,or.cxx;C++ header files often use.hhor.H;and
preprocessed C++ files use the suffix .ii. GCC recognizes files with
these names and compiles them as C++ programs even if you call the
compiler the same way as for compiling C programs (usually with the
namegcc).

所以, 「GCC regocnizes文件使用這些名稱」 和你的計劃是b eing編譯爲 作爲C++源代碼。我想C++有一些特殊的用法,我不能告訴 (我不知道C++)&。因此錯誤。現在

,關於g++gcc之間的差異,繼續進行下一 段落:

However, the use ofgccdoes not add the C++ library.g++is a program
that calls GCC and treats.c, .hand.ifiles as C++ source files
instead of C source files unless-xis used, and automatically
specifies linking against the C++ library. This program is also useful
when precompiling a C header file with a.hextension for use in C++
compilations. On many systems,g++is also installed with the name
c++.

關於真正的問題:在我的系統(Darwin 11)中沒有兩個wait s,只有 標準系統調用。檢查凱文說沒有發生。這是一樣的, stdlib。^ h包括SYS/wait.h

#include <_types.h> 
#if !defined(_ANSI_SOURCE) 
#include <sys/wait.h> 
#if (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)) 
#include <alloca.h> 
#endif /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */ 
#endif /* !_ANSI_SOURCE */ 

檢查你的頭。

+0

謝謝,但「GCC使用這些名稱對文件進行regocnizes」並不能解釋爲什麼要處理'.c'和'.cpp'一種不同的方式(正如你的文章中的引號所說,具有這些名稱的所有文件將被視爲C++源文件)。無論如何,+1的幫助和有用的信息(: –

+0

@Kiril Kirov:你可能想再讀一遍:-) ** g ++ **把.c當作C++,而不是** gcc **。並且您正在使用gcc進行編譯,它將cpp識別爲.cpp。然後你改爲.c和gcc,認定爲C. – sidyll

+0

啊,是的,你該死的正確:)對不起,我錯過了第一個報價的一部分。再次感謝:) –