简体   繁体   English

我如何在SWIG中包装一个C库,而SWIG通常在C编译期间被链接?

[英]How can I wrap a C-Library in SWIG, which has usually to be linked during C-compilation?

Given a C-library, which has to be linked during compilation if I want to use its functions. 给定一个C库,如果我想使用其功能,则必须在编译过程中将其链接。 I want to access these functions in Python using SWIG. 我想使用SWIG在Python中访问这些函数。 I can only find examples and introductions where C-Code (example.c) is wrapped using SWIG, no method how to wrap a dynamic library (example.so). 我只能找到使用SWIG包装C代码(example.c)的示例和介绍,没有找到如何包装动态库(example.so)的方法。

All you need to do to make the .so (or .a) library case work is to link the library appropriately when you do the compile step of the example build process. 使.so(或.a)库案例工作所需要做的就是在执行示例构建过程的编译步骤时适当地链接库。 You will still have to compile the example_wrap.c that gets generated, this is where you can link against things. 您仍然必须编译生成的example_wrap.c,这是您可以链接到事物的地方。

So modified from the SWIG docs that would be: 因此,从SWIG文档中进行的修改如下:

$ swig -python example.i
$ gcc -O2 -fPIC -c example.c
$ gcc -O2 -fPIC -c example_wrap.c -I/usr/local/include/python2.5
$ gcc -shared example_wrap.o -o _example.so -lmylib.so

In reality you can also skip this at the compile time linker step and use dlopen at runtime instead by injecting some extra code into the Python part of your module that calls dlopen before the shared object from SWIG gets loaded. 实际上,您也可以在编译时链接程序步骤中跳过此步骤,并在运行时使用dlopen ,而不是在加载来自SWIG的共享库之前,在调用dlopen的模块的Python部分中注入一些额外的代码。

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

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