【问题标题】:Poco::ClassLoader throws Poco::LibraryLoadException (cannot load library)Poco::ClassLoader 抛出 Poco::LibraryLoadException(无法加载库)
【发布时间】:2019-03-20 09:52:30
【问题描述】:

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

// 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' 的实例后调用终止 什么():无法加载库 中止(核心转储)

我不知道出了什么问题。我的 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}) 

【问题讨论】:

  • 不确定 Poco,但通常插件库是使用 add_libraryMODULE 关键字构建的,而不是 SHARED 的。
  • 将 try...catch 块放入 main 并捕获 Poco::Exception。这应该为您提供更多信息出了什么问题,例如try { /* your PluginLoader code */ } catch (Poco::Exception&amp; exc) { std::cout &lt;&lt; exc.displayText(): }
  • 谢谢!这表明我在使用 AbstractPlugin 时做错了。如果我将 AbstractPlugin 放在 PluginLibrary.cpp 中,它就可以工作。可能原因是 AbstractLibrary.cpp 中没有 Poco 清单

标签: c++ cmake shared-libraries poco-libraries


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-06
    • 2011-10-16
    • 1970-01-01
    • 2012-01-22
    • 1970-01-01
    • 1970-01-01
    • 2014-08-10
    相关资源
    最近更新 更多