Jump to content
sosarkar

Dynamically loading a shared library/object (systemc.so)

Recommended Posts

Hi,

 

I have been trying to dynamically load the systemc shared object "systemc.so", but so far without success.

Intention is to virtually simulate the systemc kernel within another application. 

The systemc simulation of the Top module works fine on this own, with the OSCI or questa simulator.

I checked the dependencies using ldd, and I see (the following...). Correct me if I am missing some dependencies? 

 

Can someone provide an example in this regard. I tried the example at (http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html) and was successful.

 

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

ldd ../lib_work/_sc/linux_x86_64_gcc-4.3.3/systemc.so
        linux-vdso.so.1 =>  (0x00007fff7ebf1000)
        libstdc++.so.6 => /edatools/mentor/questasim/10.0c/questasim/gcc-4.3.3-linux_x86_64/lib64/libstdc++.so.6 (0x00002affcfcd3000)
        libm.so.6 => /lib64/libm.so.6 (0x00002affcffea000)
        libgcc_s.so.1 => /edatools/mentor/questasim/10.0c/questasim/gcc-4.3.3-linux_x86_64/lib64/libgcc_s.so.1 (0x00002affd026d000)
        libc.so.6 => /lib64/libc.so.6 (0x00002affd0484000)
        /lib64/ld-linux-x86-64.so.2 (0x0000003f5a000000)
 

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

As a test case, I wrote up the following code to see the dynamic loading.

 

 

#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <systemc>
//#include "libtesttop.h"
#include "top.h"
 
 
int main(int argc, char **argv)
{
   void* lib_handle;
   char* error;
 
   lib_handle = dlopen("../scripts/lib_work/_sc/linux_x86_64_gcc-4.3.3/systemc.so", RTLD_LAZY);
   if (!lib_handle)
   {
      fprintf(stderr, "%s\n", dlerror());
      exit(1);
   }
   //create_t* create_abc = (create_t*) dlsym(lib_handle, "create");
   //destroy_t* destroy_abc = (destroy_t*) dlsym(lib_handle, "destroy");
   Top* (*create)();
   void (*destroy)(Top*);
 
   create = (Top* (*)())dlsym(lib_handle, "create_object");
   destroy = (void (*)(Top*))dlsym(lib_handle, "destroy_object");
   Top* myCosim = (Top*)create();
   myCosim->DoSomething();
   destroy( myCosim );
 
   return 0;
}
 

Share this post


Link to post
Share on other sites

I would expect your example to fail (statically) linking already, because you #include <systemc> already.

 

If you want to have SystemC as a dynamic dependency only, you need to hide this behind a properly defined "plugin interface", as you have sketched with "create_object" and "destroy_object" already.  Due to the C++ name mangling, this plugin interface should be declared as extern "C".

 

That said, two final remarks:

  • All of this is rather off-topic here, as it is not specific to SystemC itself, but to C++ shared libraries and "plugins" in general.
  • You should always include the error messages you're seeing.

Greetings from Oldenburg,
  Philipp

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×