|
10 | 10 | // Author: Christian Heimes <christian(at)cheimes(dot)de>
|
11 | 11 |
|
12 | 12 | #include "pynetclr.h"
|
| 13 | +#include "stdlib.h" |
13 | 14 |
|
14 | 15 | #ifndef _WIN32
|
15 | 16 | #include "dirent.h"
|
| 17 | +#include "dlfcn.h" |
| 18 | +#include "libgen.h" |
| 19 | +#include "alloca.h" |
16 | 20 | #endif
|
17 | 21 |
|
18 |
| -#if PY_MAJOR_VERSION > 2 |
19 |
| -#include <stdlib.h> |
20 |
| -#endif |
21 | 22 |
|
22 | 23 | // initialize Mono and PythonNet
|
23 | 24 | PyNet_Args* PyNet_Init(int ext) {
|
@@ -97,6 +98,26 @@ void main_thread_handler (gpointer user_data) {
|
97 | 98 | MonoObject *exception = NULL;
|
98 | 99 |
|
99 | 100 | #ifndef _WIN32
|
| 101 | + // Get the filename of the python shared object and set |
| 102 | + // LD_LIBRARY_PATH so Mono can find it. |
| 103 | + Dl_info dlinfo = {0}; |
| 104 | + if (0 != dladdr(&Py_Initialize, &dlinfo)) { |
| 105 | + char* fname = alloca(strlen(dlinfo.dli_fname) + 1); |
| 106 | + strcpy(fname, dlinfo.dli_fname); |
| 107 | + char* py_libdir = dirname(fname); |
| 108 | + char* ld_library_path = getenv("LD_LIBRARY_PATH"); |
| 109 | + if (NULL == ld_library_path) { |
| 110 | + setenv("LD_LIBRARY_PATH", py_libdir, 1); |
| 111 | + } else { |
| 112 | + char* new_ld_library_path = alloca(strlen(py_libdir) |
| 113 | + + strlen(ld_library_path) |
| 114 | + + 2); |
| 115 | + strcpy(new_ld_library_path, py_libdir); |
| 116 | + strcat(new_ld_library_path, ":"); |
| 117 | + strcat(new_ld_library_path, ld_library_path); |
| 118 | + setenv("LD_LIBRARY_PATH", py_libdir, 1); |
| 119 | + } |
| 120 | + } |
100 | 121 |
|
101 | 122 | //get python path system variable
|
102 | 123 | PyObject* syspath = PySys_GetObject("path");
|
|
0 commit comments