Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I load a shared object in C++?

I have a shared object (a so - the Linux equivalent of a Windows dll) that I'd like to import and use with my test code.

I'm sure it's not this simple ;) but this is the sort of thing I'd like to do..

#include "headerforClassFromBlah.h"  int main() {     load( "blah.so" );      ClassFromBlah a;     a.DoSomething(); } 

I assume that this is a really basic question but I can't find anything that jumps out at me searching the web.

like image 726
Ben L Avatar asked Jul 17 '09 08:07

Ben L


People also ask

How do I load a shared library?

Once you've created a shared library, you'll want to install it. The simple approach is simply to copy the library into one of the standard directories (e.g., /usr/lib) and run ldconfig(8). Finally, when you compile your programs, you'll need to tell the linker about any static and shared libraries that you're using.

What is a shared object in C?

c . $ cc -o libfoo.so.1 -G -K pic foo.c. A shared object is an indivisible unit that is generated from one or more relocatable objects. Shared objects can be bound with dynamic executables to form a runable process. As their name implies, shared objects can be shared by more than one application.

How do you call a function in a shared library?

Put your main function's prototype in a . h file and include it in both your main and dynamic library code. With GCC, simply compile your main program with the -rdynamic flag. Once loaded, your library will be able to call the function from the main program.

How do I read .so library?

Instead, they're just placed in an appropriate folder and used automatically by other programs via Linux's dynamic link loader. However, you might be able to read the SO file as a text file by opening it in a text editor like Leafpad, gedit, KWrite, or Geany if you're on Linux, or Notepad++ on Windows.


1 Answers

There are two ways of loading shared objects in C++

For either of these methods you would always need the header file for the object you want to use. The header will contain the definitions of the classes or objects you want to use in your code.

Statically:

#include "blah.h" int main() {   ClassFromBlah a;   a.DoSomething(); }  gcc yourfile.cpp -lblah 

Dynamically (In Linux):

#include <stdio.h> #include <stdlib.h> #include <dlfcn.h> int main(int argc, char **argv) {     void *handle;     double (*cosine)(double);     char *error;     handle = dlopen ("libm.so", RTLD_LAZY);     if (!handle) {         fprintf (stderr, "%s\n", dlerror());         exit(1);     }     dlerror();    /* Clear any existing error */     cosine = dlsym(handle, "cos");     if ((error = dlerror()) != NULL)  {         fprintf (stderr, "%s\n", error);         exit(1);     }     printf ("%f\n", (*cosine)(2.0));     dlclose(handle);     return 0; } 

*Stolen from dlopen Linux man page The process under windows or any other platform is the same, just replace dlopen with the platforms version of dynamic symbol searching.

For the dynamic method to work, all symbols you want to import/export must have extern'd C linkage.

There are some words Here about when to use static and when to use dynamic linking.

like image 187
Salgar Avatar answered Sep 22 '22 10:09

Salgar