将C ++套接字代码移植到Windows

时间:2011-09-25 04:33:26

标签: c++ sockets mingw porting

我正在努力使我的一个类使用的C ++库可以在Windows和Linux上运行(它是为Linux设计的)。它的代码是here(它不大)。我在Windows 7 64位上使用MinGW进行编译。

我在最后的链接阶段遇到HTTPInputStream问题。我编辑了HTTPInputStream.cpp的顶部,以便包含如下所示:

#include <sstream>
#include <iostream>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <sys/types.h>
#ifdef WIN32
    #include <Winsock.h>
    #define bzero(p, l) memset(p, 0, l)
#else
    #include <sys/socket.h>
    #include <netdb.h>
    #include <netinet/in.h>
#endif
#include "CS240Exception.h"
#include "StringUtil.h"

#include "HTTPInputStream.h"

这是我的makefile:

CPP = g++ -g

CS240_UTIL_H = utils/inc/CommandRunner.h utils/inc/FileInputStream.h utils/inc/FileSystem.h utils/inc/HTMLToken.h utils/inc/HTMLTokenizer.h utils/inc/HTTPInputStream.h utils/inc/StringUtil.h utils/inc/URLInputStream.h utils/inc/UnitTest.h utils/inc/CS240Exception.h utils/inc/InputStream.h
CS240_UTIL_CPP = utils/src/CommandRunner.cpp utils/src/FileInputStream.cpp utils/src/FileSystem.cpp utils/src/HTMLToken.cpp utils/src/HTMLTokenizer.cpp utils/src/HTTPInputStream.cpp utils/src/StringUtil.cpp utils/src/URLInputStream.cpp
CS240_UTIL_OBJ = utils/obj/CommandRunner.o utils/obj/FileInputStream.o utils/obj/FileSystem.o utils/obj/HTMLToken.o utils/obj/HTMLTokenizer.o utils/obj/HTTPInputStream.o utils/obj/StringUtil.o utils/obj/URLInputStream.o

all:    clean lib

lib:  $(CS240_UTIL_OBJ)
    $(CPP) -o lib/cs240utils.LIB -I utils/inc $(CS240_UTIL_OBJ)

clean:
    @- rm utils/obj/*.o


#library files

utils/obj/CommandRunner.o:  utils/src/CommandRunner.cpp utils/inc/CommandRunner.h
    $(CPP) -c -o utils/obj/CommandRunner.o -I utils/inc utils/src/CommandRunner.cpp

utils/obj/FileInputStream.o:    utils/src/FileInputStream.cpp utils/inc/FileInputStream.h
    $(CPP) -c -o utils/obj/FileInputStream.o -I utils/inc utils/src/FileInputStream.cpp

utils/obj/FileSystem.o: utils/src/FileSystem.cpp utils/inc/FileSystem.h
    $(CPP) -c -o utils/obj/FileSystem.o -I utils/inc utils/src/FileSystem.cpp

utils/obj/HTMLToken.o:  utils/src/HTMLToken.cpp utils/inc/HTMLToken.h
    $(CPP) -c -o utils/obj/HTMLToken.o -I utils/inc utils/src/HTMLToken.cpp

utils/obj/HTMLTokenizer.o:  utils/src/HTMLTokenizer.cpp utils/inc/HTMLTokenizer.h
    $(CPP) -c -o utils/obj/HTMLTokenizer.o -I utils/inc utils/src/HTMLTokenizer.cpp

utils/obj/HTTPInputStream.o:    utils/src/HTTPInputStream.cpp utils/inc/HTTPInputStream.h
    $(CPP) -c -o utils/obj/HTTPInputStream.o -I utils/inc utils/src/HTTPInputStream.cpp

utils/obj/StringUtil.o: utils/src/StringUtil.cpp utils/inc/StringUtil.h
    $(CPP) -c -o utils/obj/StringUtil.o -I utils/inc utils/src/StringUtil.cpp

utils/obj/URLInputStream.o: utils/src/URLInputStream.cpp utils/inc/URLInputStream.h
    $(CPP) -c -o utils/obj/URLInputStream.o -I utils/inc utils/src/URLInputStream.cpp

这是makefile的输出:

I:>make -f testmake.txt
g++ -g -c -o utils/obj/CommandRunner.o -I utils/include utils/src/CommandRunner.cpp
g++ -g -c -o utils/obj/FileInputStream.o -I utils/include utils/src/FileInputStream.cpp
g++ -g -c -o utils/obj/FileSystem.o -I utils/include utils/src/FileSystem.cpp
g++ -g -c -o utils/obj/HTMLToken.o -I utils/include utils/src/HTMLToken.cpp
g++ -g -c -o utils/obj/HTMLTokenizer.o -I utils/include utils/src/HTMLTokenizer.cpp
g++ -g -c -o utils/obj/HTTPInputStream.o -I utils/include utils/src/HTTPInputStream.cpp
g++ -g -c -o utils/obj/StringUtil.o -I utils/include utils/src/StringUtil.cpp
g++ -g -c -o utils/obj/URLInputStream.o -I utils/include utils/src/URLInputStream.cpp
g++ -g -o lib/cs240utils.LIB -I utils/include utils/obj/CommandRunner.o utils/obj/FileInputStream.o utils/obj/FileSystem.o utils/obj/HTMLToken.o utils/obj/HTMLTokenizer.o utils/obj/HTTPInputStream.o utils/obj/StringUtil.o utils/obj/URLInputStream.o
utils/obj/HTTPInputStream.o:I:/utils/src/HTTPInputStream.cpp:246: undefined reference to `_imp__gethostbyname@4'
utils/obj/HTTPInputStream.o:I:/utils/src/HTTPInputStream.cpp:255: undefined reference to `_imp__htons@4'
utils/obj/HTTPInputStream.o:I:/utils/src/HTTPInputStream.cpp:258: undefined reference to `_imp__socket@12'
utils/obj/HTTPInputStream.o:I:/utils/src/HTTPInputStream.cpp:264: undefined reference to `_imp__connect@12'
c:/strawberry/c/bin/../lib/gcc/i686-w64-mingw32/4.4.3/../../../../i686-w64-mingw
32/lib/libmingw32.a(lib32_libmingw32_a-crt0_c.o): In function `main':/opt/W64_156151-src.32/build-crt/../mingw-w64-crt/crt/crt0_c.c:18: undefined reference to `WinMain@16'
collect2: ld returned 1 exit status
make: *** [lib] Error 1

我一直在谷歌搜索这个,并没有找到解决这个问题的任何东西。将-lwsock32添加到命令行没有帮助。有什么想法吗?

1 个答案:

答案 0 :(得分:3)

假设cs240utils.lib是一个静态库,则不要使用g++来创建它。相反,您使用ar(或lib,如果使用MSVC工具链)将所有文件组合到库中。