Click here to Skip to main content
14,937,163 members
Please Sign up or sign in to vote.
4.56/5 (2 votes)
See more:
I have a .so and header which has a class that all member of that are virtual
and I want to use the class and its functions in my main

I try following but it gives me segmentation fault

I haven't access to .so to change it

What I have tried:

CLASS_NAME * i;
void * h = dlopen("./LIBNAME.so",RTLD_NOW);
i = (CLASS_NAME *)dlsym(h, "CLASS_NAME");

i->Create(1);//member function
Posted
Updated 27-Aug-18 1:41am
v2
Comments
Richard MacCutchan 27-Aug-18 6:37am
   
Since you have both header and shared object files, why not just use conventional compile and link?
saide_a 27-Aug-18 6:44am
   
for compile I use this command in linux terminal:
g++ -o test test.cpp -ldl
is there something wrong?
Richard MacCutchan 27-Aug-18 7:31am
   
You should also add the shared object library that you want to use, instead of trying to access it dynamically.
saide_a 27-Aug-18 7:39am
   
many thanks for your response,
I used the command in this way also
g++ -o testcan testcan.cpp -L/lib path -lLIBNAME.so
but I got this error :/usr/bin/ld: cannot find -lLIBNAME.so
I added the library path in this way :
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/MyFirstProjects/
but I still get the same error
Richard MacCutchan 27-Aug-18 8:01am
   
Try this (note the colon before LIBNAME.so):
g++ -o testcan testcan.cpp -L/lib path -l:LIBNAME.so

See ld(1): GNU linker - Linux man page[^] for full details of the various linker options.
saide_a 27-Aug-18 8:15am
   
it works , but because all member functions in header are pure virtual , it makes another problem.
this error
cannot allocate an object of abstract type ‘Class_Name’
calss_name.h:note: because the following virtual functions are pure within ‘class_name’:(all member function)

I think I force to use dynamic linking
Richard MacCutchan 27-Aug-18 8:35am
   
You cannot create an object from a class that has all pure virtual methods. And as far as I am aware you cannot create a library from such a class, so I have no idea what this library contains.
saide_a 28-Aug-18 1:25am
   
using this command I got the return object function
nm -D LIB.so
but now after get the object that has none null value
on each function call I am getting segmentation fault.
could you please help me?
Richard MacCutchan 28-Aug-18 3:58am
   
No, I cannot help you because I have no idea what this library is supposed to contain or how its classes are designed. You need to contact the person or people who produced this library for assistance.
saide_a 29-Aug-18 0:32am
   
thanks for your giudes.
My Problem Solved, I hadn't used the parentheses in "i = geti()" line
CLASS_NAME * (geti)(void) = NULL;
CLASS_NAME * i;
void * h = dlopen("./LIBNAME.so",RTLD_NOW);
geti = (CLASS_NAME* (*)())dlsym(h, "CLASS_NAME");
i = geti();
i->Create(1);
Rick York 27-Aug-18 10:30am
   
That means you must derive a class from that one before you can instantiate it and you have to provide implementations of the pure virtual methods declared.

1 solution

You will get a segmentation fault when i is NULL. Check if loading the library and getting the symbol was successful:
CLASS_NAME * i = NULL;
void * h = dlopen("./LIBNAME.so",RTLD_NOW);
// Get error message string upon errors
char *err = dlerror();
if (!h)
{
    // Report error here.
}
else
{
    i = (CLASS_NAME *)dlsym(h, "CLASS_NAME");
    if ((err = dlerror()) != NULL)
    {
        // Report error here.
    }
    else if (i)
    {
        i->Create(1);
    }
}
See also dlopen(3) - Linux man page[^].

Is there any specific reason that you are using dynamic loading? If not, just link your application with the library.
   
Comments
saide_a 27-Aug-18 6:48am
   
I get segmentation fault error even if i is not Null
Jochen Arndt 27-Aug-18 7:59am
   
Then it might be due to how the class is implemented in the library. The syntax from your example requires that dlsym() called with a class name returns a newly allocated instance of the object.

See also Dynamic Class Loading for C++ on Linux.
saide_a 27-Aug-18 8:12am
   
No , it haven't function for returning instance :(
CPallini 27-Aug-18 7:20am
   
5.
saide_a 27-Aug-18 7:50am
   
could you help me for link my app to library?, in above I put my using command.
Jochen Arndt 27-Aug-18 8:15am
   
When using the -l option (small letter L), the "lib" prefix and the extension must be omitted and the library must have a matching name.

If the name of the library is libname.so, use one of these:
-lname
-l:libname.so
full_path_to_libname.so
where the first two require the library to be located in one of the directories searched for libraries (defined by the -L option or the LD_LIBRARY_PATH environment variable for shared libraries).
saide_a 27-Aug-18 8:18am
   
if all header functions be pure virtual , can I use static linking?
Jochen Arndt 27-Aug-18 8:28am
   
That is not a linking related problem. Pure virtual functions must be implemented in derived classes.

If all functions are pure virtual it would even make no sense to put the class into a library. Then the only code stored in the library would be the common operations if provided (e.g. constructor, destructor, copy, move). But such can be also placed in the header file; and they are usually with abstract classes.
saide_a 28-Aug-18 3:47am
   
using this command I got the return object function
nm -D LIB.so
but now after getting the object that has none null value
on each function call I am getting segmentation fault.
could you please help me?
Jochen Arndt 28-Aug-18 4:18am
   
To get help you have to show the actually used code and the header file.

Have you linked the library meanwhile and derived your class?
Then there is no need to use nm. All required information is in the header file.
saide_a 29-Aug-18 0:32am
   
thanks for your giudes.
My Problem Solved, I hadn't used the parentheses in "i = geti()" line
CLASS_NAME * (geti)(void) = NULL;
CLASS_NAME * i;
void * h = dlopen("./LIBNAME.so",RTLD_NOW);
geti = (CLASS_NAME* (*)())dlsym(h, "CLASS_NAME");
i = geti();
i->Create(1);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900