I had gone through multiple stack overflow posts and tried to implement following example that uses dlopen with c++ objects class I have following code.
1) File hello.cc
#include <stdio.h>
#include "hello.h"
A::A() {
init1();
}
void A::init1() {
printf("\n init ");
}
2) File hello.h
#include <iostream>
class A {
public:
A();
void init1();
inline void fun () { cout << "\n Inside fun"; }
};
extern "C" A* createA() {
return new A;
}
}
3) File main.cpp
#include<iostream>
#include<dlfcn.h>
#include "hello.h"
using namespace std;
int main() {
void *handle;
handle = dlopen("./libhello.so", RTLD_LAZY | RTLD_GLOBAL);
if (!handle) {
cout << "The error is " << dlerror();
}
return 0 ;
}
I am using following steps to create the shared library
1) g++ -g -fPIC -c hello.cc
2) g++ -g -shared -o libhello.so hello.o
3) g++ -g -o myprog main.cpp -
main.cpp:(.text+0x18): undefined reference to `A::A()' . The function createA in hello.h is declared so the same can be used to dlsym
- I am unable to use createA in dlsym
- I am getting undefined reference to `A::A() even If I do not use dlsym
- My query is what is correct was to use C++ class objects in dlsym
- From man of dlopen I am not able to infer what is significance of RTLD_LAZY RTLD_GLOBAL RTLD_NOW flags