Getting credis to work in a C++ solution - c++

I'm working on a C++ project where I must connect to redis database. I'm trying to get the credis code to work but when I compile it I get these sets of errors
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 *'
The error is in the credis.c file from line 728 to 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 */
Where can I find these missing typenames?
I'm using visual studio 2010 to compile this and the program must run on window.
I have tried to batch the code with this suggested answer, but that didn't help.

You have at least one header file missing:
#include <fcntl.h>
that should fix some of your issues. Generally, a good place to look for the header file names is in the help text for the function itself. In this case the header file has the same name as the function (fcntl) but most are not that easy.
EINPROGRESSis defined in:
#include <errno.h>
for future reference, the E prefix usually means it is an error macro, so errno.h is the first place to look.
'function' : incompatible types - from 'int *' to 'char *' probably means you have a prototype mis-match. Your prototype does not match the function itself. Update the prototype.
Edit:
Although that will fix some of your issues, it appears that this is UNIX code (see comments). F_GETFL and F_SETFL, for example, appears not to be supported on Windows. O_NONBLOCK is in unistd.h on UNIX.
You will need to rewrite the parts of the code requiring this functionality or, better yet, get the Windows version from your supplier.
From your updated post, these are done using sockets. Sockets are fairly portable, but there are some issues. For non-blocking sockets use ioctlsocket() on Windows.
Example:
int iRetn = ioctlsocket(s, FIONBIO, 1);
where s is the socket, the third parameter is 0 for blocking, non-zero for non-blocking.
You also need to #include <winsock.h> and call WSAStartup() before using any socket routines, and call WSACleanup() at end.
(To be honest that's all I can think of right now, I didn't realise I would be answering issues on sockets).

The library works well on linux,but not windows.
On Windows,I use https://code.google.com/p/libredic/

Related

How to detect USRP usb type?

When i include uhd/usb_control.hpp in my main.cpp :
#include <uhd/transport/usb_control.hpp>
/* Some other includes */
int main (void)
{
uhd::transport::usb_control::sptr usbSpeed;
usbSpeed = uhd::transport::usb_control::make(handle, 0);
/* `handle` is a `usb_device_handle::vid_pid_pair_t` */
}
I got error from here:
static sptr make(usb_device_handle::sptr handle, const int interface);
Error:
unexpected token struct. Did you forget a ';'
struct: missing tag name
And another strange error in:
usbSpeed = uhd::transport::usb_control::make(handle, 0);
Error:
Cannot convert argument 2 from int to const int
The only implementation that i find for uhd::transport::usb_control::make is uhd/transport/usb_dummy_impl.cpp which only throw an exception.
Environment information:
Compiler: MS Visual Studio 2017
OS: MS Windows 10
C++ Standard: 17
How to fix those errors ? I only what to detect the USRP usb type. For this i read the uhd source code and i find the uhd/transport/usb_control.hpp, But I have encountered those errors.
maybe the cause of this unexpected behavior is related to your included files and a conflict between some of them, as you mentioned in addition of #include <uhd/transport/usb_control.hpp> you have some other includes. i suggest move this include line upper and lower of other includes and test your code again.
wish my suggest be useful.

SOCKET in winsock2.h not accepted in visual studio 2017

Getting error i freetds headers when i include them in my VS 2017 c++ project
in tds.h when i include that in my project
include\tds.h(1331): error C3646: 's': unknown override specifier
include\tds.h(1331): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
include\tds.h(1641): error C2065: 'TDS_SYS_SOCKET': undeclared identifier
include\tds.h(1641): error C2146: syntax error: missing ')' before identifier 's'
I am trying to use freetds 0.91.100 version in my c++ application which was built using VS 2010. It was fine then.
Now after migrating my C++ project to VS 2017 i am getting strange errors.
I have built freetds lib as well in VS 2017.
freetds has this declaration in tds_sysdep_private.h
#if !defined(__WIN32__) && !defined(_WIN32) && !defined(WIN32)
typedef int TDS_SYS_SOCKET;
#define INVALID_SOCKET -1
#define TDS_IS_SOCKET_INVALID(s) ((s) < 0)
#else
typedef SOCKET TDS_SYS_SOCKET;
#define TDS_IS_SOCKET_INVALID(s) ((s) == INVALID_SOCKET)
#endif
and the tds.h has
struct tds_socket
{
TDS_SYS_SOCKET s; /**< tcp socket, INVALID_SOCKET if not connected */
}
And the error is on this TDS_SYS_SOCKET declaration
My code include this header this way.
tdsloader.h
using namespace std;
#if defined (__cplusplus)
extern "C" {
#endif
#include "tds.h"
#if defined (__cplusplus)
}
#endif
As per the declaration of TDS_SYS_SOCKET in tds_sysdep_private.h , in case of windows build it is defined as SOCKET which is from winsock2.h
I read in other threads that the order of header file includion is important and i made sure that winsock2.h is included before windows.h or any other windows header file.
Now that SOCKET from winsock2.h is
typedef UINT_PTR SOCKET;
which in an unsigned , why is VS 2017 not able to recognize the type ?
Build should go through smoothly as it did in VS 2010.
Now with VS 2017 it shows build errors.
Here's the include order that works fine with VS 2017:
#include <tds_sysdep_private.h>
#include <tds.h>
alternatively you can:
#define _FREETDS_LIBRARY_SOURCE
#include <tds.h>

C++ MSVC dll compiling error missing type specifier and redefinition

I'm using Microsoft Visual Studio Community 2015 Update 3 and im compiling a dll. I want to call the function InitPreySplitPipe i have already tried calling it with ::pr::InitPreySplitPipe and i copied the interprocess.cpp code into game_local.cpp and interprocess.hpp code into game_local.h and call it from there but that didnt work either.
I have set up a GitHub repository with all the code if some one is interested.
Thanks for reading and sorry for my bad english :/
Compiler output:
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int
Error C2371 'pr::InitPreySplitPipe': redefinition; different basic types
Error C2556 'int InitPreySplitPipe(void)': overloaded function differs only by return type from 'void pr::InitPreySplitPipe(void)'
interprocess.hpp
namespace pr
{
(...)
void InitPreySplitPipe();
(...)
}
interprocess.cpp
#include "interprocess.hpp"
namespace pr
{
(...)
void InitPreySplitPipe()
{
pipe_preysplit = CreateNamedPipe("\\\\.\\pipe\\" PREYSPLIT_PIPE_NAME,PIPE_ACCESS_OUTBOUND | FILE_FLAG_OVERLAPPED, PIPE_TYPE_MESSAGE | PIPE_REJECT_REMOTE_CLIENTS, 1, 256 * 1000, 0, 0, NULL);
if (pipe_preysplit == INVALID_HANDLE_VALUE)
{
return;
}
std::memset(&overlapped, 0, sizeof(overlapped));
overlapped.hEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
if (overlapped.hEvent == NULL)
{
pipe_preysplit = INVALID_HANDLE_VALUE;
}
}
(...)
}
// Where the function shoud be called
game_local.cpp
(...)
#include "../PreyRun/interprocess.hpp"
pr::InitPreySplitPipe();
(...)
In game_local.cpp:
// PreyRun BEGIN
#include "../PreyRun/interprocess.hpp"
pr::InitPreySplitPipe();
// PreyRun END
You are not calling the function from within anything, so the compiler thinks it's a new function declaration. Thus the type int is assumed, of which the overloaded function int pr:InitPreySplitPipe() conflicts with your void pr::InitPreySplitPipe(). This explains all 3 error messages.

None of the 3 overloads could convert all the argument types (in MFC / C++ project)

I have copied some code from another project which I downloaded (and which compiled fine) and get the compiler error message when compiling the same code ( a file called player.cpp) in my own project:
Error 1 error C2665: 'MATExceptions::MATExceptions' : none of the 3 overloads could convert all the argument types c:\users\daniel\documents\visual studio 2012\projects\mytest1\mytest1\player.cpp 137 1 Test1
The error occurs on this line in player.cpp:
EXCEP(DirectSoundErr::GetErrDesc(hres), _T("Player::CreateDS DirectSoundCreate"));
Here is the definition of EXCEP and GetErrDesc:
#define EXCEP(/*const wchar_t * */ desc, /*const wchar_t * */ from) throw( MATExceptions(__LINE__, _T(__FILE__), 0, from, desc) );
CComBSTR DirectSoundErr::GetErrDesc(HRESULT hres)
{
switch(hres)
{
case DSERR_ALLOCATED :
return _T("The request failed because resources, such as a
priority level, were already in use by another caller.");
...
default : return _T("Unknown error");
}
}
I don't know what is different (as I have not changed the source file player.cpp). Could it be due to different compiler settings in my project compared to the original (how would I check this)?
I changed the EXCEP definition to the following:
#define EXCEP(desc, from) throw(MATExceptions(__LINE__, (wchar_t *)(__FILE__), 0, (wchar_t *)from, (wchar_t *)desc));
...and changed the call from:
EXCEP(DirectSoundErr::GetErrDesc(hres), _T("Player::CreateDS DirectSoundCreate"));
to:
EXCEP(DirectSoundErr::GetErrDesc(hres), "Player::CreateDS DirectSoundCreate");
Is that acceptable?
The original "new" can be killed by defining these in project (since Visual Studio 2015 I guess):
__PLACEMENT_NEW_INLINE
__PLACEMENT_VEC_NEW_INLINE
But once they are gone, they are gone. Now you need to make sure to include the project-specific header file which redefines them.

How do I use errorno and _get_errno?

Calling system() to run an external .exe and checking error code upon errors:
#include <errno.h>
#include <stdlib.h>
function()
{
errno_t err;
if( system(tailCmd) == -1) //if there is an error get errno
{
//Error calling tail.exe
_get_errno( &err );
}
}
First two compile errors:
error C2065: 'err' : undeclared identifier
error C2065: 'errno_t' : undeclared identifier
Not sure why as I am including the required and optional header files?
Any help is appreciated. Thank You.
A typical usage is like:
if (somecall() == -1) {
int errsv = errno;
printf("somecall() failed\n");
if (errsv == ...) { ... }
}
which is taken from here.
Just use 'errno' without any declaration. It is a macro that expands to an int value.
In the world of Standard C, the type 'errno_t' is defined by TR24731-1 (see Do you use the TR 24731 'safe' functions? for more information) and you have to 'activate it' by defining '__STDC_WANT_LIB_EXT1__'.
However, you appear to be working on Windows (judging from 'tail.exe', and also the non-standard '_get_errno()'). The rules there may depend on the C compiler you are using.
You should be able to chase down the information from this MSDN article on 'Security Enhancements in the CRT'. My impression was that it should be defined unless you actively suppress the feature, so check out whether you are actively suppressing it in your compilations.
Be aware that the MSVC definition of functions such as vsnprintf_s() do not match the TR24731-1 definitions:
MSDN:
int vsnprintf_s(
char *buffer,
size_t sizeOfBuffer,
size_t count,
const char *format,
va_list argptr
);
TR 24731-1:
int vsnprintf_s(
char * restrict s,
rsize_t n,
const char * restrict format,
va_list arg
);
The difference is not just a question of type aliases or qualifiers (rsize_t, restrict) - there are two sizes in the MS version and one in the Standard version. So much for standardization!