![](/img/trans.png)
[英]How do I use **environ from unistd.h to search for the right path?
[英]Why do some code files use close() without including unistd.h?
以以下示例為例: http : //codebase.eu/tutorial/linux-socket-programming-c和http://codebase.eu/tutorial/linux-socket-programming-c/code/tcpclient.cpp
基本上,第一個示例應用程序使用close(),但不包括unistd.h。 有人在評論中提到了這一點。 它適用於一些沒有它的人,而其他人則必須包含它。
我的問題是為什么:為什么有時沒有必要? 始終包含它是否更好? 不需要包含它是什么意思?
引用的代碼使用<iostream>
。 unistd.h
可能因此而被包含在內。 但是,如果要使用close()
,則不要指望它,也不要自己包含unistd.h
。
使用int close(int);
時最好包含#include <unistd.h>
int close(int);
因為這是保證int close(int);
的聲明的唯一方法int close(int);
在POSIX平台上編譯時,將可用於您的源代碼。 在某些情況下,其他包含的文件(或您的構建鏈)可能包含unistd.h
或包含對其的前向聲明。
當C ++預處理器處理您的源代碼時,它基本上將您的#include <something.h>
行替換為something.h
的預處理內容。 因此,如果something.h
(或遞歸包含的頭文件之一)具有#include <unistd.h>
行,則您的源代碼將看到int close(int);
的聲明int close(int);
即使您沒有直接包含unistd.h
標頭也是如此。
但是,為確保可移植性,如果使用int close(int);
,請始終包含unistd.h
int close(int);
功能( man 3p close
)!
如果要查找可能包含頭文件的位置,請參閱此問題 。 同樣,您可以對g++
/ clang++
使用-E
標志來輸出源代碼的完全預處理版本,以了解int close(int);
的聲明在哪里int close(int);
來自。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.