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

问题描述 投票:0回答:1

我目前正在尝试将c ++ dll导入python脚本。现在,我想导入一个只有一个功能的非常简单的dll(用于测试)。当我按照How to create a DLL with SWIG from Visual Studio 2010的详细说明进行工作时,有两个我不太了解的链接错误。

[步骤12之前,一切正常(使用generated _wrapper.cxx的附加预处理器定义,现在绕过strcpy)。一旦我尝试构建项目(所有编译都没有问题),我就会从VS获得此输出:

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.

我正在使用64位系统,并且已安装的python(3.6)是x64版本(虽然没有调试)。

我的.i文件看起来像这样:

%module cube

%{
#include "my_f.cpp"
%}

%include my_f.cpp

并且带有代码的.cpp文件如下:

#include "stdafx.h"
int cubes(const int &a)
{
    return a * a*a;
}
python c++ dll wrapper swig
1个回答
0
投票

我想我找到了解决方案。

因为我没有.h文件,所以有函数的声明,但没有定义。添加头文件或将该函数声明为inline函数都会有所帮助,以解决此编译错误。

© www.soinside.com 2019 - 2024. All rights reserved.