0

I am creating a DLL library which will export some functions to be used in other C++ projects. In my library, I am using precompiled headers, as well as Boost ASIO. However, I want to know if it is possible to contain all ASIO related things within the library itself so that the other program does not need to include them. For example:

stdafx.h (library):

#pragma once

#include "targetver.h"

#define WIN32_LEAN_AND_MEAN

#include <Windows.h>
#include <WinSock2.h>
#include <iostream>
#include <string.h>

#pragma comment(lib, "ws2_32")

#include <boost/asio.hpp>

Client.h (library):

#pragma once

#ifdef LIB_EXPORTS
#define LIB_API __declspec(dllexport)
#else
#define LIB_API __declspec(dllimport)
#endif

using boost::asio::ip::tcp;

namespace TestLib {
    class Client {
        boost::asio::io_service io_service_;
        tcp::resolver resolver_;
        tcp::socket socket_;

    public:
        LIB_API Client();

        LIB_API bool Connect(const std::string& szHost, const std::string& szPort);

        virtual ~Client();
    };
}

However, in my program that uses this library, I want to be able to include Client.h without also having to include Boost ASIO. Because I include this header without including Boost ASIO, I get errors about all of the lines in Client.h that reference Boost utilities, such as the using statement and the three private member variables.

How do I go about creating the library in such a way so that my other program only needs to include Client.h, for example?

John
  • 56
  • 9

1 Answers1

0

I have solved my problem using Anon Mail's suggestion. Here is the solution:

Client.h (library):

#pragma once

#ifdef LIB_EXPORTS
#define LIB_API __declspec(dllexport)
#else
#define LIB_API __declspec(dllimport)
#endif

namespace TestLib {
    class Client {
        struct Imp;
        std::unique_ptr<Imp> imp_;

    public:
        LIB_API Client();

        LIB_API bool Connect(const std::string& szHost, const std::string& szPort);

        LIB_API virtual ~Client();
    };
}

Client.cpp (library):

namespace TestLib {
    struct Client::Imp {
        Client::Imp() : 
            io_service_(new boost::asio::io_service()), 
            resolver_(new tcp::resolver(*io_service_)),
            socket_(new tcp::socket(*io_service_)) {

        }

        std::shared_ptr<boost::asio::io_service> io_service_;
        std::shared_ptr<tcp::resolver> resolver_;
        std::shared_ptr<tcp::socket> socket_;
    };

    Client::Client() : imp_(new Imp) {
    }

    bool Client::Connect(const std::string& szHost, const std::string& szPort) {
        tcp::resolver::query query(szHost, szPort);

        tcp::resolver::iterator endpoint_iter = imp_->resolver_->resolve(query);

        boost::asio::connect(*imp_->socket_, endpoint_iter);

        return true;
    }
}

Forgive me if I'm using the wrong types of pointers. Any guidance on this solution would be much appreciated!

John
  • 56
  • 9