Hi,
I have the following code which attempts to load and call a function from a .so file:
Code:
1) CTEST.h : the same header which is included in the .so
#ifndef CTEST_H_
#define CTEST_H_
#include <iostream>
#include <stdio.h>
using namespace std;
class CTEST {
public:
CTEST ();
virtual ~CTEST();
int* ctest1(int i);
int* ctest2(int i);
private:
int i;
};
typedef CTEST* create_t();
typedef void destroy_t(CTEST*);
#ifdef __cplusplus
extern "C" {
#endif
extern "C" CTEST* create();
extern "C" void destroy(CTEST* p);
#ifdef __cplusplus
}
#endif
#endif /*CTEST_H_*/
2) soprog.cpp: program that loads loadctest.so file and tries to call the method ctest2():
Code:
#include <dlfcn.h>
#include "CTEST.h"
int main(int argc, char **argv)
{
void *lib_handle;
int x = 5;
char *error;
lib_handle = dlopen("/usr/local/lib/libctest.so", RTLD_LAZY);
if (!lib_handle)
{
cout<<dlerror()<<endl;
exit(1);
}
create_t* create_ctest = (create_t*) dlsym(lib_handle, "create");
if ((error = dlerror()) != NULL)
{
cout<<dlerror()<<endl;
exit(1);
}
destroy_t* destroy_ctest = (destroy_t*) dlsym(lib_handle, "destroy");
CTEST* tobj = create_ctest();
int* retval = tobj->ctest2(x);
//cout<<*retval<<endl;
destroy_ctest(tobj);
dlclose(lib_handle);
return 0;
}
The problem I have been facing is the following call gets called:
CTEST* tobj = create_ctest();
As it shows the messages it has in its constructor, thus validating the correct .so loaded and method called.
But whenever I am trying to call the ctest2() method in the following line:
int* retval = tobj->ctest2(x);
The ld is giving the following error message:
soprog.cpp
.text+0x200): undefined reference to `CTEST::ctest2(int)'
collect2: ld returned 1 exit status
Can somebody help me in this?
Thanks a lot.
-PD