Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initializing Redis++ on IDE #5

Closed
afifzaki opened this issue Jan 14, 2019 · 24 comments
Closed

Initializing Redis++ on IDE #5

afifzaki opened this issue Jan 14, 2019 · 24 comments

Comments

@afifzaki
Copy link

Hi, thanks for your work.
I try to compile a simple cpp program:

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

using namespace sw::redis;

int main()
{
auto redis = Redis("tcp://127.0.0.1:6379");
return 0;
}

but that script generate an error:

/home/user/Developments/clang/redis_test/main.cpp:10: undefined reference to sw::redis::Redis::Redis(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' CMakeFiles/redis_test.dir/main.cpp.o: In function sw::redis::Connection::ContextDeleter::operator((redisContext*) const':
/usr/local/include/sw/redis++/connection.h:132: undefined reference to `redisFree'

Is there a simplest example do you provide for initiating redisDB?

@sewenew
Copy link
Owner

sewenew commented Jan 14, 2019

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

@sewenew
Copy link
Owner

sewenew commented Jan 14, 2019

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

@afifzaki
Copy link
Author

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?

@sewenew
Copy link
Owner

sewenew commented Jan 15, 2019

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

@afifzaki
Copy link
Author

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.

@sewenew
Copy link
Owner

sewenew commented Jan 15, 2019

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

@afifzaki afifzaki changed the title Initiate Redis++ Initializing Redis++ on IDE Jan 15, 2019
@vakker vakker mentioned this issue Apr 3, 2020
@ideazw ideazw mentioned this issue Aug 6, 2020
@yitian108
Copy link

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

@sewenew
Copy link
Owner

sewenew commented Jan 13, 2021

@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

@yitian108
Copy link

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

@yitian108
Copy link


IMG_7352

@yitian108
Copy link

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.

@sewenew
Copy link
Owner

sewenew commented Jan 13, 2021

@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

@yitian108
Copy link

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

@sewenew
Copy link
Owner

sewenew commented Jan 14, 2021

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

@yitian108
Copy link

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

@sewenew
Copy link
Owner

sewenew commented Jan 14, 2021

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

Regards

@yitian108
Copy link

@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

@yitian108
Copy link

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

@sewenew
Copy link
Owner

sewenew commented Jan 18, 2021

@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

@cupid-gracer
Copy link

cupid-gracer commented Feb 23, 2021

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?

@sewenew
Copy link
Owner

sewenew commented Feb 23, 2021

@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.

@cupid-gracer
Copy link

@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

@meghadriG
Copy link

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(..)

@AhmedAbouali
Copy link

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

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

No branches or pull requests

6 participants