gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:05:40 25 4
gpt4 key购买 nike

我正在尝试让我的一个类(class)使用的 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"

这是我的生成文件:

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 添加到命令行没有帮助。有什么想法吗?

最佳答案

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

关于c++ - 将 C++ 套接字代码移植到 Windows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7543681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com