简体   繁体   English

C++ DLL 使用 SWIG 和 Visual Studio 2017 导入 python

[英]C++ DLL import in python with SWIG and Visual Studio 2017

I'm currently trying to import a c++ dll into a python script.我目前正在尝试将 c++ dll 导入 python 脚本。 For now I want to import a very simple dll with just one function (for testing).现在我想导入一个非常简单的 dll 和一个 function (用于测试)。 As I worked my way through the detailed instruction from How to create a DLL with SWIG from Visual Studio 2010 , there are two linking errors I don't really understand.当我通过如何使用 Visual Studio 2010 中的 SWIG 创建 DLL 中的详细说明进行工作时,有两个我不太了解的链接错误。

Everything worked until Step 25 (with an additional preprocessor definition for the generated _wrapper.cxx to bypass strcpy for now).在第 25 步之前一切正常(现在为generated _wrapper.cxx添加了一个额外的预处理器定义以绕过strcpy )。 As soon as I try to build the project (everything compiles without a problem), I get this output from VS:一旦我尝试构建项目(一切编译都没有问题),我从 VS 得到这个 output:

my_f.obj : error LNK2005: "int __cdecl cubes(int const &)" (?cubes@@YAHAEBH@Z) already defined in my_f.obj
Creating library C:\work\example64\my_f\x64\Release\_my_f.lib and object
C:\work\example64\my_f\x64\Release\_my_f.exp
C:\work\example64\my_f\x64\Release\_my_f.pyd : fatal error LNK1169: one or more multiply defined symbols found
Done building project "my_f.vcxproj" -- FAILED.

I'm working with a 64 Bit system and the installed python (3.6) is a x64 version (no debug though).我正在使用 64 位系统,安装的 python (3.6) 是 x64 版本(虽然没有调试)。

My .i file looks like this:我的.i文件如下所示:

%module cube

%{
#include "my_f.cpp"
%}

%include my_f.cpp

And the .cpp file with the code is the following one:带有代码的.cpp文件如下:

#include "stdafx.h"
int cubes(const int &a)
{
    return a * a*a;
}

I think I found a solution for this.我想我找到了解决方案。

Since I hadn't a .h file there was a declaration of the function but no definition.由于我没有.h文件,因此声明了 function 但没有定义。 Either adding an header file or declare the function as an inline function helps, to get around this compiling error.添加 header 文件或将 function 声明为inline function 有助于解决此编译错误。

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

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