There are a few ways to install Redis, I downloaded it from the official site, redis.io, following the instructions you could find there. In a matter of minutes I had a plain Redis server up and running and I checked it through redis-cli (Redis command line interface utility).
There are plenty of available Redis clients for different programming languages and you typically want to get an existing one matching your elected language. But in C++ case, I see only one alternative in the list proposed by Redis, and there are a few reasons not to peek it up. The code in the repository is quite old (2/3 years), and in the list it is not marked as recommended. So I fell back to hiredis, the official C client, included in the distribution (you could find it under deps/hiredis).
All I need is at hand, I just have to write a Makefile (you can download it from github), and then I am ready to write some code:
# # Makefile for hiredis client # MY_NAME := hrc MY_SRCS := $(wildcard *.cpp) MY_OBJS := ${MY_SRCS:.cpp=.o} MY_INCLUDE_DIRS := /usr/local/include/hiredis MY_LIBRARY_DIRS := /usr/local/lib MY_LIBRARIES := hiredis CXXFLAGS += $(foreach includedir,$(MY_INCLUDE_DIRS),-I$(includedir)) CXXFLAGS += -Wall -g -std=c++11 LDFLAGS += $(foreach librarydir,$(MY_LIBRARY_DIRS),-L$(librarydir)) LDLIBS += $(foreach library,$(MY_LIBRARIES),-l$(library)) .PHONY: all clean all: $(MY_NAME) $(MY_NAME): $(MY_OBJS) $(LINK.cc) -o $(MY_NAME) $(MY_OBJS) $(LDLIBS) clean: @- rm -rf $(MY_OBJS) $(MY_NAME)My client is going to be named hrc (as MY_NAME shows), it is written in C++, and the code is contained in .cpp files (MY_SRCS). Any cpp file would have a matching object file identified by the "o" extension (MY_OBJS).
The only custom directory I want to include now is the one for the hiredis client (MY_INCLUDE_DIRS), and I want the make tool to check just in one custom directory for non standard library (MY_LIBRARY_DIRS), and the only custom library I currently want (MY_LIBRARIES) is hiredis. You would probably ask makefile to look in other directories. Remember that the actual library names are decorated versions for the bare name you should put in the makefile. In this case, Redis compilation has generated an archive named libhiredis.a, for static linkage, and a shareable object named libhiredis.so.
Besides, the linker would search for the actual versioned file, as maintained by ldd. In the worst case scenario, it could happens that the linker would complain for missing a versioned so. You could fix this by ldconfig, or even by hand, creating a reference to the missing file through symlink.
I add to the CXXFLAGS firstly the dependencies for include directories and then a bunch of option - I want all the warnings up (-Wall), the executable filled with debug information (-g), and the code generated to support the latest C++ standard (-std=c++11).
In the LDFLAGS I add the dependencies for library directories, and in LDLIBS the actual requested library names.
An then I defines a few targets. A couple of phony ones, "all" and "clean", as tradition wants, and the one that actually creates my target.
Notice that I don't specify explicitly the name of the C++ compiler, but I rely on the make tool to be smart enough to deduce it from the environment. For this reason I use LINK.cc, that in my case contains the expected g++.
In the next post, I am going to write some C++ code meant to be compiled with this Makefile.
No comments:
Post a Comment