【发布时间】:2018-11-29 05:56:44
【问题描述】:
在visual studio 2017 中创建了一个空的 c++
使用以下 C++ 方法添加了以下文件
//gfg.c
#include <stdio.h>
#include <math.h>
//our header file
#include "gfg.h"
#define ll long long
double myvar = 3.4;
// calculate factorial
ll int fact(ll int n)
{
if (n <= 1)
return 1;
else
return (n * fact(n - 1));
}
//find mod
int my_mod(int n, int m)
{
return(n % m);
}
//gfg.h
#pragma once
long long int fact(long long int n);
int my_mod(int n, int m);
//gfg.i 用于痛饮
/* file : gfg.i */
/* name of module to use*/
%module gfg
%{
/* Every thing in this file is being copied in
wrapper file. We include the C header file necessary
to compile the interface */
#include "gfg.h"
/* variable declaration*/
double myvar;
%}
/* explicitly list functions and variables to be interfaced */
double myvar;
long long int fact(long long int n1);
int my_mod(int m, int n);
/* or if we want to interface all functions then we can simply
include header file like this -
%include "gfg.h"
*/
为 gfg.i 文件添加了自定义操作,如下所示,输出文件名为 gfg_wrap.c
$(SWIG_PATH)\swig.exe -python gfg.i
在编译 gfg.i 文件时,它给出了两个输出 gfg.py 和 gfg_wrap.c。
然后我创建了Setup.py 文件,内容如下
# File : setup.py
from distutils.core import setup, Extension
#name of module
name = "gfg"
#version of module
version = "1.0"
# specify the name of the extension and source files
# required to compile this
ext_modules = Extension(name='_gfg',sources=["gfg.i","gfg.c"])
setup(name=name,
version=version,
ext_modules=[ext_modules])
#C:\Python37\python_d.exe setup.py build_ext --inplace
自定义操作为
C:\Python37\python_d.exe setup.py build_ext --inplace
这个python目录包含swig.exe
执行此操作后,它在项目目录中生成了一个_gfg_d.cp37-win_amd64.pyd 文件。
当从 CMD 给定 import gfg 时,它显示以下错误。
我试图从gfg.h 访问fact 方法我错过了什么吗?
【问题讨论】:
-
看起来你已经为 Python 解释器构建了它,它与你实际运行的解释器不同。不过,我不能从所写的内容中看出如何。可能与 python_d.exe 不是您系统的默认 Python 有关。
-
python37.dll 由 Python 的 Release 版本使用,但您正在尝试使用 Python 的 Debug 版本,它正在寻找 python37_d.dll。对于 Python 3.7 使用
python.exe运行它,它会工作。