Load shared library by path at runtime

后端 未结 4 1162
青春惊慌失措
青春惊慌失措 2020-12-05 05:04

I am building a Java application that uses a shared library written in C++ and compiled for different operating systems. The problem is, that this shared library itself depe

4条回答
  •  生来不讨喜
    2020-12-05 05:39

    Un UNIX/Linux systems you can use dlopen. The issue then is you have to fetch all symbols you need via dlsym

    Simple example:

    typedef int (*some_func)(char *param);
    
    void *myso = dlopen("/path/to/my.so", RTLD_NOW);
    some_func *func = dlsym(myso, "function_name_to_fetch");
    func("foo");
    dlclose(myso);
    

    Will load the .so and execute function_name_to_fetch() from in there. See the man page dlopen(1) for more.

提交回复
热议问题