Giter Club home page Giter Club logo

Comments (24)

sewenew avatar sewenew commented on May 20, 2024 3

Hi @afifzaki

If you're using CLion, you should modify the related CMakeLists.txt file to add hiredis and redis-plus-plus dependencies, and reload it.

The CMakeLists.txt should looks like the following:

cmake_minimum_required(VERSION 3.0.0)

project(app)

set(CMAKE_CXX_FLAGS "-std=c++11 -Wall -W -Werror -fPIC")

set(SOURCE_FILES app.cpp)
add_executable(app ${SOURCE_FILES})

# <------------ add hiredis dependency here --------------->
find_path(HIREDIS_HEADER hiredis)
target_include_directories(app PUBLIC ${HIREDIS_HEADER})

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

# <------------ add redis-plus-plus dependency here -------------->
find_path(REDIS_PLUS_PLUS_HEADER sw)      # NOTE: this should be *sw* NOT *redis++*
target_include_directories(app PUBLIC ${REDIS_PLUS_PLUS_HEADER})

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

If you installed hiredis and redis-plus-plus at the default location, you can compile it without any other cmake options. However, if you installed these libraries at non-default location, you should also specify the -DCMAKE_PREFIX_PATH cmake option to specify the path where you installed these libraries. See this for how to set cmake option with CLion.

If you're using code block, this might be helpful for how to add dependencies.

Regards

from redis-plus-plus.

afifzaki avatar afifzaki commented on May 20, 2024 1

Hi,
Thank you very much, the code is perfectly worked in CLion.
I think I won't close this issue because this is very helpful for others who have the same problem.

Good work, keep it up.

from redis-plus-plus.

sewenew avatar sewenew commented on May 20, 2024

Hi @afifzaki

Your code is the simplest way to initializing a connection to Redis.

The problem is that you didn't link hiredis library. redis-plus-plus is a wrapper based on hiredis lib, so you need to install hiredis, and link it.

g++ -std=c++11 -o app app.cpp -lhiredis -lredis++ -pthread

See how to install hiredis for details.

If you still have any problem with redis-plus-plus, feel free to let me know.

Regards

from redis-plus-plus.

sewenew avatar sewenew commented on May 20, 2024

Hi @afifzaki

I updated the above comment. If you are using GCC, you need to specify -lhiredis and -lredis++ after app.cpp, NOT before it. But if you are using CLANG the order is NOT a problem. I'll update the doc to make it more generic. Thanks for finding the doc bug :)

g++ -std=c++11 -o app app.cpp -lhiredis -lredis++ -pthread

Also, if you link the dynamic libraries, when you running your application, you might get the following error:

error while loading shared libraries: xxx: cannot open shared object file: No such file or directory.

Check this for solution.

Regards

from redis-plus-plus.

afifzaki avatar afifzaki commented on May 20, 2024

Hi,
Thanks for your solution, the code successfully compiles if you're compiling that with your terminal (I'm using ubuntu 18.04). But the problem arose if you try compiling it inside IDE (code block, CLion).

I'm already following every instruction you made (the installation instruction is very clear). Why this is happening?

from redis-plus-plus.

sewenew avatar sewenew commented on May 20, 2024

I'm glad that you like redis++. I'll add some instruction on building application with cmake in the documentation.

Thank you again for finding the documentation bug :)

Regards

from redis-plus-plus.

yitian108 avatar yitian108 commented on May 20, 2024

hi @sewenew @afifzaki ,
I still encounter this issue, when I write the cmakelists.txt as below, the clion debug report can't find lredis++:

`cmake_minimum_required(VERSION 3.12)
project(redis)

set(CMAKE_CXX_STANDARD 14)

add_executable(${PROJECT_NAME} main.cpp )

find_path(HIREDIS_INC_DIR hiredis)
target_include_directories(${PROJECT_NAME} PUBLIC $ENV{HIREDIS_INC_DIR})

find_library(HIREDIS_LIB_DIR hiredis)
target_link_libraries(${PROJECT_NAME} hiredis)

find_path(REDIS_PLUS_PLUS_INC_DIR sw)
target_include_directories(${PROJECT_NAME} PUBLIC $ENV{REDIS_PLUS_PLUS_INC_DIR})

find_library(REDIS_PLUS_PLUS_LIB_DIR redis++)
target_link_libraries(${PROJECT_NAME} redis++)`

I have troubled with this problem two days, but when I use the command line , it works correctly:

g++ -std=c++11 -I/usr/local/app/redis-plus-plus/include -o redis main.cpp -L/usr/local/app/hiredis/lib -lhiredis -L/usr/local/app/redis-plus-plus/lib -lredis++ -pthread

ps: $ENV{REDIS_PLUS_PLUS_INC_DIR} = /usr/local/app/redis-plus-plus/include
$ENV{REDIS_PLUS_PLUS_LIB_DIR} = /usr/local/app/redis-plus-plus/lib
$ENV{HIREDIS_LIB_DIR} = /usr/local/app/hiredis/lib

Thanks

from redis-plus-plus.

sewenew avatar sewenew commented on May 20, 2024

@yitian108 Your CMakeLists.txt is incorrect. find_path and find_library will try to find the header and lib path, and save it into the variables you specified. So when you try to call target_include_directories and target_link_libraries, you should use these variables as parameters:

find_path(HIREDIS_INC_DIR hiredis)
target_include_directories(${PROJECT_NAME} PUBLIC ${HIREDIS_INC_DIR})

find_library(HIREDIS_LIB_DIR hiredis)
target_link_libraries(${PROJECT_NAME} ${HIREDIS_LIB_DIR})

find_path(REDIS_PLUS_PLUS_INC_DIR sw)
target_include_directories(${PROJECT_NAME} PUBLIC ${REDIS_PLUS_PLUS_INC_DIR})

find_library(REDIS_PLUS_PLUS_LIB_DIR redis++)
target_link_libraries(${PROJECT_NAME} ${REDIS_PLUS_PLUS_LIB_DIR})

Also, instead of set the environment variable, you should use -DCMAKE_PREFIX_PATH command line arguments to specify the installation path of hiredis and redis++ when running cmake. So that find_path and find_library will correctly find the location and set those variables.

Please check cmake's doc for more details on how to add dependency for a cmake project.

Regards

from redis-plus-plus.

yitian108 avatar yitian108 commented on May 20, 2024

Hi @sewenew ,thank you for replying this, I set these environment variables as below, and it real works good in the command line:
$ENV{REDIS_PLUS_PLUS_INC_DIR} = /usr/local/app/redis-plus-plus/include
$ENV{REDIS_PLUS_PLUS_LIB_DIR} = /usr/local/app/redis-plus-plus/lib
$ENV{HIREDIS_LIB_DIR} = /usr/local/app/hiredis/lib

also , I run cmake, I used the options -DCMAKE_PREFIX_PATH -DCMAKE_INSTALL_PREFIX etc, please see below:

cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=/usr/local/app/hiredis -DCMAKE_INSTALL_PREFIX=/usr/local/app/redis-plus-plus ..

that's I really don't know why it is works good with hiredis and in command line, only does not work with redis++

Regards

from redis-plus-plus.

yitian108 avatar yitian108 commented on May 20, 2024


IMG_7352

from redis-plus-plus.

yitian108 avatar yitian108 commented on May 20, 2024

I use the simple cmakelists.txt as attachment, and message() them in console, the result is the same, can't find the library redis++, I don't know whether it related with the version of clion. I think it should not.

from redis-plus-plus.

sewenew avatar sewenew commented on May 20, 2024

@yitian108 Since you install hiredis and redis-plus-plus in different paths. You should specify both path to -DCMAKE_PREFIX_PATH, and separate them with ;. Also, in your case, if you don't need to install the binary to some where, you don't need to use the -DCMAKE_INSTALL_PATH.

cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=/usr/local/app/hiredis;/usr/local/app/redis-plus-plus ..

Again, these environment variable has nothing to do with cmake building.

Regards

from redis-plus-plus.

yitian108 avatar yitian108 commented on May 20, 2024

Hi @sewenew , thanks for your suggestion, I found some interesting things while I reinstall the library redis++, if I use the command as you involved above, the console will report there is no the path of CMakelists.txt,
image
so, I separate them with another option DCMAKE_PREFIX_PATH, like following command, it will work:
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=/usr/local/app/hiredis -DCMAKE_PREFIX_PATH=/usr/local/app/redis-plus-plus ..
however, funny, the app will installed in default directory "/usr/local/include/sw", and the directory /usr/local/app/redis-plus-plus is empty.

last, if the app installed in default directory, the code can be built successfully in Clion, but run failed, it reported:

/home/yh/dev/cpp/redis/cmake-build-debug/redis: error while loading shared libraries: libredis++.so: cannot open shared object file: No such file or directory

let me check the others of the cmakelists.txt again.

ps: I want to know how the cmake can identify correctly about the variables HIREDIS_HEADER, REDIS_PLUS_PLUS_HEADER, REDIS_PLUS_PLUS_LIB, etc. Does it mean I should set them first before using, some like:

set (HIREDIS_HEADER /usr/local/app/hiredis/include/)

Regards

from redis-plus-plus.

sewenew avatar sewenew commented on May 20, 2024

if I use the command as you involved above, the console will report there is no the path of CMakelists.txt,

My bad. On Linux, you should use : to separate the paths. ; works for Windows. Check the doc for detail.

error while loading shared libraries: libredis++.so: cannot open shared object file: No such file or directory

You should set LD_LIBRARY_PATH before running your executable. Check the doc for more info.

Sorry, but I'm not an expert on cmake. If you have questions on cmake internals, or confused by comments above, you'd better ask an expert for help. Sorry again...

Regards

from redis-plus-plus.

yitian108 avatar yitian108 commented on May 20, 2024

Great! Your suggestion really good for me, and I checked the cmakelists.txt again, and sloved it smoothly. Since I am the beginner of learning redis, I want to find a better c++ library for redis, so I choosed the redis++ as the first library. If you have some good examples for learning this, could you pls share them with me?

Thanks again

from redis-plus-plus.

sewenew avatar sewenew commented on May 20, 2024

@yitian108 You can check the test code for more examples.

Regards

from redis-plus-plus.

yitian108 avatar yitian108 commented on May 20, 2024

@sewenew , thank you sewenew, I believe the test will give me much help about learning the library, but sorry for my three questions:
(1), Can I download the test files via your test page? I only find there is 'master' branch, but I did not find the url of downloading files
(2), As far as I know, the api redis.hgetall is convenient for us to get all related results once, but some people say the api may have performance and will affect the product, isn't it?
(3), Last but not least, thank you for your help during last two days, I want to say if I have other problems of redis++, how can I contact you and which way is more fit?

Regards

from redis-plus-plus.

yitian108 avatar yitian108 commented on May 20, 2024

Hi @sewenew, sorry for the trouble, may I know how to delete a key with pattern, i.e. if I need to delete the keys that start with "abc", so, I try to edit as redis.del("abc*"), it seems not to delete the keys like abc12, abcd, abcc etc.
Regards

from redis-plus-plus.

sewenew avatar sewenew commented on May 20, 2024

@yitian108

(1), Can I download the test files via your test page? I only find there is 'master' branch, but I did not find the url of downloading files

The master branch contains the test files. Check the 'test' directory.

(2), As far as I know, the api redis.hgetall is convenient for us to get all related results once, but some people say the api may have performance and will affect the product, isn't it?

This question is beyond the scope of redis-plus-plus. You'd better seek help from somewhere else, e.g. stackoverflow's redis topic or redis' google groups.

(3), Last but not least, thank you for your help during last two days, I want to say if I have other problems of redis++, how can I contact you and which way is more fit?

If you have problem with redis-plus-plus, you can open a new issue, instead of asking questions under this issue. Since these question has nothing to do with the origin problem, i.e. how to build redis-plus-plus.

may I know how to delete a key with pattern, i.e. if I need to delete the keys that start with "abc", so, I try to edit as redis.del("abc*"), it seems not to delete the keys like abc12, abcd, abcc etc.

Again, this is not a redis-plus-plus building problem. You'd better seek help from stackoverflow or google for it: how to remove redis keys matching a pattern.

Regards

from redis-plus-plus.

cupid-gracer avatar cupid-gracer commented on May 20, 2024

Hi @sewenew

using namespace sw::redis;
auto redis = Redis("127.0.0.1:6379");
redis.set("key", "value");

but I have this result
undefined reference to `sw::redis::Redis::set(std::basic_string_view<char, std::char_traits > const&, std::basic_string_view<char, std::char_traits > const&, std::chrono::duration<long, std::ratio<1l, 1000l> > const&, sw::redis::UpdateType)'
image

Could you help me?

from redis-plus-plus.

sewenew avatar sewenew commented on May 20, 2024

@cupid-gracer Did you install Redis-plus-plus with C++17 while compile your application code with C++11? In that case, you need to compile your application with C++17.

from redis-plus-plus.

cupid-gracer avatar cupid-gracer commented on May 20, 2024

@cupid-gracer Did you install Redis-plus-plus with C++17 while compile your application code with C++11? In that case, you need to compile your application with C++17.

Great!
I've fixed.
thank you

from redis-plus-plus.

meghadriG avatar meghadriG commented on May 20, 2024

This might be related. Haven't been able to solve it yet.
Compiler: clang 12,
build platform Win10,
cmake v3.21.0, vcpkg toolchain used.

Error:
lld-link: error: undefined symbol: public: bool __cdecl sw::redis::Redis::set(class std::basic_string_view<char, struct std::char_traits<char>> const &, class std::basic_string_view<char, struct std::char_traits<char>> const &, class std::chrono::duration<__int64, struct std::ratio<1, 1000>> const &, enum sw::redis::UpdateType)

Relevant Lines from CMakeLists.txt:
find_package(hiredis CONFIG REQUIRED) find_package(redis++ CONFIG REQUIRED) .... target_link_libraries(${PROJECT_NAME} PRIVATE hiredis::hiredis redis++::redis++)

Code
The basic sample in redis++

#include <sw/redis++/redis++.h> ... sw::redis::Redis r("tcp://127.0.01:6379"); r.set("foo", "val"); auto v = r.get("foo");

The undefined symbol error is emitted for both set(..) and get(..)

from redis-plus-plus.

AhmedAbouali avatar AhmedAbouali commented on May 20, 2024

This might be related. Haven't been able to solve it yet. Compiler: clang 12, build platform Win10, cmake v3.21.0, vcpkg toolchain used.

Error: lld-link: error: undefined symbol: public: bool __cdecl sw::redis::Redis::set(class std::basic_string_view<char, struct std::char_traits<char>> const &, class std::basic_string_view<char, struct std::char_traits<char>> const &, class std::chrono::duration<__int64, struct std::ratio<1, 1000>> const &, enum sw::redis::UpdateType)

Relevant Lines from CMakeLists.txt: find_package(hiredis CONFIG REQUIRED) find_package(redis++ CONFIG REQUIRED) .... target_link_libraries(${PROJECT_NAME} PRIVATE hiredis::hiredis redis++::redis++)

Code The basic sample in redis++

#include <sw/redis++/redis++.h> ... sw::redis::Redis r("tcp://127.0.01:6379"); r.set("foo", "val"); auto v = r.get("foo");

The undefined symbol error is emitted for both set(..) and get(..)

check out this link : https://stackoverflow.com/questions/66417946/how-to-import-package-in-cmake-from-vcpkg

I think you are missing target_include_directories

from redis-plus-plus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.