动态库找不到主

时间:2017-10-02 20:06:27

标签: c++

我正在尝试实现用于在动态加载的库中填充工厂的代码。这里的代码示例来自here

Shape.h(基类)

#ifndef __SHAPE_H
#define __SHAPE_H

#include <map>
#include <string>

using namespace std;

// base class for all shapes
class shape
{
    public:
    virtual void draw()=0;
};

// typedef to make it easier to set up our factory
typedef shape *maker_t();
// our global factory
extern map<string, maker_t *,less<string> > factory;
#endif // __SHAPE_H

Circle.h

#ifndef __CIRCLE_H
#define __CIRCLE_H

#include "Shape.h"

class circle : public shape
{
   public:
       void draw();
};
#endif // __CIRCLE_H

Circle.cpp

#include <iostream>
#include "Circle.h"

using namespace std;

void circle::draw()
{
    // simple ascii circle<\n>
    cout << "\n";
    cout << "      ****\n";
    cout << "    *      *\n";
    cout << "   *        *\n";
    cout << "   *        *\n";
    cout << "   *        *\n";
    cout << "    *      *\n";
    cout << "      ****\n";
    cout << "\n";
}

extern "C" {
shape *maker()
{
    return new circle;
}

class proxy
{
    public:
        proxy()
        {
            // register the maker with the factory
            factory["circle"] = maker;
        }
};

// our one instance of the proxy
proxy p;

}

主要

#include <iostream>
#include <map>
#include <list>
#include <vector>
#include <string>
#include <dlfcn.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "Shape.h"

using namespace std;

// size of buffer for reading in directory entries
static unsigned int BUF_SIZE = 1024;

// our global factory for making shapes
map<string, maker_t *, less<string> > factory;

int main(int argc, char **argv)
{
    FILE *dl;   // handle to read directory
    char *command_str = "ls *.so";  // command string to get dynamic lib names
    char in_buf[BUF_SIZE]; // input buffer for lib names
    list<void *> dl_list; // list to hold handles for dynamic libs
    list<void *>::iterator itr;
    vector<string> shape_names;  // vector of shape types used to build menu
    list<shape *> shape_list;  // list of shape objects we create
    list<shape *>::iterator sitr;
    map<string, maker_t *, less<string> >::iterator fitr;

    // get the names of all the dynamic libs (.so files) in the current dir
    dl = popen(command_str, "r");
    if (!dl)
    {
        perror("popen");
        return(-1);
    }

    void *dlib;
    char name[1024];

    while (fgets(in_buf, BUF_SIZE, dl))
    {
        // trim off the whitespace
        char *ws = strpbrk(in_buf, " \t\n");
        if(ws) 
        {
            *ws = '\0';
        }

        // append ./ to the front of the lib name
        sprintf(name, "./%s", in_buf);
        dlib = dlopen(name, RTLD_NOW);
        if(dlib == NULL)
        {
            cerr << dlerror() << endl;
            return(-1);
        }

        // add the handle to our list
        dl_list.insert(dl_list.end(), dlib);
    }

    int i = 0;
    // create an array of the shape names
    for (fitr=factory.begin(); fitr!=factory.end(); fitr++)
    {
        shape_names.insert(shape_names.end(), fitr->first);
        i++;
    }
    int choice;
    // create a menu of possible shapes to create and let the user make some
    while (1)
    {
        i = 1;
        for(fitr=factory.begin(); fitr!=factory.end(); fitr++)
        {
            cout << i << " - Create " << fitr->first << endl;
            i++;
        }
        cout << i << " - Draw created shapes\n";
        i++;
        cout << i << " - Exit\n";
        cout << "> ";
        cin >> choice;

        if (choice == i)
        {
            // destroy any shapes we created

            for(sitr=shape_list.begin(); sitr!=shape_list.end(); sitr++)
            {
                delete *sitr;
            }

            // close all the dynamic libs we opened
            for (itr=dl_list.begin(); itr!=dl_list.end(); itr++)
            {
                dlclose(*itr);
            }

            return(1);
        }

        if (choice == i - 1)
        {                
            // draw the shapes
            for (sitr=shape_list.begin(); sitr!=shape_list.end(); sitr++)
            {
                (*sitr)->draw();
            }
        }

        if (choice > 0 && choice < i - 1)
        {
            // add the appropriate shape to the shape list
            shape_list.insert(shape_list.end(), factory[shape_names[choice-1]]());
        }
    }
}

当我运行它时,我收到以下错误:

  

./ libCircle.so:undefined symbol:factory

如何获取 libCircle.so 以查看工厂地图并更新它?

2 个答案:

答案 0 :(得分:1)

您的问题是默认情况下不会导出可执行文件中的符号,因此dlopen加载的库无法解析这些符号。

来自GNU ld的手册:

   -E
   --export-dynamic
   --no-export-dynamic
       When creating a dynamically linked executable, using the -E option or
       the --export-dynamic option causes the linker to add all symbols to the
       dynamic symbol table.  The dynamic symbol table is the set of symbols
       which are visible from dynamic objects at run time.

       If you do not use either of these options (or use the
       --no-export-dynamic option to restore the default behavior), the
       dynamic symbol table will normally contain only those symbols which are
       referenced by some dynamic object mentioned in the link.

       If you use "dlopen" to load a dynamic object which needs to refer back
       to the symbols defined by the program, rather than some other dynamic
       object, then you will probably need to use this option when linking the
       program itself.

       You can also use the dynamic list to control what symbols should be
       added to the dynamic symbol table if the output format supports it.
       See the description of --dynamic-list.

       Note that this option is specific to ELF targeted ports.  PE targets
       support a similar function to export all symbols from a DLL or EXE; see
       the description of --export-all-symbols below.

所以你的解决方案是:

  • 链接存根共享对象,该对象使用应导出的符号(请参阅链接中提到的某些动态对象引用的&#34; &#34;手册页中的文本)
  • 将共享符号移动到一个单独的共享对象中,可执行文件和dlopen&#d; d库可以全部(动态)链接
  • 在链接时使用factory
  • 从可执行文件中显式导出--dynamic-list
  • 在链接时使用--export-dynamic
  • 显式地从可执行文件中导出所有符号

答案 1 :(得分:0)

您正在main.cpp中定义“C ++”符号factory,但是,您在其他文件中引用了“C”符号“factory”。你必须在main.cpp中使factory成为extern“C”。