在windows環境下寫linux代碼
我在linux下寫代碼時一直找不到方便的工具,vi和emacs雖然提供了自動完成的功能,但是還是沒有
vc+visual assistant方便,因此就產生了在windows下 linux代碼的想法,這個企圖曾經被狒狒鄙視為
“自做孽,不可活”,不過經過實踐,村長我還是活過來了:-)
一、實驗環境
1、裝有win2k的機器一台,並裝好了vc6+vs2003+visual assistant+source insight
2、裝有linux as3的機器一台,安裝了所有的軟體包
二、步驟
1、將linux機器上的/usr/include拷貝到windows機器上的指定目錄如
f:/linuxhead下
把/usr/lib/gcc-lib/i386-redhat-linux/3.2.3/include/也拷到剛才的目錄下,該個名字為"gcchead"
2、在vs2003的標頭檔包含路徑("工具"->"選項"->“項目”->"vc++目錄"->"包含檔案")加入剛才的
f:/linuxhead/include目錄
f:/linuxhead/gcchead/include目錄
並保證這兩個目錄在目錄列表的最上方
3、建立一個空項目,加入一個 main.cpp檔案,然後將常用的linux標頭檔包含進去,編譯之。
//main.cpp的內容
#include <sys/types.h>
#include <sys/socket.h>
#include <time.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/un.h>
#include <sys/select.h>
#include <sys/param.h>
#include <sys/poll.h>
#include <strings.h>
#include <sys/ioctl.h>
#include <pthread.h>
#include <syslog.h>
#include <termios.h>
#include <grp.h>
#include <pwd.h>
#include <sys/resource.h>
#include <setjmp.h>
#include <sys/mman.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <linux/dirent.h>
#include <linux/unistd.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/utsname.h>
#include <utime.h>
#include <sys/wait.h>
int main(int argc ,char* argv[])
{
return 0;
}
編譯時間出現的錯誤依次為
1、bits/types.h(31): fatal error C1083: 無法開啟包含檔案:“stddef.h”: No such file or directory
說明沒找到stddef.h檔案
發現/linux目錄下有這個檔案,將linux目錄加到包含目錄裡
2、bits/in.h(69): error C2380: “ip_opts”前的類型(建構函式有傳回型別或是當前類型名稱的非法重定義?)
因為
struct ip_opts
{
struct in_addr ip_dst; /* First hop; zero without source route. */
char ip_opts[40]; /* Actually variable in size. */
};成員名與結構名相同,vc認為是錯誤,所以只好改成員名為ip_opta
3、gcchead/include/stdarg.h(43): error C2146: 語法錯誤 : 缺少“;”(在標識符“__gnuc_va_list”的前面)
typedef __builtin_va_list __gnuc_va_list;
由於__builtin_va_list類型不存在,所以改為
typedef void* __gnuc_va_list;
4、stdlib.h(833): error C2065: “wchar_t” : 未聲明的標識符
extern int mbtowc (wchar_t *__restrict __pwc,
__const char *__restrict __s, size_t __n) __THROW;
在前面增加一句
#include "linux/Nls.h"
5、linux/nls.h(7): error C2146: 語法錯誤 : 缺少“;”(在標識符“wchar_t”的前面)
typedef __u16 wchar_t;
增加
#include "asm/types.h"
6、asm/types.h(11): error C2144: 語法錯誤 : “char”的前面應有“;”
typedef __signed__ char __s8;
增加一句#define __signed__ signed
7、linux/dirent.h(6): error C2146: 語法錯誤 : 缺少“;”(在標識符“d_off”的前面)
__kernel_off_t d_off;
增加一行#include "asm/posix_types.h"
8、linux/dirent.h(12): error C2146: 語法錯誤 : 缺少“;”(在標識符“d_ino”的前面)
__u64 d_ino;
增加一行#include "asm/types.h"
並在asm/types.h檔案裡增加兩行
typedef __signed__ long long __s64;
typedef unsigned long long __u64;
解決上述問題後,編譯通過.
p.s網友超越無限曾告訴我magic c++也能實現我需要的功能,然而我下載了他的4.0使用版後,毛病百出,最後以失敗告終
只好“自己動手、豐衣足食”了,呵呵