Poco :: ClassLoader抛出Poco :: LibraryLoadException(无法加载库)

问题描述 投票:0回答:1

我正在尝试制作一个简单的插件系统。我在库中有三个类:

// 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'的实例后调用终止what():无法加载库Aborted(core dumped)

我无法弄清楚出了什么问题。我的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}) 
c++ cmake shared-libraries poco-libraries
1个回答
0
投票

解决方案是将AbstractPlugin类放在清单语句所在的同一源文件中(PluginLibrary.cpp),该文件实际上是作为.so库构建的。

© www.soinside.com 2019 - 2024. All rights reserved.