python ctypes在共享库中看不到符号

时间:2019-01-31 05:06:12

标签: shared-libraries ctypes

我可以在共享库中看到符号.c-banner { .material-icons { padding-right: 12px; } /* can also do [class*="__icon"] but may be less predictable */ &__icon { /* rest of the scss */ } }

fact

但是当我这样做

> nm -D libtest.so
                 w __cxa_finalize
000000000000111a T fact
                 w __gmon_start__
                 w _ITM_deregisterTMCloneTable
                 w _ITM_registerTMCloneTable

我明白了

>>> import ctypes
>>> lib = ctypes.CDLL('./libtest.so')
>>> lib.fact

这是我的AttributeError: ./libtest.so: undefined symbol: fact 文件的内容:

test.c

我使用编译共享库

int fact(int n);
int fact(int n) {
  if (n <= 0)
    return 1;
  return n*fact(n-1);
}

我很困惑。我在做什么错了?

1 个答案:

答案 0 :(得分:1)

不知道您的问题出在哪里,因为它对我来说很好:-)

建议您准确地尝试以下成绩单中的步骤,以查看是否存在问题(pax$是我的提示,而不是命令的一部分,您会发现我在明确地使用./来存储文件,以确保没有可能会在您的各种搜索路径中的其他位置使用文件)

pax$ cat ./test.c
int fact(int n) { return (n <= 0) ? 1 : n * fact(n-1); }

pax$ gcc -c -fPIC ./test.c -o ./test.o
pax$ gcc -shared -o ./libtest.so ./test.o
pax$ nm -D ./libtest.so
0000000000201028 B __bss_start
                 w __cxa_finalize
0000000000201028 D _edata
0000000000201030 B _end
0000000000000680 T fact
00000000000006ac T _fini
                 w __gmon_start__
0000000000000528 T _init
                 w _ITM_deregisterTMCloneTable
                 w _ITM_registerTMCloneTable
                 w _Jv_RegisterClasses

pax$ python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> import ctypes
>>> lib = ctypes.CDLL('./libtest.so') ; lib
<CDLL './libtest.so', handle 1131fe0 at 0x7fd4c53cf978>

>>> lib.fact
<_FuncPtr object at 0x7fd4c550de58>

>>> lib.fact(6)
720
相关问题