简单的C ++模块系统无法正常运行

时间:2019-12-02 19:20:49

标签: c++ linux g++ shared-libraries

我已经用C ++实现了一个简单的模块系统(仅用于演示目的)。不幸的是,该程序无法正常工作(它不会产生预期的输出,它什么也不会打印出来)。

这是我的源代码:

module.hpp

#ifndef _MODULE_HPP
#define _MODULE_HPP

#include <memory>

/// Interface for modules.
class Module {
  public:
    virtual void run() = 0;
    virtual ~Module(){};
};

/// Symbol of the module construtor function.
const auto MODULE_CONSTRUCTOR_SYMBOL = "create_module";

/// Type of the module constructor function.
using MODULE_CONSTRUCTOR = std::unique_ptr<Module> (*)();

#endif /* _MODULE_HPP */

first.hpp

#ifndef _FIRST_HPP
#define _FIRST_HPP

#include <iostream>

#include "module.hpp"

class First : public Module
{

public:

    First();

    virtual ~First();

    virtual void run();

    std::unique_ptr<Module> create_module(void);

};

extern "C"
{
    typedef std::unique_ptr<Module> create_first_module_t(void);

    std::unique_ptr<Module> create_module(void);
}

#endif /* _FIRST_HPP */

first.cpp

#include "first.hpp"

First::First()
{
}

std::unique_ptr<Module> First::create_module()
{
    auto ptr = std::unique_ptr<Module>{};

    std::cout << "Creation of first module" << std::endl;

    return ptr;
}

First::~First()
{
    std::cout << "Destruction of first module" << std::endl;
}

void First::run()
{
    std::cout << "Running the first module" << std::endl;
}

second.hpp

#ifndef _SECOND_HPP
#define _SECOND_HPP

#include <iostream>

#include "module.hpp"

class Second : public Module
{

public:

    Second();

    virtual ~Second();

    virtual void run();

    std::unique_ptr<Module> create_module(void);

};

extern "C"
{
    typedef std::unique_ptr<Module> create_second_module_t(void);

    std::unique_ptr<Module> create_module(void);
}

#endif /* _SECOND_HPP */

second.cpp

#include "second.hpp"

Second::Second()
{
}

std::unique_ptr<Module> Second::create_module()
{
    auto ptr = std::unique_ptr<Module>{};

    std::cout << "Creation of second module" << std::endl;

    return ptr;
}

Second::~Second()
{
    std::cout << "Destruction of second module" << std::endl;
}

void Second::run()
{
    std::cout << "Running the second module" << std::endl;
}

main.cpp

#include <memory>
#include <dlfcn.h>

#include "first.hpp"
#include "second.hpp"

int main(void)
{
    if (void* firstModule = dlopen("libFirst", RTLD_NOW))
    {
        if (create_first_module_t* createFirstModule = (create_first_module_t*)dlsym(firstModule, "create_module"))
        {
            std::unique_ptr<Module> firstModule(createFirstModule());
            firstModule.get()->run();
        }

        dlclose(firstModule);
    }

    if (void* secondModule = dlopen("libSecond", RTLD_NOW))
    {
        if (create_second_module_t* createSecondModule = (create_second_module_t*)dlsym(secondModule, "create_module"))
        {
            std::unique_ptr<Module> secondModule(createSecondModule());
            secondModule.get()->run();
        }

        dlclose(secondModule);
    }

    return 0;
}

以下是g ++编译器的说明(在Linux计算机上):

g++ -Wall -Wextra -shared -fPIC first.cpp -o first.so -ldl
g++ -Wall -Wextra -shared -fPIC second.cpp -o second.so -ldl
g++ -Wall -Wextra -c main.cpp
g++ -o main main.o first.so -ldl

预期输出应为以下内容:

$ LD_LIBRARY_PATH=. ./main ./first.so
Creation of first module
Running the first module
Destruction of first module

$ LD_LIBRARY_PATH=. ./main ./first.so ./second.so
Creation of first module
Creation of second module
Running the first module
Running the second module
Destruction of first module
Destruction of second module

我的实现存在问题,但我不知道错误在哪里。

我希望有人可以帮助我解决我的问题。

0 个答案:

没有答案