為Windows 使用者恢復「unistd.h」遺失的元素
在跨平台編碼的世界中,遇到特定於平台的情況障礙是不可避免的。將程式碼從 Unix 移植到 Windows 的程式設計師面臨的常見挑戰之一是 Visual C 中缺少「unistd.h」。這個問題的解決方案在於要麼替換單一函數,要麼尋求更全面的解決方案。
對於那些尋求全面方法的人來說,社群正在努力創建一個與 Windows 相容的“unistd.h”,它涵蓋了其 Unix 版本的基本功能。以下程式碼片段提供了一個起點,鼓勵使用者根據需求做出貢獻。
#ifndef _UNISTD_H #define _UNISTD_H 1 /* This is intended as a drop-in replacement for unistd.h on Windows. * Please add functionality as needed. * https://stackoverflow.com/a/826027/1202830 */ #include <stdlib.h> #include <io.h> #include <getopt.h> /* getopt at: https://gist.github.com/ashelly/7776712 */ #include <process.h> /* for getpid() and the exec..() family */ #include <direct.h> /* for _getcwd() and _chdir() */ #define srandom srand #define random rand /* Values for the second argument to access. These may be OR'd together. */ #define R_OK 4 /* Test for read permission. */ #define W_OK 2 /* Test for write permission. */ //#define X_OK 1 /* execute permission - unsupported in windows*/ #define F_OK 0 /* Test for existence. */ #define access _access #define dup2 _dup2 #define execve _execve #define ftruncate _chsize #define unlink _unlink #define fileno _fileno #define getcwd _getcwd #define chdir _chdir #define isatty _isatty #define lseek _lseek /* read, write, and close are NOT being #defined here, because while there are file handle specific versions for Windows, they probably don't work for sockets. You need to look at your app and consider whether to call e.g. closesocket(). */ #ifdef _WIN64 #define ssize_t __int64 #else #define ssize_t long #endif #define STDIN_FILENO 0 #define STDOUT_FILENO 1 #define STDERR_FILENO 2 /* should be in some equivalent to <sys> */ typedef __int8 int8_t; typedef __int16 int16_t; typedef __int32 int32_t; typedef __int64 int64_t; typedef unsigned __int8 uint8_t; typedef unsigned __int16 uint16_t; typedef unsigned __int32 uint32_t; typedef unsigned __int64 uint64_t; #endif /* unistd.h */</sys></direct.h></process.h></getopt.h></io.h></stdlib.h>
但是,那些尋求更有針對性的解決方案的人可以單獨解決缺失的功能。例如,隨機函數可以用“srand”和“rand”替換,“strcmp”用“_stricmp”替換。對於“getopt”,外部函式庫“gist.github.com/ashely/7776712”提供了一個實作。
雖然建立自訂「unistd.h」是可能的,但值得探索是否有人已經完成了更廣泛功能的繁重工作。這種方法充分利用了社區的努力並節省了寶貴的開發時間。
以上是如何恢復 Windows 中缺少的「unistd.h」功能以進行跨平台 C 開發?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本文詳細介紹了C函數返回類型,包括基本(int,float,char等),派生(數組,指針,結構)和void類型。 編譯器通過函數聲明和返回語句確定返回類型,執行

Gulc是一個高性能的C庫,優先考慮最小開銷,積極的內襯和編譯器優化。 其設計非常適合高頻交易和嵌入式系統等關鍵應用程序,其設計強調簡單性,模型

本文解釋了C函數聲明與定義,參數傳遞(按值和指針),返回值以及常見的陷阱,例如內存洩漏和類型不匹配。 它強調了聲明對模塊化和省份的重要性

本文詳細介紹了字符串案例轉換的C功能。 它可以通過ctype.h的toupper()和tolower()解釋,並通過字符串迭代並處理零終端。 常見的陷阱,例如忘記ctype.h和修改字符串文字是

本文研究C函數返回值存儲。 較小的返回值通常存儲在寄存器中以備速度;較大的值可能會使用指針來記憶(堆棧或堆),影響壽命並需要手動內存管理。直接ACC

本文分析了形容詞“獨特”的多方面用途,探索其語法功能,常見的短語(例如,“不同於”,“完全不同”),以及在正式與非正式中的細微應用

本文詳細介紹了c中有效的STL算法用法。 它強調了數據結構選擇(向量與列表),算法複雜性分析(例如,std :: sort vs. std vs. std :: partial_sort),迭代器用法和並行執行。 常見的陷阱

本文解釋了C標準模板庫(STL),重點關注其核心組件:容器,迭代器,算法和函子。 它詳細介紹了這些如何交互以啟用通用編程,提高代碼效率和可讀性t


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Dreamweaver CS6
視覺化網頁開發工具

禪工作室 13.0.1
強大的PHP整合開發環境

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Atom編輯器mac版下載
最受歡迎的的開源編輯器