我正在尝试制作一个简单的插件系统。我在图书馆里有三个班级:
// AbstractPlugin.h
//
// This is used both by the class library and by the application.
#ifndef AbstractPlugin_INCLUDED
#define AbstractPlugin_INCLUDED
#include <string>
class AbstractPlugin
{
public:
AbstractPlugin();
virtual ~AbstractPlugin();
virtual std::string name() const = 0;
};
#endif // AbstractPlugin.h
// AbstractPlugin.cpp
//
// This is used both by the class library and by the application.
#include "AbstractPlugin.h"
AbstractPlugin::AbstractPlugin()
{
}
AbstractPlugin::~AbstractPlugin()
{
}
// PluginLibrary.cpp
#include "AbstractPlugin.h"
#include "Poco/ClassLibrary.h"
#include <iostream>
#include <string>
class PluginA: public AbstractPlugin
{
public:
std::string name() const
{
return "PluginA";
}
};
class PluginB: public AbstractPlugin
{
public:
std::string name() const
{
return "PluginB";
}
};
POCO_BEGIN_MANIFEST(AbstractPlugin)
POCO_EXPORT_CLASS(PluginA)
POCO_EXPORT_CLASS(PluginB)
POCO_END_MANIFEST
// optional set up and clean up functions
void pocoInitializeLibrary()
{
std::cout << "PluginLibrary initializing" << std::endl;
}
void pocoUninitializeLibrary()
{
std::cout << "PluginLibrary uninitializing" << std::endl;
}
这是我的main.cpp文件。我尝试甚至使用相对路径而不是使用“ lib”前缀来加载库。
// main.cpp
#include "Poco/ClassLoader.h"
#include "Poco/Manifest.h"
#include "AbstractPlugin.h"
#include <iostream>
#include <Poco/SharedLibrary.h>
using Poco::SharedLibrary;
typedef Poco::ClassLoader<AbstractPlugin> PluginLoader;
typedef Poco::Manifest<AbstractPlugin> PluginManifest;
int main(int argc, char** argv)
{
PluginLoader loader;
std::string libName("/home/sstoenescu/Work/toys/pocoClassLoaderExample/libPluginLibrary");
libName += Poco::SharedLibrary::suffix(); // append .dll or .so
loader.loadLibrary(libName);
PluginLoader::Iterator it(loader.begin());
PluginLoader::Iterator end(loader.end());
for (; it != end; ++it)
{
std::cout << "lib path: " << it->first << std::endl;
PluginManifest::Iterator itMan(it->second->begin());
PluginManifest::Iterator endMan(it->second->end());
for (; itMan != endMan; ++itMan)
{
std::cout << itMan->name() << std::endl;
}
}
AbstractPlugin* pPluginA = loader.create("PluginA");
AbstractPlugin* pPluginB = loader.create("PluginB");
std::cout << pPluginA->name() << std::endl;
std::cout << pPluginB->name() << std::endl;
loader.classFor("PluginA").autoDelete(pPluginA);
delete pPluginB;
loader.unloadLibrary(libName);
return 0;
}
这就是我得到的:
抛出'Poco :: LibraryLoadException'实例后调用terminate what():无法加载库 中止(核心已弃用)
我不知道出什么问题了。我的CMakeLists.txt文件如下所示:
cmake_minimum_required(VERSION 2.8.3)
project(tutocpp14)
#set(Poco_DIR "/usr/local/lib/cmake/Poco/")
set(Poco_DIR "/usr/local/lib/")
set(Poco_INCLUDE_DIRS "/usr/include/Poco/")
find_package(Poco REQUIRED COMPONENTS Foundation Net XML Util) # add other components here
# check c++11 / c++0x
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11 " COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
set(CMAKE_CXX_FLAGS "-std=c++11")
elseif(COMPILER_SUPPORTS_CXX0X)
set(CMAKE_CXX_FLAGS "-std=c++0x")
else()
message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()
include_directories( ${Poco_INCLUDE_DIRS})
add_library(PluginLibrary SHARED src/PluginLibrary.cpp)
add_executable(publisher src/main.cpp)
target_link_libraries(publisher ${Poco_LIBRARIES})
答案 0 :(得分:0)
解决方案是将AbstractPlugin类放在清单声明(PluginLibrary.cpp)所在的同一源文件中,该文件实际上是作为.so库构建的文件。