1

I am using PIMPL to hide implementation details of my library from the end user. Also want to capture all 3rd party dependencies of the library within itself; so that I would not need the end user to link them to use my library.

The Interface

//// core.h (the interface)
namespace N
{
    enum class priority { ... };
    enum class type { ... }
    class event_base { 
        ... // getting queued time, priority, name, type etc;
        private:
            priority m_priority;
            type m_type;
        
    };
    typedef std::shared_ptr<event_base> p_evt;
    
    class server_backend
    {
        public:
            server_backend(const uint32_t &port = 10100);
            ~server_backend();
            
            bool try_push(p_evt p);
            
            size_t get_size_evt_bus();
            
            void dump_evt_bus();
        
        private:
            struct impl;
            std::unique_ptr<impl> p_impl;
    }
}

Implementation details

//// core.cpp
#include "backend.h"
using namespace N;
class server_backend::impl
{
    impl(const uint32_t& port)
        :_bimpl(std::make_unique<backendimpl>(port))
    {}
    
    bool try_push(p_evt p)
    {
        return true;
    }
    
    size_t get_size_evt_bus()
    {
        return 0;
    }
    
    void dump_evt_bus()
    { }

    private:
        std::unique_ptr<backendimpl> _bimpl;

}

server_backend::server_backend()
    :p_impl(std::make_unique<impl>(port))
{}

server_backend::~server_backend() = default;

/// call impl functions from server_backend public functions

My implementation creates an instance of a class deeper in the library. This class does contain datatypes from 3rd party libs.

//backend.h
class backendimpl final
{
    backend(const uint32_t &port);
    
    ~backendimpl();
    
    bool try_push(p_evt p);
            
    size_t get_size_evt_bus();
            
    void dump_evt_bus();
    
  
    // other public functions
    
    private:
    // private members (some from 3rd party libs)
    
    // private functions
}

I am trying to hide these third party libs from the end user. But I find that when I create a client app (basically just a CTest stub), I do need to include the 3rd party libs along with my library. Logically, I guess the problem comes up when I include the deeper class header into my interface implementation; but I do not see a way around it.

Can anyone help?

Roy2511
  • 938
  • 1
  • 5
  • 22
  • Workaround : Build the lib as shared, instead of static. [This answer.](https://stackoverflow.com/a/7616425/3089908) says " When you create a static library, the linker doesn't try to resolve everything that is missing; it assumes you'll be linking it later to another library, or that the executable itself will contribute some missing function." I won't add this workaround as an answer, because it's not one. – Roy2511 Aug 07 '20 at 16:39

0 Answers0