c++ - 在 C++ 中使用动态类加载时出现链接器错误

标签 c++ vector dynamic linker .so

我正在尝试将一个类动态加载到我的代码中。在 Polygon.hpp 中我有类声明,在 triangle.cpp 中有定义。我已经生成了 triangle.so。 我在我的 main.cpp 中包含了 Polygon.hpp。另外,我正在尝试创建多边形对象的 vector ,但在 vector.push_back 链接器期间抛出“ undefined reference ”错误。如果我注释掉 vector.push_back 一切正常。我知道我创建了一个 vector Polygon* 并使用它,但在我的情况下这是不可能的,因为这会导致现有遗留代码发生变化,这是不可取的。 有没有办法我可以使用这种方法。 这是代码:

多边形.hpp

#ifndef POLYGON_HPP                                                          
#define POLYGON_HPP                                                          

class polygon {
protected:
    double side_length_;

public:
    polygon()
       : side_length_(0) {}

 virtual ~polygon(){}

 void set_side_length(double side_length) {
    side_length_ = side_length;
 }

 polygon (const polygon &obj);

 virtual double area();
};

// the types of the class factories                                          
typedef polygon* create_t();
typedef void destroy_t(polygon*);

#endif

三角形.cpp

#include "polygon.hpp"
#include <cmath>

double polygon::area() {
    return side_length_ * side_length_ * sqrt(3) / 2;
}

polygon::polygon(const polygon &obj)
{
    this->side_length_ = obj.side_length_;
}


// the class factories                                                             
extern "C" polygon* create() {
   return new polygon;
}

extern "C" void destroy(polygon* p) {
  delete p;
}

编译生成.so:

g++ -fPIC -c traingle.cpp
g++ -shared -o traingle.so traingle.o

主要.cpp

#include "polygon.hpp"
#include <iostream>
#include <vector>
#include <dlfcn.h>
using namespace std;
int main() {
using std::cout;
using std::cerr;

vector<polygon> t;   //creating a vector of polygon
// load the triangle library                                             
void* triangle = dlopen("./triangle.so", RTLD_LAZY);
if (!triangle) {
    cerr << "Cannot load library: " << dlerror() << '\n';
    return 1;
}

// reset errors                                                          
dlerror();

// load the symbols                                                      
create_t* create_triangle = (create_t*) dlsym(triangle, "create");
const char* dlsym_error = dlerror();
if (dlsym_error) {
    cerr << "Cannot load symbol create: " << dlsym_error << '\n';
    return 1;
}

destroy_t* destroy_triangle = (destroy_t*) dlsym(triangle, "destroy");
dlsym_error = dlerror();
if (dlsym_error) {
    cerr << "Cannot load symbol destroy: " << dlsym_error << '\n';
    return 1;
}
// create an instance of the class                                       
polygon* poly = create_triangle();

t.push_back(*poly);  //---> this is where the linking error is happening
// use the class                                                         
poly->set_side_length(7);
    cout << "The area is: " << poly->area() << '\n';

// destroy the class                                                     
destroy_triangle(poly);

// unload the triangle library                                           
dlclose(triangle);
}

编译:

$ g++ -std=c++11 main.cpp -ldl
/tmp/cc4RdzvX.o: In function `void   __gnu_cxx::new_allocator<polygon>::construct<polygon, polygon const&>(polygon*,  polygon const&)':
main.cpp: (.text._ZN9__gnu_cxx13new_allocatorI7polygonE9constructIS1_JRKS1_EEEvPT_DpOT0_[_ZN9__gnu_cxx13new_allocatorI7polygonE9constructIS1_JRKS1_EEEvPT_DpOT0_]+0x48): undefined reference to `polygon::polygon(polygon const&)'
/tmp/cc4RdzvX.o: In function `void std::_Construct<polygon, polygon&>(polygon*, polygon&)':
main.cpp:(.text._ZSt10_ConstructI7polygonJRS0_EEvPT_DpOT0_[_ZSt10_ConstructI7polygonJRS0_EEvPT_DpOT0_]+0x44): undefined reference to `polygon::polygon(polygon const&)'
collect2: error: ld returned 1 exit status

最佳答案

您定义了 vector<polygon> t;main.cpp , 这将使它链接到 polygon的 ctor/dtor,这是链接错误。

解决问题:

  1. 要么按照@robert 的回答直接链接polygon.cpp
  2. 或者避免使用polygon的完整类型在你的main.cpp .

所以你应该定义vector<polygon*> t;相反,推/弹出 polygon*到/从 vector 。

你不应该忘记调用 destroy_triangle()当你从 vector 中弹出时,否则你有内存泄漏。

关于c++ - 在 C++ 中使用动态类加载时出现链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39997786/

相关文章:

C++ 语言一些可变的实例

c++ - 在 C++ 中初始化 vector ?

c++ - 如何获取嵌套 vector 中的元素类型?

c++ - 从循环内的 vector 中删除项目

c# - 在 C# 中通过字符串变量初始化一个类?

WPF 和 Linq 到实体 : How can I dynamically display data where it is changed by another user?

c++ - 如何将代码结果(在 void 函数内)写入 C++ 中的文本文件

c++ - 将非静态成员函数作为参数传递给成员函数

c# - 将 C++ 函数转换为 C#

ios - 是否可以从 json 响应 swift ios 动态创建文本字段和按钮?