{"id":168,"date":"2023-06-14T14:29:05","date_gmt":"2023-06-14T13:29:05","guid":{"rendered":"https:\/\/mrzebra.co.uk\/code\/?p=168"},"modified":"2023-06-15T09:43:46","modified_gmt":"2023-06-15T08:43:46","slug":"building-redis-with-cmake","status":"publish","type":"post","link":"https:\/\/zebra-north.com\/code\/2023\/06\/14\/building-redis-with-cmake\/","title":{"rendered":"Building Redis++ with CMake"},"content":{"rendered":"\n<p><a href=\"https:\/\/github.com\/sewenew\/redis-plus-plus\">Redis++<\/a> is the recommended C++ binding for connecting to a Redis server from a C++ application, however its build script is not entirely friendly towards CMake.  It relies upon <a href=\"https:\/\/github.com\/redis\/hiredis\">hiredis<\/a>, but the make script expects it to already be compiled on the system.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>To compile hiredis as part of your own CMake script, add the following:<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-clike\"># Download Hiredis, upon which Redis Plus Plus depends.\nFetchContent_Declare(\n  hiredis\n  GIT_REPOSITORY https:\/\/github.com\/redis\/hiredis\n  GIT_TAG        v1.1.0\n  SOURCE_DIR     hiredis\n)\n\nFetchContent_MakeAvailable(hiredis)\n\n# Download the Redis binding.\nFetchContent_Declare(\n  redis_plus_plus\n  GIT_REPOSITORY https:\/\/github.com\/sewenew\/redis-plus-plus\n  GIT_TAG        1.3.8\n)\n\n# Pre-fill the include and library paths so that Redis++ does not search for them.\nset(CMAKE_INCLUDE_PATH &quot;${CMAKE_INCLUDE_PATH};${hiredis_SOURCE_DIR}&quot;)\nset(TEST_HIREDIS_LIB &quot;${hiredis_BINARY_DIR}\/libhiredisd.a&quot;)\nset(HIREDIS_LIB &quot;${hiredis_BINARY_DIR}\/libhiredisd.a&quot;)\n\nFetchContent_MakeAvailable(redis_plus_plus)\n\n# ... include your own source files here.\n\n# Add the Redis++ include directory.\ntarget_include_directories(myTarget PRIVATE &quot;${redis_plus_plus_SOURCE_DIR}\/src&quot;)\n\n# Link with Redis++ and Hiredis.\ntarget_link_libraries(myTarget PRIVATE redis++::redis++_static)\ntarget_link_libraries(myTarget PRIVATE hiredis::hiredis_static)<\/code><\/pre>\n\n\n\n<p>This operates as follows:<\/p>\n\n\n\n<p><code>FetchContent_Declare(hiredis ...)<\/code> creates the <code>hiredis<\/code> target, and then <code>FetchContent_MakeAvailable(hiredis)<\/code> causes the files to actually be downloaded.  The <code>SOURCE_DIR hiredis<\/code> option is required because Redis++ expects the include file to be in <code>&quot;hiredis\/hiredis.h&quot;<\/code>, and by default CMake will use the directory <code>hiredis-src<\/code> not <code>hiredis<\/code>.<\/p>\n\n\n\n<p><code>FetchContent_Declare(redis_plus_plus ...)<\/code>creates the <code>redis_plus_plus<\/code> target, however the CMake script for Redis++ assumes that <code>hiredis<\/code> has already been compiled and installed at this point, so it checks for the hiredis header file and library file, and fails if the do not exist.<\/p>\n\n\n\n<p>Because we are controlling the compilation of <code>hiredis<\/code>, we know the destination that the library will be built to, and so we can pre-fill the include and library paths, making Redis++ skip searching for them.<\/p>\n\n\n\n<p>The Redis++ CMake script can then be executed as normal.<\/p>\n\n\n\n<p>The final step is to link with the generated libraries.  For dynamic linking, simply remove the <code>_static<\/code> suffix and change the library name in <code>HREDIS_LIB<\/code> &amp; <code>TEST_HREDIS_LIB<\/code> to <code>libhiredisd.so<\/code>.<\/p>\n\n\n\n<p>Note that the Hiredis library must be added <strong>after<\/strong> the Redis++ library.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Redis++ is the recommended C++ binding for connecting to a Redis server from a C++ application, however its build script is not entirely friendly towards CMake. It relies upon hiredis, but the make script expects it to already be compiled on the system.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[28,27],"class_list":["post-168","post","type-post","status-publish","format-standard","hentry","category-cpp","tag-cmake","tag-redis"],"_links":{"self":[{"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/posts\/168","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/comments?post=168"}],"version-history":[{"count":5,"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/posts\/168\/revisions"}],"predecessor-version":[{"id":174,"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/posts\/168\/revisions\/174"}],"wp:attachment":[{"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/media?parent=168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/categories?post=168"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/tags?post=168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}