我正在尝试在 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 标头之后包含它不会出现问题。包括<memory>
很重要。<memory>
包括额外的标题,如<sys/select.h>
,已经包含在内。
我能说的最好的是,MinGW 标头导致了问题。它们提供具有两个不同签名的相同功能:
/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*
。这似乎与邮件列表有关,但我不明白我应该做什么:用 PTIMEVAL 替换 struct timeval 用法并在 LP64 上以不同的方式定义 TIMEVAL。
可能存在一些指针大小问题,详见将应用程序移植到 64 位 Cygwin 时需要注意什么?,但它是 MinGW 代码(而不是 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