简体   繁体   English

无法解析的外部符号针对共享的C库使用swig构建python C扩展

[英]Unresolved external symbols building a python C extension with swig against a shared C library

I'm trying to wrap a C library for python using SWIG. 我正在尝试使用SWIG包装python的C库。 I'm on a linux 64-bit sytem (Gentoo) using the standard system toolchain. 我在使用标准系统工具链的Linux 64位系统(Gentoo)。 The library (SUNDIALS) is installed on my system with shared libraries in /usr/local/lib 该库(SUNDIALS)与/usr/local/lib共享库一起安装在我的系统上

My interface file is simple (to start with) 我的界面文件很简单(开头)

%module nvecserial

%{
#include "sundials/sundials_config.h"
#include "sundials/sundials_types.h"
#include "sundials/sundials_nvector.h"
#include "nvector/nvector_serial.h"
%}

%include "sundials/sundials_config.h"
%include "sundials/sundials_types.h"
%include "sundials/sundials_nvector.h"
%include "nvector/nvector_serial.h"

Given the interface file above, I run 给定上面的接口文件,我运行

$ swig -python -I/usr/local/include nvecserial.i 
$ gcc -O2 -fPIC -I/usr/include/python2.7 -c nvecserial_wrap.c
$ gcc -shared /usr/local/lib/libsundials_nvecserial.so nvecserial_wrap.o -o _nvecserial.so
$ python -c "import nvecserial"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "nvecserial.py", line 28, in <module>
    _nvecserial = swig_import_helper()
  File "nvecserial.py", line 24, in swig_import_helper
    _mod = imp.load_module('_nvecserial', fp, pathname, description)
ImportError: ./_nvecserial.so: undefined symbol: N_VLinearSum

A little digging to double check things shows 一点挖掘仔细检查事情表明

$ objdump -t /usr/local/lib/libsundials_nvecserial.so |grep Linear
0000000000001cf0 g     F .text  00000000000002e4              N_VLinearSum_Serial
$ objdump -t _nvecserial.so |grep Linear
00000000000097e0 l     F .text  0000000000000234              _wrap_N_VLinearSum
000000000000cd10 l     F .text  0000000000000234              _wrap_N_VLinearSum_Serial
0000000000000000         *UND*  0000000000000000              N_VLinearSum
0000000000000000       F *UND*  0000000000000000              N_VLinearSum_Serial

As far as I can tell, N_VLinearSum is a wrapper around N_VLinearSum_Serial (there's a parallel implementation too, so presumably, N_VLinearSum in nvecparallel would wrap N_VLinearSum_Parallel). 据我所知,N_VLinearSum是N_VLinearSum_Serial的包装(也有并行实现,因此nvecparallel中的N_VLinearSum可以包装N_VLinearSum_Parallel)。 Where I'm lost though is what to do next. 我迷路的地方是下一步要做的。 Is this a problem with my interface definition, or a problem with my compilation? 这是我的界面定义问题还是编译问题?

We'll I've got it working by linking in an extra library. 我们将通过链接一个额外的库来使其工作。 It seems libsundials_nvecserial.so and brethren don't contain the symbol N_VLinearSum. 看来libsundials_nvecserial.so和弟兄们不包含符号N_VLinearSum。 The SUNDIALS make process places functions and symbols from sundials_nvector.h into different .so files, somewhat counter intuitively. 该日晷制作工艺场所的功能和符号从sundials_nvector.h成不同的.so文件,有些违反直觉。

For now, I got this working with 现在,我已经与

$ gcc -shared -L/usr/local/lib nvecserial_wrap.o -o _nvecserial.so\
-lsundials_nvecserial -lsundials_cvode
$ python -c "import nvecserial"
$

I'll continue playing around with actual .o files from the source distribution, but considering the intent to distribute the wrapped module eventually using distutils, and that not everyone will have access to the SUNDIALS source on their systems, I'll probably stick with linking in the extra shared library. 我将继续处理源分发中的实际.o文件,但考虑到最终使用distutils分发包装模块的意图,并且并不是每个人都可以访问其系统上的SUNDIALS源,我可能会坚持使用链接到额外的共享库中。

Instead of 代替

gcc -shared /usr/local/lib/libsundials_nvecserial.so nvecserial_wrap.o -o _nvecserial.so

try 尝试

gcc -shared -L/usr/local/lib nvecserial_wrap.o -o _nvecserial.so -lsundials_nvecserial

The -l should be at end otherwise the lib may not be searched for symbols. -l应该在结尾,否则可能不会在lib中搜索符号。 This is explained in the ld man page. ld手册页对此进行了说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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