我正在开发一个 C++ 项目,我必须在其中连接到 redis 数据库。我正在尝试获取 credis代码可以工作,但是当我编译它时,我得到了这些错误集
1>c:\c++redis\credis.c(728): warning C4013: 'fcntl' undefined; assuming extern returning int
1>c:\c++redis\credis.c(728): error C2065: 'F_GETFL' : undeclared identifier
1>c:\c++redis\credis.c(729): error C2065: 'F_SETFL' : undeclared identifier
1>c:\c++redis\credis.c(729): error C2065: 'O_NONBLOCK' : undeclared identifier
1>c:\c++redis\credis.c(734): error C2065: 'EINPROGRESS' : undeclared identifier
1>c:\c++redis\credis.c(740): warning C4133: 'function' : incompatible types - from 'int *' to 'char *'
错误在credis.c
文件728到746行
/* connect with user specified timeout */
flags = fcntl(fd, F_GETFL);
if ((rc = fcntl(fd, F_SETFL, flags | O_NONBLOCK)) < 0) {
DEBUG("Setting socket non-blocking failed with: %d\n", rc);
}
if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) != 0) {
if (errno != EINPROGRESS)
goto error;
if (cr_selectwritable(fd, timeout) > 0) {
int err;
unsigned int len = sizeof(err);
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len) == -1 || err)
goto error;
}
else /* timeout or select error */
goto error;
}
/* else connect completed immediately */
我在哪里可以找到这些缺失的类型名称?
我正在使用 visual studio 2010 进行编译,程序必须在窗口上运行。
我尝试用 this 对代码进行批处理建议的答案,但这没有帮助。
最佳答案
您至少缺少一个头文件:
#include <fcntl.h>
这应该可以解决您的一些问题。通常,查找头文件名称的好地方是函数本身的帮助文本。在这种情况下,头文件与函数同名 (fcntl
),但大多数都不是那么简单。
EINPROGRESS
定义于:
#include <errno.h>
为了将来引用,E
前缀通常意味着它是一个错误宏,所以 errno.h
是第一个要看的地方。
'function' : incompatible types - from 'int *' to 'char *'
可能意味着您的原型(prototype)不匹配。您的原型(prototype)与函数本身不匹配。更新原型(prototype)。
编辑:
虽然这会解决您的一些问题,但看起来这是 UNIX 代码(请参阅注释)。 F_GETFL
和 F_SETFL
,例如,在 Windows 上似乎不受支持。 O_NONBLOCK
在unistd.h
在 UNIX 上。
您将需要重写代码中需要此功能的部分,或者更好的是,从您的供应商处获取 Windows 版本。
从您更新的帖子来看,这些是使用套接字完成的。套接字相当便携,但也存在一些问题。对于非阻塞套接字,请使用 ioctlsocket()
在 Windows 上。
例子:
int iRetn = ioctlsocket(s, FIONBIO, 1);
哪里s
是socket,第三个参数为0表示阻塞,非0表示非阻塞。
您还需要#include <winsock.h>
并调用WSAStartup()
在使用任何套接字例程之前,调用 WSACleanup()
最后。
(老实说,这就是我现在能想到的,我没有意识到我会在套接字上回答问题)。
关于c++ - 让 credis 在 C++ 解决方案中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15729530/