sewenew / redis-plus-plus

Redis client written in C++
Apache License 2.0
1.6k stars 347 forks source link

[QUESTION] Link error when using Subscriber class in redis-plus-plus #525

Closed hardworker0012 closed 11 months ago

hardworker0012 commented 11 months ago

I built redis++, created a library and applied it to my project well. The creation of AsyncRedis worked fine and commands like ping, hset, hgetall command worked fine. I also checked the asynchronous behavior using future.then. And I tried to use pub/sub, and publish works fine,

but when I try to call the subscribe function using AsyncRedis.subscriber, I get the link error below, but even if I find and fix the suspicious part, it doesn't work. I'm writing to ask if you can help me solve it.

I've checked that the redis++ library build includes AsyncSubscriber, and there's no reason for the link error. I've been trying to build it for two days and haven't been able to solve it, so I'm posting a question.

-Link error message

"error LNK2019: "public: class boost::future cdecl sw::redis::AsyncSubscriber::subscribe(class std::basic_string_view<char,struct std::char_traits > const &)" (?subscribe@AsyncSubscriber@redis@sw@@QEAA?AV? $future@X@boost@@AEBV?$basic_string_view@DU?$char_traits@D@std@@@std@@@Z)"public: bool cdecl RedisTest::Initialize(int,unsigned char,char const ,int,char const )" (?Initialize@RedisTest@@QEAA_NHEPEBDH0@Z)"

P.S. redis++ is a very good library. Many thanks to you. My question was sent through a translator, so I don't know if it made sense.

Environment:

OS: Windows11 Compiler: Visual Studio 2022(17.6.5) redis : version 7.2.1 hiredis version: version 1.2.0 redis-plus-plus version: 1.3.10 I want to use Redis++'s Subscribe feature.

sewenew commented 11 months ago

I've left a comment on stackoverflow, please check it:

Looks like you used the continuation feature with boost. In that case, you should install boost first, enable the boost future support with -DREDIS_PLUS_PLUS_ASYNC_FUTURE=boost, and link boost with your application. Check this for detail.

hardworker0012 commented 11 months ago

Thank you for your help. I solved the problem, I didn't figure out where it was working, but there seems to be some command that determines which header to use, boost or std, and copies the file. As a result, async_utils.h, which uses std::future, was overwritten at build time, so when I tried to use boost::future as the build output lib, I got a link error.

My solution was to override the file to always overwrite async_utils.h with the boost version.


As an additional question, is using only redis++_static.lib the same as using redis++.lib and redis++.dll?

Also, if I subscribe to a channel as a "Subscriber or AsyncSubscriber" and am receiving messages, is it okay to subscribe to the channel additionally in a separate thread? The reason I ask is that I've seen it work without any issues when I've done it, but I'm wondering if this is the correct way to use it.

Redis++ is a great library that I really enjoy using. I really appreciate it.

Translated with www.DeepL.com/Translator (free version)

sewenew commented 11 months ago

It seems that you didn’t run the make install command, which copies the right headers to the installation directory. With Visual Studio, there should be an install menu on the menu bar, which runs the install stuff. This is a better way than manually copying files.

Regards


发件人: hardtosay @.> 发送时间: Monday, October 30, 2023 10:43:23 AM 收件人: sewenew/redis-plus-plus @.> 抄送: sewenew @.>; Comment @.> 主题: Re: [sewenew/redis-plus-plus] [QUESTION] Link error when using Subscriber class in redis-plus-plus (Issue #525)

Thank you for your help. I solved the problem, I didn't figure out where it was working, but there seems to be some command that determines which header to use, boost or std, and copies the file. As a result, async_utils.h, which uses std::future, was overwritten at build time, so when I tried to use boost::future as the build output lib, I got a link error.

My solution was to override the file to always overwrite async_utils.h with the boost version.


As an additional question, is using only redis++_static.lib the same as using redis++.lib and redis++.dll?

Also, if I subscribe to a channel as a "Subscriber or AsyncSubscriber" and am receiving messages, is it okay to subscribe to the channel additionally in a separate thread? The reason I ask is that I've seen it work without any issues when I've done it, but I'm wondering if this is the correct way to use it.

Redis++ is a great library that I really enjoy using. I really appreciate it.

Translated with www.DeepL.com/Translatorhttp://www.DeepL.com/Translator (free version)

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

sewenew commented 11 months ago

As an additional question, is using only redis++_static.lib the same as using redis++.lib and redis++.dll?

redis++_static_lib is static library, while others are dynamic library. You can use either. It depends on whether you want to link it statically or dynamically.

if I subscribe to a channel as a "Subscriber or AsyncSubscriber" and am receiving messages, is it okay to subscribe to the channel additionally in a separate thread?

Do you mean that you have two threads, both threads subscribe to the same channel with different Subscriber or AsyncSubscriber? Yes, you can do that safely. Since different subscribers are dependent objects and has nothing to do with each other.

Regards

hardworker0012 commented 11 months ago

I'm sorry. I think I may have misunderstood the meaning of my question, so let me rephrase it.

    ~MainThread
    {
        _AsyncSubscriber->on_meta([this](Subscriber::MsgType type, OptionalString OptionParamStr, long long num)
        _AsyncSubscriber->on_message([this](std::string channel, std::string msg)
        _AsyncSubscriber->on_pmessage([this](std::string pattern, std::string channel, std::string msg)
        ...
        _AsyncSubscriber->psubscribe(pattern); 

        **// Receiving a message from an IO Thread...**
        ...
        ...
        Make_SubThread()...
    }

    ~SubThread
    {
        _AsyncSubscriber->psubscribe(addedPattern)  // 
        _AsyncSubscriber->subscribe(addedChannel)  // 
    }

After creating the _AsyncSubscriber in the main thread and registering each callback as above. subscribe to the channel, and then while it's Receiving the message I'm wondering if it's ThreadSafe to subscribe to additional channels in a subthread?

sewenew commented 11 months ago

Yes, the code you given is thread-safe.

Regards

hardworker0012 commented 11 months ago

Thank you for your helpful reply. May you always be happy. Thank you so much