【问题标题】:undefined symbol while using ctypes使用 ctypes 时未定义的符号
【发布时间】:2019-12-16 06:24:52
【问题描述】:

我正在使用 ctypes 调用 C 代码:

mysum.cpp

//global variables and required functions are defined
int mysum(int ECG_sample) 
{
   int HR;
   ecg_wave_sample = ECG_sample;
   Filter_CurrentECG_sample(&ecg_wave_sample, &ecg_filterout);  
   Calculate_HeartRate(ecg_filterout,&global_HeartRate); // calculate HeartRate
   HR = global_HeartRate;
   return HR;
}

g++ -shared -o mysum.so -fPIC mysum.cpp 用于创建.so 文件

Python 包装器:

libname = '/home/yasaswini/hp2-notebooks/notebooks/Algorithm_testing_on_database/mysum_example  /mysum.so'
libdir = './'
lib=ctl.load_library(libname, libdir)
py_add_one = lib.mysum
py_add_one.restype = ctypes.c_int
py_add_one.argtypes = [ctypes.c_int]
sample = -145
results = py_add_one(sample)

我收到这个错误

 AttributeError Traceback (most recent call last) <ipython-input-75-858227ec99e5> in <module>
  6 # 1. open the shared library
  7 #mylib = ctypes.CDLL(libfile)
  ----> 8 py_add_one = lib.mysum
  9 
  10 # 2. tell Python the argument and result types of function mysum

  ~/anaconda3/lib/python3.7/ctypes/__init__.py in __getattr__(self, name)
   375         if name.startswith('__') and name.endswith('__'):
   376             raise AttributeError(name)
   --> 377         func = self.__getitem__(name)
   378         setattr(self, name, func)
   379         return func

   ~/anaconda3/lib/python3.7/ctypes/__init__.py in __getitem__(self, name_or_ordinal)
  380 
  381     def __getitem__(self, name_or_ordinal):
  --> 382         func = self._FuncPtr((name_or_ordinal, self))
  383         if not isinstance(name_or_ordinal, int):
  384             func.__name__ = name_or_ordinal

  AttributeError: /home/yasaswini/hp2-notebooks/notebooks/Algorithm_testing_on_database /mysum_example/mysum.so: undefined symbol: mysum

有人能指出我在这里到底做错了什么吗?

【问题讨论】:

标签: python ctypes


【解决方案1】:

这可能与 C++ 名称修改有关。

您需要将extern 添加到 mysum.cpp 中的函数定义中,因为您使用的是 C++ (g++)。在你的情况下:

extern "C" int mysum(int ECG_sample) 
{
   ...
}

有关名称修改的更多信息

另见

https://stackoverflow.com/a/11237254/7919597

https://stackoverflow.com/a/34380673/7919597

【讨论】:

    【解决方案2】:

    我使用了here 给出的逻辑。

    .c 文件:

    int simple_function(int ECG_samples) 
    {
        ecg_wave_sample = ECG_samples;
        ECG_ProcessCurrSample(&ecg_wave_sample, &ecg_filterout); 
        QRS_Algorithm_Interface(ecg_filterout);
        printf("HR:%d\n",ecg_wave_sample);
        return global_HeartRate;  
    }
    

    Python 脚本:

    import numpy
    def call_function_with_no_args(libc): 
    ECG_samples = [3,4,6,6,9,11,13,17,18,21,23,24,28,30,33,35,36,36]
    while(1):
    
        for i in range (len(ECG_samples)):
            value = print(libc.simple_function(ECG_samples[i]))
        print()
    return value
    
    if __name__ == "__main__":
    # load the shared library into c types.
        libname = os.path.abspath(os.path.join(os.path.dirname(__file__),
                                           "libclib1.so"))
        LIBC = ctypes.CDLL(libname)
    
    
    call_function_with_no_args(LIBC)
    

    我认为这是与ctypes 合作的最佳方式。它简单易懂。 Makefile 生成 .so 文件。感谢您的帮助。 :-)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-12
      • 1970-01-01
      • 2021-12-13
      • 1970-01-01
      • 2016-03-26
      • 1970-01-01
      • 2018-12-28
      相关资源
      最近更新 更多