C++ Redis hiredis compiler error? - c++

Thanks to someone on this site, I was able to use a sample C++ source file.
https://gist.github.com/1893378
When I compile this, I get a bunch of strange compiler errors:
/home/.../workspace/redis-hiredis-3c46b13/pipelineTest.cpp: In function ‘void redisTop(redisContext*)’:
/home/.../workspace/redis-hiredis-3c46b13/pipelineTest.cpp:142:32: error: invalid conversion from ‘void*’ to ‘redisReply*’
/home/../workspace/redis-hiredis-3c46b13/pipelineTest.cpp:162:25: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long int’
/home/=.../workspace/redis-hiredis-3c46b13/pipelineTest.cpp: In function ‘int main(int, char**)’:
I realize I may be out of synch with hiredis libray but I am using the latest one. Any ideas how to properly build this C++ program?
Also, I am looking for a complete C++ tutorial of pipeline with Redis. Here is a really good example in Java:
http://www.cafebabe.me/2011/05/redis-pipelines-and-transactions.html
Is there no example tutorial like this for C++?
Thanks either way

Actually, it is a C program, not C++. The C++ compiler is usually more pedantic that a C compiler, and it complains. Minor adaptations are needed to compile with a C++ compiler.
I did them, and put the file online again:
https://gist.github.com/1893378
It now compiles fine with g++ 3.4 and 4.3
AFAIK, there is no C++ tutorial for Redis. I think the best way to deal with Redis in C++ is to develop your own wrapping classes on top of hiredis. It is not that hard.

Just cast redisCommand to redisReply
reply = (redisReply *)redisCommand(context, "PING");

This is a C program. You have to build it with a C compiler.

Related

Adding C++ code in C program - expected ‘,’ or ‘...’ before ‘this’

I am building a C program with no problem on CentOS using provided Makefiles in its distro package. However I need to modify source files and I need to use iqxmlrpc and boost libraries for that purpose. I have changed Makefiles respectively but when I want to build the package I get following error:
../src/libiqxmlrpc/libiqxmlrpc/except.h:9:21: error: stdexcept: No such file or directory
../src/libiqxmlrpc/libiqxmlrpc/except.h:10:18: error: string: No such file or directory
In file included from ../src/libiqxmlrpc/libiqxmlrpc/libiqxmlrpc.h:17,
from redir.c:2617:
../src/libiqxmlrpc/libiqxmlrpc/except.h:14: error: expected '=', ',', ';', 'asm' or 'attribute' before 'iqxmlrpc'
... followed by many other errors ...
I believe above error is because iqxmlpc is written in c++ and gcc is treating it as C so I tried followings:
1) Use g++
2) Use gcc -x c++
Doing above, error changes to a lot of following in many header files:
error: expected ‘,’ or ‘...’ before ‘this’
All the lines that compiler is complaining somehow have this or delete keywords as parameter. See below:
int net_route(struct in_addr *dst, int delete);
or
extern int gad_new(struct gad_t **this)
Do you have any idea how can I fix this? Frankly, I have no idea what using 'this' or 'delete' mean as parameters. Are these only parameter names that gcc ignores the rule that we can not use keywords?
------------------------------
BTW, original programmer have used nested functions all across his code which were compiled fine using gcc. when I switch to g++, compiler complains about function-definition not being allowed in another function. Is there any way I can tell g++ to ignore this error?
Those weren't keywords in C, they are in C++.
You will have to rename those parameters before the code will compile in C++.
You may run into other porting issues related to C++'s stricter type checking before you are done.
Another option is to keep the C and C++ code in separate files, and use extern "C" in the C++ code for every function call that must cross the boundary. Since your C code extensively uses nested functions and other features not permitted in C++, this is probably your quickest approach.
this and delete are reserved keywords in C++ (not in C though) . You cannot use these as variable names when compiling with g++.
Its better to use different variable names in parameters and then compile with g++.

Error: Assigning to char* from incompatible type void*

So I'm trying to store a response from a libcURL HTTP request into a C-string to be parsed later on. The response code is written entirely in C, while everything else is in C++, and with any other C++ compiler, it should work fine. But when I try to compile, even if I give the '-x c' arguments followed by the filename, I get these specific responses.
g++ main.cpp -x c cJSON.c -x c respbuffer.c -lcurl -lm
./respbuffer.c:14:9: error: assigning to 'char *' from incompatible type 'void *'
s->ptr = malloc(s->len+1);
^ ~~~~~~~~~~~~~~~~
./respbuffer.c:23:9: error: assigning to 'char *' from incompatible type 'void *'
s->ptr = realloc(s->ptr, new_len+1);
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~
Weirdly enough, this error only pops up when compiling with g++. If I use gcc, it works fine, and it runs smoothly enough. For the curious, I wrote everything in Xcode, and I'm compiling with GCC 4.2.1.
This happens because C is pretty laxed with types, while C++ is strict. malloc() returns void*, so in order to use it as char* you need to cast it.
You should not use a C++ compiler to compile C code. The two languages are different, and code which is correct in C may silently misbehave in C++.
Adding a cast to your code masks the problem; even if it appears to work for now, you really have no idea what else might be happening.
On my system, -x c causes g++ to actually invoke the C compiler. Perhaps you have some version of g++ that does not support that switch, or perhaps g++ is an alias for some other compiler on your system.
If you cannot get -x c to work, then use gcc as the compiler instead of g++ . (You will need to use separate invocations for the C files than for the C++ files, followed by a link step).
In C the malloc function return a void* of a memory block on the heap. In C it's everything ok because a void* is implicitly casted to a char*.
But in C++ it's forbidden, so you have to explicitly cast it.
So in this case the right code is:
s->ptr = static_cast<char*>(malloc(s->len+1));

Compile in c++14

So in my CSE course we are given a header file to use right now for our programs that we're writing.
Unfortunately I can't get terminal to compile using that header, it gives quite a few errors (compiling with just 'g++'). Also, when I'm at my university and I'm using PuTTY I get the same errors while using this header. However, I don't get the errors when I compile with 'g++ -std=c++14'.
I've tried compiling with this command on terminal on my mac, but it says it doesn't recognize the c++14 part.
dhcp-10-202-147-243:hw1pr1 Admin$ g++ -std=c++14 hw1pr1.cpp
error: invalid value 'c++14' in '-std=c++14'
Any help on how I could get this to work would be greatly appreciated. Hopefully this all made some sort of sense.
Here's the error I get when I compile with the header file I'm talking about in terminal with just g++.
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/ext/hash_map:212:5: warning:
Use of the header <ext/hash_map> is deprecated. Migrate to <unordered_map>
[-W#warnings]
# warning Use of the header <ext/hash_map> is deprecated. Migrate to ...
^
In file included from read_first_name.cpp:1:
./std_lib_facilities_4.h:43:20: error: no matching function for call to object
of type 'hash<char *>'
return hash<char*>()(s.c_str());
^~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/ext/__hash:39:12: note:
candidate function not viable: 1st argument ('const value_type *'
(aka 'const char *')) would lose const qualifier
size_t operator()(char *__c) const _NOEXCEPT
^
In file included from read_first_name.cpp:1:
./std_lib_facilities_4.h:112:8: warning: comparison of unsigned expression < 0
is always false [-Wtautological-compare]
if (i<0||size()<=i) throw Range_error(i);
~^~
./std_lib_facilities_4.h:118:8: warning: comparison of unsigned expression < 0
is always false [-Wtautological-compare]
if (i<0||size()<=i) throw Range_error(i);
~^~
3 warnings and 1 error generated.
This error doesn't happen and the program will compile fully when I use PuTTY and 'g++ std=c++14'
There's lots of change between C++ standards, so what is valid in one revision need not be in another.
g++ defaults to -std=gnu++98 for C++, which is the decades old C++98-standard enhanced with GNU extensions (most of which are conformant).
Choose the proper revision: -std=c++1y -pedantic is a very close approximation to C++14.
What changes introduced in C++14 can potentially break a program written in C++11?
Looking at what you say you're having to use and the name format of that .cpp file, I think I'm in the same class. A year later, looks like, but here's my solution for archive's sake:
The std_lib_facilities.h header comes with the Bjarne Stroustrup textbook, "Programming: Principles and Practices Using C++". For those unaware, Bjarne Stroustrup invented C++ (he has a pretty good idea what he's talking about). Incidentally, the book is a fantastic way to learn C++, if one takes the time to actually read it. The std_lib_facilities.h header is just a convenient header file for beginners in C++, containing links to all the major standard libraries used in the textbook, as well as some helper functions that help account for potential mistakes or errors, or are just convenient for learning (such as an error() function that handles simple exception throwing for the student, or adding an "out of bounds" check for vectors). It's ultimately just a way to allow students to hop right into code without having to learn specifics about the header.
Stroustrup keeps updated with C++ and thus includes several libraries that require the c++11 standard. The CSCE department wants its students (at least in this early class) to connect to the department's Unix system and compile from there, in order to avoid confusion with downloading and updating compilers.
I happened to already have had a couple C++ classes beforehand, and thus already had g++ set up on my Ubuntu laptop. I avoided including the std_lib_facilities for as long as possible since I was getting the same error as Topic Creator Joe, where g++ didn't recognize the "c++11" part (manually including the required libraries worked fine until we had to use a class from the textbook that used one of the header's helper functions) . Eventually, I found a help topic online that advised me simply to update my g++ compiler to 4.7 or higher, since 4.6 and lower doesn't have support for C++11 (or, of course, C++14). It was oddly rather involved compared to updates one might be used to on Mac or Windows, and I doubt the exact process would apply, but that is (was?) likely the problem: it's just an older version of g++, and it needs an update to compile C++11 and later. I recommend searching for ways to update g++/gcc for Mac.
Should, y'know, anyone else with this problem stumble upon this and not have their problem solved yet.

types may not be defined in 'sizeof' expressions in module_param

I am writing a bluetooth driver for debian based linux. I have used module_param to send command line argument to a module. It takes 3 arguments. First is the variable to store the argument value, second is the type of the argument and third is the permission.
my code looks like this....
#include <linux/module.h>
bool x =1;
module_param(x,bool,0000);
one more thing is I am compiling the module using g++ and I know people write module in C.
the error I am getting is like this.
test.cc:10:error: types may not be defined in 'sizeof' expressions
Please post if you have an answer for it.
There was some problem with verfiy.h file with c++. They have fixed it in latest g++ compiler (g++ 3.2.2 and 4.0.1), try downloading the latest g++ and compile. Please go through this link http://www.mail-archive.com/bug-gnulib#gnu.org/msg01306.html. Hope this answers your question :-).

Adobe Alchemy compiled SWC - not sure how to compile library for FlasCC

I'm trying to get game-music-emu-flash working with FlasCC. It uses the C/C++ Game_Music_Emu library and was originally compiled with Alchemy, but I want to see if there is a performance increase with FlasCC. I'd like to use SWIG with typemaps to call the library since the current project ActionScript is not compatible with FlasCC. I'm following code from the samples. I haven't coded the typemaps yet but for now I created swig.i as
#ifdef SWIG
%module libgmeLibModule
%{
#include "gme/gme.h"
%}
%include "gme/gme.h"
#else
#include "gme/gme.h"
#endif
and the Makefile follows the samples closely by replacing what's needed to match. During compilation I'm getting similar errors all in the form
libgmeLib_wrapper.cpp: In function `void _wrap_gme_ay_type_get()':
libgmeLib_wrapper.cpp:2575: error: invalid conversion from `const void*' to `void*'
libgmeLib_wrapper.cpp:2575: error: initializing argument 1 of `void* memcpy(void*, const void*, size_t)'
Compiling with
"$(FLASCC)/usr/bin/g++" $(BASE_CFLAGS) -04 gmemain.cpp gme/*.cpp -emit-swc=sample.libgme -o libgme.swc
in the Makefile by itself under all: compiles without errors.
I don't know C++ that well so any help is appreciated. Also would using SWIG and typemaps be the way to go or would manually wrapping make sense? I thought SWIG would be better since gme is a decent sized library.
An Adobe staffer helped me out with this. He even coded a demo.
The Adobe forum thread can be found here and his demo can be found at gme-flascc