sewenew / redis-plus-plus

Redis client written in C++
Apache License 2.0
1.64k stars 351 forks source link

Redis++ connection returns NULL on Windows #491

Closed andrewjohnstonn closed 1 year ago

andrewjohnstonn commented 1 year ago

I have been trying to build a Redis++ application on Windows but I am still unable to establish a connection with my Redis-server. I'm wondering if anyone has successfully built a CMake application on Windows as I have a feeling it is something in my CMake that is wrong as the provided test passes everything. I have included my CMake below.

cmake_minimum_required(VERSION 3.16.4)

project(RedisProject)

set(REDIS_PLUS_PLUS_CXX_STANDARD=17)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(HIREDIS_HEADER "${CMAKE_CURRENT_SOURCE_DIR}/../")
set(HIREDIS_LIB "${CMAKE_CURRENT_SOURCE_DIR}/../hiredis/build/Debug/hiredisd.lib")
set(REDIS_PLUS_PLUS_HEADER "${CMAKE_CURRENT_SOURCE_DIR}/../redis-plus-plus/src")
set(REDIS_PLUS_PLUS_LIB "${CMAKE_CURRENT_SOURCE_DIR}/../redis-plus-plus/build/Debug/redis++_static.lib")

file(GLOB sources CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/*h ${CMAKE_CURRENT_SOURCE_DIR}/*cpp)

add_executable(redis ${sources})

target_include_directories(redis PUBLIC "${CMAKE_SOURCE_DIR}/../redis-plus-plus/src/sw/redis++/cxx17")
target_include_directories(redis PUBLIC "${CMAKE_SOURCE_DIR}/../redis-plus-plus/src/sw/redis++/tls")

find_path(HIREDIS_HEADER hiredis)
target_include_directories(redis PUBLIC ${HIREDIS_HEADER})

find_library(HIREDIS_LIB hiredis)
target_link_libraries(redis ${HIREDIS_LIB})

find_path(REDIS_PLUS_PLUS_HEADER sw)
target_include_directories(redis PUBLIC ${REDIS_PLUS_PLUS_HEADER})

find_library(REDIS_PLUS_PLUS_LIB redis++_static)
target_link_libraries(redis ${REDIS_PLUS_PLUS_LIB})

Environment:

andrewjohnstonn commented 1 year ago

I'll also note my cpp program is literally just:

auto client = sw::redis::Redis("tcp://host:6379")

std::cout << client.ping();
sewenew commented 1 year ago

It seems that you didn't finish the install steps, but built with redis-plus-plus' source code? That should not work.

Instead, you should build redis-plus-plus with cmake, and run make install to install headers and libs. The installation process will do some critical stuff to ensure related headers are installed correctly.

NOTE: redis-plus-plus does not return NULL pointer, it throws exception when error happens.

Regards

andrewjohnstonn commented 1 year ago

No make file is generated on Windows when I cmake .. in the build folder

andrewjohnstonn commented 1 year ago

I also get an error when I run make in the hiredis folder on Windows:

$ make Makefile:102: Extraneous text after `else' directive gcc -std=c99 -c -O3 -fPIC -Wall -W -Wstrict-prototypes -Wwrite-strings -Wno-missing-field-initializers -g -ggdb -pedantic alloc.c alloc.c:1:0: warning: -fPIC ignored for target (all code is position independent) [enabled by default] alloc.c:40:17: error: 'strdup' undeclared here (not in a function) make: *** [alloc.o] Error 1

sewenew commented 1 year ago

If you are using vs2017 or later, you can use the ’open folder’ feature to build both hiredis and redis-plus-plus with buitin cmake support.

Also you can install redis-plus-plus with vcpkg.

Sorry, but I’m not familiar with windows env. If you have problem with building hiredis on windows, you can seek help from hiredis community.

Regards


发件人: Andrew Johnston @.> 发送时间: Tuesday, May 30, 2023 8:07:22 PM 收件人: sewenew/redis-plus-plus @.> 抄送: sewenew @.>; Comment @.> 主题: Re: [sewenew/redis-plus-plus] Redis++ connection returns NULL on Windows (Issue #491)

I also get an error when I run make in the hiredis folder on Windows:

$ make Makefile:102: extraneous text after 'else' directive gcc -std=c99 -c -O3 -fPIC -Wall -W -Wstrict-prototypes -Wwrite-strings -Wno-missing-field-initializers -g -ggdb -pedantic alloc.c process_begin: CreateProcess(NULL, gcc -std=c99 -c -O3 -fPIC -Wall -W -Wstrict-prototypes -Wwrite-strings -Wno-missing-field-initializers -g -ggdb -pedantic alloc.c, ...) failed. make (e=2): The system cannot find the file specified. make: *** [Makefile:270: alloc.o] Error 2

― Reply to this email directly, view it on GitHubhttps://github.com/sewenew/redis-plus-plus/issues/491#issuecomment-1568319645, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ACWWTAPEOQDKEQECNNFUMOLXIXPHVANCNFSM6AAAAAAYTYR5LA. You are receiving this because you commented.Message ID: @.***>

andrewjohnstonn commented 1 year ago

Hi,

Got it working, you were right I just had to build using Visual Studio I was building manually on VS Code.

Cheers!