在使用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
代码:
int main() { cout << "C++ dlopen demo\n\n"; // open the library // reset errors //---------- extern "C" void hello() {//----------
//main.cpp:
//----------
#include
#include
using std::cout;
using std::cerr;
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)();
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
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;
}
代码:
int main() { // load the triangle library // reset errors // create an instance of the class // use the class // unload the triangle library //---------- class polygon { public: virtual ~polygon() {} void set_side_length(double side_length) { virtual double area() const = 0; // the types of the class factories #endif //---------- class triangle : public polygon { // the class factories extern "C" void destroy(polygon* p) {//----------
//main.cpp:
//----------
#include "polygon.hpp"
#include
#include
using std::cout;
using std::cerr;
void* triangle = dlopen("./triangle.so", RTLD_LAZY);
if (!triangle) {
cerr << "Cannot load library: " << dlerror() << '\n';
return 1;
}
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;
}
polygon* poly = create_triangle();
poly->set_side_length(7);
cout << "The area is: " << poly->area() << '\n'; // destroy the class
destroy_triangle(poly);
dlclose(triangle);
}
//polygon.hpp:
//----------
#ifndef POLYGON_HPP
#define POLYGON_HPP
protected:
double side_length_;
polygon()
: side_length_(0) {}
side_length_ = side_length;
}
};
typedef polygon* create_t();
typedef void destroy_t(polygon*);
//triangle.cpp:
//----------
#include "polygon.hpp"
#include
public:
virtual double area() const {
return side_length_ * side_length_ * sqrt(3) / 2;
}
};
extern "C" polygon* create() {
return new triangle;
}
delete p;
}
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/211270.html原文链接:https://javaforall.net
