2009-09-23 77 views
0

我正在尋找將此Windows工具移植到Unix(Mac/OSX)的幫助;需要幫助將Win32 C++移植到Unix

http://www.oxyron.de/html/netdrive01src.zip

首先,我想知道,如果任何人都可以花一點時間來快速瀏覽一下在(小)的源代碼,如果它是一個相當簡單的任務。其次,關於移植代碼的任何提示將不勝感激。我試過編譯它(使用Xcode/G ++),但它會拋出80多個錯誤,其中許多錯誤似乎與常量,如「_MAX_FNAME」,我假設是一個系統常量來定義最大值文件名長度。我試圖通過標準的C頭文件(在我的Mac上)來查看,但是當我在SYSLIMITS.H(NAME_MAX)中發現了類似的常量時,我​​似乎還沒有取得很大的進展。

+0

奇怪的是,它不包含windows.h並且在戰略位置擁有#ifdef WIN32,但仍需要移植 - 您是否已經開始移植代碼? – 2009-09-23 19:31:46

+0

我應該說這不是我的代碼。如果可能的話,我只是試圖構建一個unix版本的工具。 :) – 2009-09-23 20:32:52

回答

1

如果大部分錯誤都是關於預處理器定義的,那麼您可以在編譯器命令行中添加這些定義。例如,對於g ++,這將是-D _MAX_FNAME=NAME_MAX選項(或任何適用的值)。

0

對代碼(grep "#include <")的簡短介紹顯示所有平臺依賴項都位於nethost頭中。這頭包含所有需要的#ifdef處理平臺的獨立性:

#ifdef WIN32 
    #define WIN32_LEAN_AND_MEAN 1 
    #include <winsock2.h> 
#else 
    #ifdef __APPLE__ 
     #include <netinet/in.h> 
    #endif 

    #include <sys/types.h> 
    #include <sys/socket.h> 
    #include <netdb.h> 
    #include <unistd.h> 
    #include <arpa/inet.h> 
    #include <sys/select.h> 
#endif 

看起來不錯。第一眼。

3

似乎有幾個不同類別的錯誤:

main.cpp:8:19: error: conio.h: No such file or directory 
main.cpp:61: error: ‘_kbhit’ was not declared in this scope 

conio.h是窗口的控制檯IO頭。 _kbhit是其中的一個功能。

main.cpp:17: warning: deprecated conversion from string constant to ‘char*’ 

ANSI C++中的字符串常量類型爲const char *。如果你使用C++ std :: string而不是使用new的C字符串,那麼在代碼中也會出現很多奇怪的字符串函數。

vbinary.cpp:5:16: error: io.h: No such file or directory 

vdirectory.cpp:91: error: ‘_findfirst’ was not declared in this scope 
vdirectory.cpp:99: error: ‘_findnext’ was not declared in this scope 
vdirectory.cpp:101: error: ‘_findclose’ was not declared in this scope 
vfile.cpp:19: error: ‘_MAX_DRIVE’ was not declared in this scope 
vfile.cpp:20: error: ‘_MAX_DIR’ was not declared in this scope 
vfile.cpp:21: error: ‘_MAX_FNAME’ was not declared in this scope 
vfile.cpp:22: error: ‘_MAX_EXT’ was not declared in this scope 

io.h是另一個微軟頭,與導航目錄的功能,並與他們使用的宏。改爲使用dirent.h中的功能。諸如VDirectory::CreatePath之類的函數假定存在單獨的驅動器號; unix文件系統不這樣做,因此最好爲這些實現設置完全分離的類,而不是嘗試使用#ifdef s將兩個單獨的實體放入每個函數中,並使用一個#ifdef在main中選擇合適的一個。

常量如_MAX_FNAME在io.h和微軟的stdlib.h中。它們不在標準stdlib.h中,它們的輸入大小也不受限制。 POSIX版本使用NAME_MAX

+0

感謝您的所有信息。在你看來,你認爲它的寫作過於以Windows爲中心,成爲unix的一個簡單端口嗎?我主要想的是驅動器/文件夾/文件處理的東西。 – 2009-09-23 20:39:09

+0

使用調用POSIX文件和目錄處理的相同接口來編寫和測試一組類的時間不應超過幾個小時。儘管我也會檢查它所做的任何事情是否已經在現有的軟件中實現, Linux中有各種保險絲(用戶空間中的文件系統)網絡驅動器安裝系統。 – 2009-09-23 21:54:58