Hi all,
I'm in the process of building Hiredis, the official C client library for Redis (Windows port of course) using C++ Builder.
Windows port of Redis is a fork maintained by Microsoft Open Tech community and is of course tight to MS compiler and libraries. In fact, there is no luck in trying to compile the source just "out of the box" as Visual Studio does, even if the source is straight standard C (well, at last the original Redis code).
The source can be found here: Windows port of Redis
I know there is a Delphi client available, but it really sounds sad (and frustrating) to me having to import the Delphi code because the "native C source" cannot be used. No way. That's why I decided to give it a try.
To compare sources an libraries involved, I'm using C++ Builder 10.1.2 (Berlin Update 2) on one side and Visual Studio Community 2017 on the other side. The basic steps using C++ Builder are:
(Edit: I edited the post and removed all code formatting features because I had problems with it. Also, for the same reason all #include in the post has standard parenthesis instead of "minor and major" chars. Sorry for that.)
- Download of source version redis-win-3.2.100
- Create a new Static Library project called Hiredis and add some files contained in the "redis-win-3.2.100\deps\hiredis" subfolder: from the deps\hiredis folder:
- async.c
- hiredis.c
- net.c
- sds.c
(from src\Win32_Interop folder):
- Win32_ANSI.c
- Win32_Common.cpp
- Win32_Error.c
- Win32_EventLog.cpp
- Win32_FDAPI.cpp
- Win32_fdapi_crt.cpp
- Win32_RedisLog.c
- Win32_rfdmap.cpp
- Win32_Time.c
- Win32_VariadicFunctor.cpp
- Setup the project to use Clang based compiler (not really needed as this should be standard C code, but see later)
- Add project conditional defines:
- _STDC__=1 (required to cut off (re)definition of “off_t” inside stdio.h)
- __SSIZE_T_DEFINED=1 (required to cut off (re)definition of type ssize_t inside _stddef.h)
- _OFF_T_DEFINED (required to cut off (re)definition of type off_t inside sys\types.h; needs C Standard Header modification, see below.)
- Build
The project fails to compile as I said, here is a step by step description of the issues and fixes I used. After each fix I usually compile the single unit or directly build the whole project. Each fix provided must not break the original source which still compiles using MSVC, this will be eventually a requirement when talking about pushing the code to the MSOpen Tech community.
- [bcc32c Error] Win32_Error.h(37): unknown type name 'size_t'
Fix: added a conditional block and the relative include to the header that contains that type.
#ifdef __BORLANDC__
#include (stdlib.h)
#endif
This error is due to the fact that inside Win32_Error.h there is another include and this header in MS C Runtime Library version contains another include to a Visual C header. (Kudos to Embarcadero C Runtime Library version)
- [bcc32c Error] stdio.h(56): typedef redefinition with different types ('long' vs 'long long')
This error is due to the re-definition of the type off_t inside the file win32_types.h provided in the hiredis source.
Now, according to the info in "win32_types.h" they need to re-define both off_t
and _off_t types
to match Posix version of Redis.
But, in order to use this definition without conflicts you need to include this header before any other inclusion of the original header "sys\types.h", which contains the original types definitions under a conditional define (_OFF_T_DEFINED).
Win32_types.h will also define _OFF_T_DEFINED to avoid subsequent types re-definitions. (anyway MSVC has an additional conditional define _OFF_T_DEFINED at project level.)
Now, on MS Visual Studio C Runtime Library, "stdio.h" does not contain "off_t" type definition, that's why it everything works. Moreover, Embarcadero C Runtime Library has no conditional defs to skip off_t and _off_t definitions inside "sys\types.h" as MS does.
First Fix: I'd rather not modify C Runtime Library headers at all, but at the moment I have no other options here than modifying Embarcadero C Runtime Library header file "sys\types.h" and wrap the off_t type definition with the above mentioned _OFF_T_DEFINED, just as in MS Visual Studion C Runtime headers:
#ifndef _OFF_T_DEFINED
#define _OFF_T_DEFINED
typedef long off_t;
#endif
-
[bcc32c Error] Win32_variadicFunctor.cpp(61) expected '(' after 'for'
Fix: replace MS style “for each” with standard C++11 “for ( range_declaration : range_expression )”
-
[bcc32c Error] Win32_fdapi_crt.cpp(35): use of undeclared identifier '_pipe'
Fix 1: add #include
Fix 2: replace functions _setmode(), _isatty() with setmode() and isatty()
-
Win32_time.c/.h errors
Fix 1: move struct timezone definition from .c to .h
Fix 2: include time.h in Win32_time.h
- [CLANG Error] sds.h(53): expected identifier
The file contains an helper macro to pack structs. The macro uses __pragma() compiler extension to allow the usage of pragma "in-a-macro". According to the compiler documentation, also thanks to this answer on StackOverflow from Remy Lebeau, old BCC32 does not support this statements. But Clang based compilers do. This is the first fix that is tight to Clang compiler, even if we could build a different solution by using #pragma as stated in stakoverflow post by Remy to have BCC32 working.
Anyway, I was not able to make __pragma() work that way, so I created another macro using _Pragma() instead and that worked fine.
#if defined(__BORLANDC__) && defined(__clang__)
#define PACK( __Declaration__ ) _Pragma( "pack(push, 1)" ) _Declaration__ _Pragma( "pack(pop)" )
#else
#define PACK( __Declaration__ ) __pragma( pack(push, 1) ) __Declaration__ __pragma( pack(pop) )
#endif
- [CLANG Error] hiredis.c(1041): conflicting types for 'redisConnectWithTimeout'
hiredis.h(184): previous declaration is here
(unresolved)
As sometime happens the compiler message is not really helpful. In fact the problem is not 'redisConnectWithTimeout' function prototype but rather one of the parameter types used in it, timeval which is defined in "winsock2.h".
For some strange reason, on MSVC this type definition is known in hiredis.h at function declaration, even if winsock2.h is not included by any of the include entries, nor in any sub-includes. Unfortunately, any inclusion of winsock2.h leads to an incredible amount of other errors which make me think about searching for another solution.
- [bcc32c Error] types.h(65): expected unqualified-id (unresolved)
- [bcc32c Error] types.h(69): no member named 'off_t' in namespace 'std' (unresolved)
At last the second error comes from the conditional define to wrap off_t.
Here is where I lay by now, welcome contributions from anyone can help.
Thanks.
Alex B.