如何创建一个扩展夜风通过对已经包裹库? [英] How to create an extension to already wrapped library via SWIG?
问题描述
我有一个图书馆。它通过痛饮挤包。我想创建一个插件来扩展它。插件需要从已经包裹库类运行有类似无效的init(oldT岁);
。图书馆是由Java和C#中使用。现在,这个插件也将在那里被使用。图书馆和插件是独立的动态链接库。如何高大痛饮,我已经有一个 oldT
键入包裹创建插件绑定时?
I have a library. It is wraped via SWIG. I want to create a plugin to extend it. Plugin requires a class from already wrapped library to run having something like void init( oldT old);
. Library is used from Java and c#. Now this plugin also will be used from there. Library and plugin are separate dll's. How to tall SWIG that I already have that oldT
type wrapped when creating binding for plugin?
推荐答案
您正在寻找 %的进口
在你的插件的.i文件。你需要要么有(或假的)原始.i文件从现有的库。
You're looking for %import
in the .i file of your plugin. You'll need to either have (or fake) the original .i file from the existing library.
一个MCVE基于一个简单的头文件针对Java的(但没有具体到Java):
A MCVE targeting Java (but nothing specific to Java) based on a simple header file:
#ifndef EXISTING_H
#define EXISTING_H
struct oldT {
};
#endif
原来的库接口文件:
The original library interface file:
%module existing
%{
#include "existing.h"
%}
%include "existing.h"
使用,在地方,我们可以建立原始库:
With that in place we can build the original library:
swig2.0 -Wall -java existing.i
gcc -Wall -Wextra -shared -o libexisting.so -I/usr/lib/jvm/default-java/include -I/usr/lib/jvm/default-java/include/linux existing_wrap.c
这libexisting.so生成一些Java的 oldT
键入
现在我们写的插件接口文件:
Now we write our plugin interface file:
%module plugin
%import "existing.i"
%{
#include "existing.h"
%}
%inline %{
void plugin_init(struct oldT old) {
printf("Hello\n");
}
%}
这里的关键是使用%的进口
带来的,而不是产生包装code表示要扩展已经包裹在库中的组件
The key thing here is the use of %import
to bring in, but not generate wrapper code for the components already wrapped in the library you want to extend.
同样,我们可以编译如下:
Again we can compile this:
swig2.0 -Wall -java plugin.i
gcc -Wall -Wextra -shared -o libplugin.so -I/usr/lib/jvm/default-java/include -I/usr/lib/jvm/default-java/include/linux plugin_wrap.c
(请注意,您的实际情况下,您需要做反对现有的库的共享库在某些情况下链接此)
(Note that for your real scenario you made need to link this against the shared library of the existing library in some scenarios)
然后对其进行测试我写了一个Java少量的:
Then to test it I wrote a tiny amount of Java:
public class run {
public static void main(String[] argv) {
System.loadLibrary("existing");
System.loadLibrary("plugin");
plugin.plugin_init(new oldT());
}
}
这点我编译和运行:
Which I compiled and ran with:
javac run.java
LD_LIBRARY_PATH=. java run
Hello
这篇关于如何创建一个扩展夜风通过对已经包裹库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!