Skip to content

Conversation

@sjanel
Copy link
Contributor

@sjanel sjanel commented May 14, 2025

Configure hiredis features file before find package of hiredis to avoid configuration error.

Fix issues encountered by this PR, and this issue.

Here's how to use FetchContent with redis++ as a dependency with modern CMake (>= v3.24):

CMakeLists.txt

cmake_minimum_required(VERSION 3.24)
project(examples LANGUAGES CXX)

include(FetchContent)

FetchContent_Declare(
  hiredis
  GIT_REPOSITORY https://github.com/redis/hiredis
  GIT_TAG origin/master
  GIT_SHALLOW TRUE
  SOURCE_DIR _deps/hiredis
  # Make sure redis++ finds fetched content with its find_package()
  OVERRIDE_FIND_PACKAGE
)

FetchContent_Declare(
  redis-plus-plus
  GIT_REPOSITORY https://github.com/sjanel/redis-plus-plus.git
  GIT_TAG bugfix/hiredis-fetch-configure-file-found
  GIT_SHALLOW TRUE
)
FetchContent_MakeAvailable(redis-plus-plus)


add_executable(main
  main.cc
)

target_link_libraries(main
  redis++
)

main.cc

#include <sw/redis++/redis++.h>
#include <iostream>

int main() {
  auto redis = sw::redis::Redis("tcp://127.0.0.1:6379");
  redis.set("key", "value");
  auto val = redis.get("key");
  if (val) {
    std::cout << "Got: " << *val << std::endl;
  }
  return 0;
}

(adapted from this gist)

@sjanel sjanel force-pushed the bugfix/hiredis-fetch-configure-file-found branch 6 times, most recently from 75fcea5 to 8135fa5 Compare May 14, 2025 09:58
@sjanel sjanel force-pushed the bugfix/hiredis-fetch-configure-file-found branch from 8135fa5 to b961816 Compare May 14, 2025 10:10
@sjanel sjanel changed the title [BugFix] - Allow hiredis to be retrieved automatically by FetchContent Allow hiredis to be retrieved automatically by FetchContent May 14, 2025
@juwalter
Copy link

just tried this in our project and configure and build are successful. big help and very nice work!!

@sewenew
Copy link
Owner

sewenew commented May 19, 2025

Great job! Thanks a lot!

The only problem is that this solution requires minimum version of cmake is 3.24. Is it possible to make the CMakeLists.txt also works with older version cmake for those who don't require the FetchContent feature? I mean, if they want to use FetchContent feature, the upgrade cmake to 3.24, otherwise, they can use an older version to build.

Regards

@sjanel
Copy link
Contributor Author

sjanel commented May 19, 2025

Hello @sewenew !

Indeed it requires CMake 3.24 because of OVERRIDE_FIND_PACKAGE, but only for the consumer of the library, not for redis-plus-plus itself. Unfortunately, I don't know how to make this work without OVERRIDE_FIND_PACKAGE without a full rewrite of the CMakeLists.txt.

@sewenew sewenew merged commit 3a5d451 into sewenew:master Jun 11, 2025
3 checks passed
@sewenew
Copy link
Owner

sewenew commented Jun 11, 2025

@sjanel Sorry, but I misunderstood the solution. User with older version cmake, can still build redis-plus-plus the old way.

Thanks a lot for your contribution!

Regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants