Create a gist now

Instantly share code, notes, and snippets.

@xiongjia /0_main.cxx
Last active Sep 14, 2017

What would you like to do?
A simple sample of Boost DLL #boost #devsample
/**
* A simple sample of Boost DLL
*/
#include <iostream>
#include "boost/shared_ptr.hpp"
#include "boost/function.hpp"
#include "boost/dll/import.hpp"
#include "1_plugin.hxx"
namespace boostfs = boost::filesystem;
namespace boostdll = boost::dll;
/**
* You can build this sample via Boost 1.61 + CMake
* 0_main.cxx - The main function, it will load & create the plugin.
* 1_plugin.hxx - The definition of the Plugin APIs
* 1_plugin.cxx - A test plugin exported via BOOST_DLL_ALIAS()
*/
int main(int argc, char **argv)
{
std::cout << "Boost DLL testing ..." << std::endl;
/* Load the plugin from current working path
* (e.g. The plugin on Windows is ${CWD}/ProgPlug.dll )
*/
boostfs::path pluginPath = boostfs::current_path() / boostfs::path("ProgPlugin");
std::cout << "Load Plugin from " << pluginPath << std::endl;
typedef boost::shared_ptr<Plugin>(PluginCreate)();
boost::function <PluginCreate> pluginCreator;
try
{
pluginCreator = boostdll::import_alias<PluginCreate>(pluginPath,
"create_plugin", boostdll::load_mode::append_decorations);
}
catch (const boost::system::system_error &err)
{
std::cerr << "Cannot load Plugin from " << pluginPath << std::endl;
std::cerr << err.what() << std::endl;
return -1;
}
/* create the plugin */
auto plugin = pluginCreator();
std::cout << "Plugin Name: " << plugin->name() << std::endl;
std::cout << "Plugin API test: 1 + 1 = " << plugin->add(1, 1) << std::endl;
return 0;
}
/**
* A simple sample of Boost DLL
*/
#include "boost/shared_ptr.hpp"
#include "boost/make_shared.hpp"
#include "boost/dll/alias.hpp"
#include "1_plugin.hxx"
class PluginTest1 : public Plugin
{
public:
static boost::shared_ptr<Plugin> create(void)
{
return boost::make_shared<PluginTest1>();
}
public:
PluginTest1(void)
: Plugin()
{
/* NOP */
}
virtual const char *name(void) const
{
return "PluginTest1";
}
virtual const int add(const int v1, const int v2)
{
return v1 + v2;
}
};
BOOST_DLL_ALIAS(PluginTest1::create, create_plugin)
/**
* A simple sample of Boost DLL
*/
#ifndef _1_PLUGIN_HXX_
#define _1_PLUGIN_HXX_ 1
class Plugin
{
public:
virtual const char *name(void) const = 0;
virtual const int add(const int v1, const int v2) = 0;
};
#endif /* !defined(_1_PLUGIN_HXX_) */
# CMake build script
cmake_minimum_required(VERSION 2.8)
# project name & version
project(ProgDLLTest)
# common settings (Boost libraries)
if (MSVC)
# Enable the static libraries on Windows
foreach (flag_var
CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO
CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO)
# update to the static version of the run time library
string(REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
endforeach()
set(CMAKE_C_STANDARD_LIBRARIES "")
set(CMAKE_CXX_STANDARD_LIBRARIES "")
endif()
# enable boost static flag
unset(Boost_LIBRARIES)
set(Boost_USE_STATIC ON)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME ON)
# boost components
find_package(Boost REQUIRED system filesystem)
include_directories("${PROJECT_SOURCE_DIR}"
"${Boost_INCLUDE_DIRS}")
set(progdll_dbg_libs
"${Boost_SYSTEM_LIBRARY_DEBUG}"
"${Boost_FILESYSTEM_LIBRARY_DEBUG}")
set(progdll_opt_libs
"${Boost_SYSTEM_LIBRARY_RELEASE}"
"${Boost_FILESYSTEM_LIBRARY_RELEASE}")
add_executable(ProgDLLTest
"${PROJECT_SOURCE_DIR}/0_main.cxx")
add_library(ProgPlugin SHARED
"${PROJECT_SOURCE_DIR}/1_plugin.cxx"
"${PROJECT_SOURCE_DIR}/1_plugin.hxx")
target_link_libraries(ProgDLLTest
debug "${progdll_dbg_libs}"
optimized "${progdll_opt_libs}"
ProgPlugin)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment