dlopen 相关错误

dlopen 相关错误在使用 ndk 编译一个 32 位 arm 平台的 share so 库 在 c 中打开 c 编写的 nbsp nbsp nbsp so 库 so 库可以正常的打开使用懒加载 但是始终找不到函数 提示没有定义的符号链接 解决方法 extern C 使用这个将你的 so 的 c 文件包含 这样在 dlopen 时可以正常的调用

在使用ndk 编译一个32 位arm 平台的share .so 库, 在c ++ 中打开c++编写的    .so 库, .so 库可以正常的打开使用懒加载, 但是始终找不到函数,提示没有定义的符号链接.

解决方法:

extern “C” {

}

使用这个将你的.so 的c++ 文件包含,这样在dlopen 时可以正常的调用.

 使用dlopen和dlsym来使用C++中的类 2008-08-09 23:43:37

分类:

一般来说,dlopen和dlsym是来处理C库中的函数的,对于C++中存在的name mangle问题,类的问题就不易处理,看下文你会有所收获。

转载自:

http://www.linuxsir.org/bbs/printthread.php?t=

 

C++ dlopen mini HOWTO 中译版 [原创]
 

C++ dlopen mini HOWTO
作者:Aaron Isotton

2006-03-16

译者: 2006-08-05


代码:

//----------
//main.cpp:
//----------
#include


#include

int main() {

    using std::cout;
    using std::cerr;

    cout << "C++ dlopen demo\n\n";     // open the library
    cout << "Opening hello.so...\n";
    void* handle = dlopen("./hello.so", RTLD_LAZY);
    
    if (!handle) {

        cerr << "Cannot open library: " << dlerror() << '\n';
        return 1;
    }
    
    // load the symbol
    cout << "Loading symbol hello...\n";
    typedef void (*hello_t)();

    // reset errors
    dlerror();
    hello_t hello = (hello_t) dlsym(handle, "hello");
    const char *dlsym_error = dlerror();
    if (dlsym_error) {

        cerr << "Cannot load symbol 'hello': " << dlsym_error <<
            '\n';
        dlclose(handle);
        return 1;
    }
    
    // use it to do the calculation
    cout << "Calling hello...\n";
    hello();
    
    // close the library
    cout << "Closing library...\n";
    dlclose(handle);
}

//----------
// hello.cpp:
//----------
#include

extern "C" void hello() {

    std::cout << "hello" << '\n';
}





















































代码:

extern "C" int foo;
extern "C" void bar();

代码:

extern "C" {

    extern int foo;
    extern void bar();
}


代码:

extern "C" int foo;

代码:

extern "C" {

    int foo;
}

代码:

//----------
//main.cpp:
//----------
#include "polygon.hpp"
#include


#include

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() << '\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();

    // 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);
}

//----------
//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;
    }

    virtual double area() const = 0;
};

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

#endif

//----------
//triangle.cpp:
//----------
#include "polygon.hpp"
#include

class triangle : public polygon {

public:
    virtual double area() const {

        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;
}







































































































版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/211270.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 下午10:49
下一篇 2026年3月18日 下午10:49


相关推荐

  • 2017中国程序员薪资生存现状调查报告结论_程序员的收入

    2017中国程序员薪资生存现状调查报告结论_程序员的收入程序员一直都是一个备受人们关注的群体。2014年,据IDC统计,全球约有1850万名程序员,中国占10%。随着近年全国互联网创业热潮的兴起,“互联网+”、“云计算”以及“智能硬件”等领域发展迅速,市场对程序员的需求更为旺盛。  由程序员客栈联合稀土掘金通过对北京、广东、浙江、上海等全国28个省、直辖市及特别行政区的10W+优秀程开发者进行了一次调查。调查报告里对程序员的年龄组成、性别比例、

    2022年10月11日
    3
  • 【Custom Mutator Fuzz】简单Protobuf使用练习

    【Custom Mutator Fuzz】简单Protobuf使用练习前面两篇文章中已经讲解了protobuf的结构以及生成代码的分析,这篇文章简单的介绍一下protobuf的使用。这里主要跟着libprotobuf-mutator_fuzzing_learning项目进行结构感知模糊测试练习

    2025年8月31日
    7
  • 200 : parsererror错误

    200 : parsererror错误原因ajax请求中返回data,与controller中requestMapping方法返回类型不一致导致,或者controller中方法无返回值即void,都可引起该错误。

    2022年6月23日
    34
  • 女生做java开发最多做几年,附赠复习资料

    女生做java开发最多做几年,附赠复习资料前言疫情过去,真正的春暖花开又回来了,时不时的可以和朋友约个饭,感慨今年的工作竞争压力很大,工作很不好找。作为一个开发人员,你是否面上了理想的公司,拿到了理想中的薪资?作为程序员,跳槽就是最好的涨薪方式。前提当然是你有足够实力,而不是只会纸上谈兵。面试准备不充分,就是浪费时间,更是对自己不负责任。今天给大家分享一份由粉丝投稿,我精心整理出来的一份1200页Java架构面试专题(文末见面试答案),绝大部分都是一线大厂的面试真题,可以根据这份面试专题查漏补缺,希望能够帮助你尽快找到工作!kafka面试基

    2022年7月7日
    20
  • 优化阶乘算法的探索

    优化阶乘算法的探索优化阶乘算法的探索中国地质大学(武汉)  陈海丰 阶乘(factorial)是基斯顿·卡曼(ChristianKramp,1760–1826)于1808年发明的运算符号。阶乘,也是数学里的一种术语,是指从1乘以2乘以3乘以4一直乘到所要求的数。例如所要求的数是4,则阶乘式是1×2×3×4,得到的积是24,24就是4的阶乘。如果所要求的数是n,则阶乘式是1×2×3×……×n,设

    2022年7月24日
    8
  • iFlyCode插件(以IDEA为例)

    iFlyCode插件(以IDEA为例)

    2026年3月14日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号