zoukankan      html  css  js  c++  java
  • linux下c++动态库的生成及使用

    文章来源于:http://hi.baidu.com/ablenavy/item/b498901c6826bbf587ad4e33

    我的程序是一个类,在网上找了半天,都是c的例子,c++的类封装成静态库倒容易,可是如何封装成动态库,在其它程序中调用呢?
    Linux下的动态链接库叫so,即Shared Object,共享对象。一些函数就不说了,网上多的是。把我遇到的问题写下来吧

    提示错误 undefined reference to `dlopen'
    编译时增加“-ldl”选项即可解决。

    提示错误 cannot open shared object file: No such file or directory
    将当前目录的绝对路径添加到LD_LIBRARY_PATH即可
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/zhj/test_so/

    提示错误 undefined reference to
    检查了一下,原来在makefile里少包含了一个文件。
    这一类错误一般都是缺少相应的类文件所致。

    例子来源:
    http://www.linuxsir.org/bbs/printthread.php?t=266890

    这个扩展名为hpp的文件,我分解了一下,下面就上代码吧

    polygon.h
    //----------
    //polygon.h:
    //----------
    #ifndef POLYGON_H
    #define POLYGON_H
    #include <stdio.h>

    class polygon {
    protected:
    double side_length_;

    public:
    polygon();
    virtual ~polygon();

    void set_side_length(double side_length);

    virtual double area() const;
    };

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

    #endif

    polygon.cpp
    //----------
    //polygon.cpp:
    //----------
    #include "polygon.h"


    polygon::polygon()
    {
    //set_side_length(0);
    }

    polygon::~polygon()
    {
    }

    void polygon::set_side_length(double side_length)
    {
    printf("polygon side_length: %f ",side_length);
    side_length_ = side_length;
    printf("polygon side_length_: %f ",side_length_);
    }

    double polygon::area() const
    {
    return 0;
    }

    triangle.cpp
    //----------
    //triangle.cpp:
    //----------
    #include "polygon.h"
    #include <cmath>

    class triangle : public polygon {
    public:
    virtual double area() const {
    printf("triangle side_length_: %f ",side_length_);
    return side_length_ * side_length_ * sqrt(3) / 2;
    }
    };


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

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

    main.cpp
    //----------
    //main.cpp:
    //----------
    #include "polygon.h"
    #include <iostream>
    #include <dlfcn.h>

    int main() {
    using std::cout;
    using std::cerr;

    // load the triangle library
    void* triangle = dlopen("./triangle.so", RTLD_LAZY);
    if (!triangle) {
    cerr << "Cannot load library: " << dlerror() << ' ';
    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 << ' ';
    return 1;
    }

    destroy_t* destroy_triangle = (destroy_t*) dlsym(triangle, "destroy");
    dlsym_error = dlerror();
    if (dlsym_error) {
    cerr << "Cannot load symbol destroy: " << dlsym_error << ' ';
    return 1;
    }

    // create an instance of the class
    polygon* poly = create_triangle();

    // use the class
    poly->set_side_length(7);
    cout << "The area is: " << poly->area() << ' ';

    // destroy the class
    destroy_triangle(poly);

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

    makefile

    objects = main.o polygon.o
    main: $(objects)
    g++ -g -rdynamic -o $@ $(objects) -ldl
    main.o: main.cpp
    g++ -g -c main.cpp
    polygon.o: polygon.cpp polygon.h
    g++ -g -c polygon.cpp

    .PHONY : clean
    clean :
    -rm main $(objects)


    用下面的命令编译,生成libtriangle.so
    g++ -g -fpic -shared -o libtriangle.so triangle.cpp polygon.cpp

    然后make一下,运行main试试吧!

  • 相关阅读:
    Git工作中用法(Gitlab)
    面试记录,不定时更新中。。。
    webservice 技术改进
    HTTP详解
    Git学习05 --分支管理02
    Git学习04 --分支管理
    链表判环问题
    k-选取问题
    图片与字符串(base64编码)的转化
    二叉堆与堆排序的简单实现
  • 原文地址:https://www.cnblogs.com/foreverstars/p/3419767.html
Copyright © 2011-2022 走看看