简体   繁体   中英

using ctypes to link c++ and python in linux

I am writing a program in python. now i want to use ctypes to use some functions of a class i have in c++. so basically , i have an array of data in python. i have another program in c++ which is supposed to use that array and transform it to an image class. here is the program i have in c++. i have already made the makefile in linux and it compiles.

the name of the file is 'pclink.cpp'

   #include <stdio.h>
#include "MImage.h"
#include<stdlib.h>

#ifdef __cplusplus
extern "C" {
#endif
  int conv(double* data, int x,int y, int z, int len) {
    MImage M;
    //M.MLoadFromArray(data,x,y,z,len);    

    return 0;

  }
#ifdef __cplusplus
}
#endif

I have also made the .so and .o files every thing seems fine. but when in python i type

  import numpy as np
import ctypes
import os

pclink = np.ctypeslib.load_library('pclink','.')

this is the message i get

Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    pclink=np.ctypeslib.load_library('pclink','.')
  File "/usr/lib/python2.7/dist-packages/numpy/ctypeslib.py", line 131, in load_library
    raise exc
OSError: /home/lucy94/pclink.so: undefined symbol: _ZN6MImage14MLoadFromArrayEPdiiii

previously i tried to link c++ and python with a simple program which does not have any classes and it works fine. it seems the problem is when i try to define an object from another class. so any one knows how to fix the problem? thanks

Looks like your built .so file depends on another shareable library containing the MImage stuff. I don't think Python ctypes will automatically load dependent libraries for you, you have to pull them in yourself. And you'll need to specify the RTLD_GLOBAL flag so the symbols from that MImage library are available to pclink.so when you load that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM