我正在尝试在MinGW上构建Botan,这实际上是MinGW-w64(x86_64-pc-msys)。 MinGW失败,如下所示。我想我把它简化为MCVE:
#include <sys/select.h>
#include <winsock2.h>
#include <windows.h>
#include <memory>
int main(int argc, char* argv[])
{
fd_set fds;
FD_ZERO(&fds);
FD_SET(0, &fds);
timeval tv;
tv.tv_sec = 5;
tv.tv_usec = 0;
int rc = select(1, &fds, NULL, NULL, &tv);
return 0;
}
结果是:
$ g++ -m64 -pthread -std=c++11 test.cxx -o test.exe
In file included from /usr/include/w32api/winsock2.h:56:0,
from test.cxx:2:
/usr/include/w32api/psdk_inc/_fd_types.h:100:2: warning: #warning "fd_set and associated macros have been defined in sys/types. This can cause runtime problems with W32 sockets" [-Wcpp]
#warning "fd_set and associated macros have been defined in sys/types. \
^~~~~~~
In file included from test.cxx:2:0:
/usr/include/w32api/winsock2.h:995:34: error: conflicting declaration of C function ‘int select(int, _types_fd_set*, _types_fd_set*, _types_fd_set*, PTIMEVAL)’
WINSOCK_API_LINKAGE int WSAAPI select(int nfds,fd_set *readfds,fd_set *writefds,fd_set *exceptfds,const PTIMEVAL timeout);
^~~~~~
In file included from test.cxx:1:0:
/usr/include/sys/select.h:73:5: note: previous declaration ‘int select(int, _types_fd_set*, _types_fd_set*, _types_fd_set*, timeval*)’
int select __P ((int __n, fd_set *__readfds, fd_set *__writefds,
^~~~~~
首先包括
<sys/select.h>
很重要。在Windows header 之后包含它不会出现问题。包括<memory>
很重要。 <memory>
包含其他 header ,例如<sys/select.h>
,已包含在内。我能说的最好的是,MinGW header 引起了问题。它们提供具有两个不同签名的相同功能:
/usr/include/w32api/winsock2.h
:int select(int, _types_fd_set*, _types_fd_set*, _types_fd_set*, PTIMEVAL)
/usr/include/sys/select.h
int select(int, _types_fd_set*, _types_fd_set*, _types_fd_set*, timeval*)
问题似乎是最后一个争论。在一种情况下,它是
PTIMEVAL
,在另一种情况下,它是timeval*
。这似乎与邮件列表有关,但是我不知道该怎么做:Replace struct timeval usage with PTIMEVAL and define TIMEVAL differently on LP64。如What do I have to look out for when porting applications to 64 bit Cygwin?所述,可能存在一些指针大小问题,但这是MinGW代码(而非Botan代码)。
有什么问题,我该如何解决?
这是Botan的
src/lib/utils/socket/socket.cpp
。这是编译错误:
g++ -fstack-protector -m64 -pthread -std=c++11 -D_REENTRANT -O3 -momit-leaf-frame-pointer \
-Wall -Wextra -Wpedantic -Wstrict-aliasing -Wcast-align -Wmissing-declarations \
-Wpointer-arith -Wcast-qual -Wzero-as-null-pointer-constant -Wnon-virtual-dtor \
-Ibuild/include -c src/lib/utils/socket/socket.cpp -o build/obj/lib/utils_socket.o
In file included from /usr/include/w32api/winsock2.h:56:0,
from src/lib/utils/socket/socket.cpp:35:
/usr/include/w32api/psdk_inc/_fd_types.h:100:2: warning: #warning "fd_set and associated macros have been defined in sys/types. This can cause runtime problems with W32 sockets" [-Wcpp]
#warning "fd_set and associated macros have been defined in sys/types. \
^~~~~~~
In file included from src/lib/utils/socket/socket.cpp:35:0:
/usr/include/w32api/winsock2.h:995:34: error: conflicting declaration of C function ‘int select(int, _types_fd_set*, _types_fd_set*, _types_fd_set*, PTIMEVAL)’
WINSOCK_API_LINKAGE int WSAAPI select(int nfds,fd_set *readfds,fd_set *writefds,fd_set *exceptfds,const PTIMEVAL timeout);
^~~~~~
In file included from /usr/include/sys/types.h:68:0,
from /usr/include/pthread.h:11,
from /usr/lib/gcc/x86_64-pc-msys/6.3.0/include/c++/x86_64-pc-msys/bits/gthr-default.h:35,
from /usr/lib/gcc/x86_64-pc-msys/6.3.0/include/c++/x86_64-pc-msys/bits/gthr.h:148,
from /usr/lib/gcc/x86_64-pc-msys/6.3.0/include/c++/ext/atomicity.h:35,
from /usr/lib/gcc/x86_64-pc-msys/6.3.0/include/c++/memory:73,
from build/include/botan/types.h:17,
from build/include/botan/internal/socket.h:11,
from src/lib/utils/socket/socket.cpp:8:
/usr/include/sys/select.h:73:5: note: previous declaration ‘int select(int, _types_fd_set*, _types_fd_set*, _types_fd_set*, timeval*)’
int select __P ((int __n, fd_set *__readfds, fd_set *__writefds,
^~~~~~
make: *** [Makefile:1066: build/obj/lib/utils_socket.o] Error 1
最佳答案
我怀疑这是错误的目标-您无意中为MSYS2环境本身(确实是POSIXy)进行构建。这就是为什么<sys/select.h>
被拉入并导致冲突。至少,我非常怀疑如果您打算仅在MSYS2环境中进行构建,则您将尝试使用<winsock2.h>
或<windows.h>
。您应该确保从命令行正确调用了MSYS/使用了正确的快捷方式,以便将其配置为针对MINGW64进行构建,或者,如果已安装,请确保已为MINGW64安装了gcc工具链。如果您没有这样做,那么即使您已正确打开MINGW64,它也会将默默默认返回MSYS2的gcc工具链,这将继续引入特定于MSYS2构建的内容。可以通过运行来安装(或重新安装)用于MINGW64的GCC
pacman -Sy mingw-w64-x86_64-gcc
您还可以使用以下方法安装或重新安装Windows特定的头文件:
pacman -Sy mingw-w64-x86_64-headers-git
完成此操作后,运行
uname
确认您处于正确的构建环境中,这应该会产生然后
gcc -dumpmachine
,应该产生对于MSYS2,这些值为
和
分别。
为什么我知道这一点?从字面上看,我整夜都熬着同样的问题,直到注意到_WIN32和_WIN64都未定义以及包含文件的来源(
/usr/include
而不是/mingw64/x86_64-w64-mingw32/include
)之后,我才意识到我正在为x86_64进行构建-pc-msys,而不是x86_64-w64-mingw32。(当然,如果您要针对32位Windows进行构建,则以上所有内容同样适用。请确保已正确调用MINGW32环境,并且已运行
pacman -Sy mingw-w64-i686-gcc mingw-w64-i686-headers-git
,所以不会结束)尝试使用为MSYS2配置的gcc进行构建。)无论如何,我敢肯定,这个问题已经存在两年了,OP已经发展了。但这是一个容易犯的配置错误。因此,无论如何,我希望这会有所帮助!不要误会我的意思!并确保所有人安全!