【问题标题】:Use SWIG for .cpp which depends on another .cpp将 SWIG 用于依赖于另一个 .cpp 的 .c++
【发布时间】: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。这是真的?如果是,我该怎么做?

【问题讨论】:

标签: cmake swig


【解决方案1】:

解决了!

swig -c++ -python file1.i

g++ -Isrc -fPIC -I/../../../../usr/include/python3.6m -I/../../../../usr/include/x86_64-linux-gnu/python3.6m -lpython3.6m -c file1.cpp file2.cpp file1_wrap.cxx

g++ -shared -fPIC -o _file1.pyd file1.o file2.o file1_wrap.o

事实证明,我们可以使用 CMake 来定义依赖项并自动执行此过程。 此外,如 SWIG 文档 Introduction_build_system 中所述,SWIG 可以与 CMake 一起使用

【讨论】:

    猜你喜欢
    • 2023-03-17
    • 2013-01-05
    • 2016-04-28
    • 1970-01-01
    • 1970-01-01
    • 2015-11-14
    • 1970-01-01
    • 2017-05-24
    • 2021-09-12
    相关资源
    最近更新 更多