开发者

Porting C++ socket code to Windows

开发者 https://www.devze.com 2023-04-08 00:16 出处:网络
I am trying to make a C++ library used by one of my classes work on both Windows and Linux (it was designed for Linux). The code for it is here (it\'s not large). I am compiling with MinGW on Windows

I am trying to make a C++ library used by one of my classes work on both Windows and Linux (it was designed for Linux). The code for it is here (it's not large). I am compiling with MinGW on Windows 7 64 bit.

I'm running into trouble with HTTPInputStream during the final linking stage. I edited the top of HTTPInputStream.cpp so that the includes look like this:

#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"

Here is my 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

And here is the output from that 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开发者_StackOverflow中文版 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

I've been googling about this a lot and haven't found anything that solves this. Adding -lwsock32 to the command line doesn't help. Any ideas?


Assuming the cs240utils.lib is meant to be a static library, you don't use g++ to create it. Instead, you use ar (or lib, if using the MSVC toolchain) to assemble all the files into the library.

0

精彩评论

暂无评论...
验证码 换一张
取 消