【发布时间】:2020-03-03 09:11:30
【问题描述】:
我有一个 .cpp 文件,它调用另一个 .cpp 文件。我想使用 SWIG 为此创建一个 Python 包装器。 我如何使用 SWIG 制作这个。 当我有一个 .cpp 文件时,我可以通过以下方式创建一个 .so:
//app.cpp
#include "app.hpp"
int p(int a)
{
std::cout<<"hello...SWIG runs fine!"<<std::endl;
return a;
}
//app.hpp
#include <iostream>
int p(int a);
//app.i
%module app
%{
#include "app.hpp"
%}
%include "app.hpp"
运行的命令是:
swig -c++ -python app.i
g++ -Isrc -fPIC -I/../../../../usr/include/python3.6m -I/../../../../usr/include/x86_64-linux-gnu/python3.6m -lpython3.6m -c app.cpp app_wrap.cxx
g++ -shared -fPIC -o _app.so app.o app_wrap.o
我用这种方法成功生成了一个.so文件。但是,现在我的 app.cpp 需要使用另一个 .cpp 文件中定义的函数(hello.cpp 中称为 int fn1(int x) 的函数)。我现在如何生成 .so?如果有人能提供一个小例子,那就太好了!
编辑::有人建议我必须同时使用 SWIG 和 CMAKE。这是真的?如果是,我该怎么做?
【问题讨论】:
-
你应该看看github.com/Mizux/cmake-swig(仍在进行中)