0

Created an empty c++ in visual studio 2017 added the following files with following C++ methods

//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 for swig

/* 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" 
*/

Added custom action for gfg.i file as below with output file name as gfg_wrap.c

$(SWIG_PATH)\swig.exe -python gfg.i

while compiling gfg.i file, it given two outputs gfg.py and gfg_wrap.c.

then i created Setup.py file with the following contents

# 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

with custom action as

C:\Python37\python_d.exe setup.py build_ext --inplace

this python directory contains swig.exe

after executing this, it generated an _gfg_d.cp37-win_amd64.pyd file in the project directory.

when given import gfg from CMD it shown the following error.

enter image description here

I was trying to access fact method from gfg.h Is there something iam missing out?

2
  • Looks like you've built it for a Python interpreter that's different to the one you're actually running. I can't quite see how from what's written though. Maybe something to do with the python_d.exe being not your system's default Python. Commented Nov 30, 2018 at 17:12
  • python37.dll is used by a Release build of Python, but you're trying to use a Debug build of Python, which is looking for python37_d.dll instead. Run it with python.exe for Python 3.7 and it will work. Commented Dec 2, 2018 at 20:34

1 Answer 1

1

python37.dll is used by a Release build of Python, but you're trying to use a Debug build of Python, which is looking for python37_d.dll instead. Run it with python.exe for Python 3.7 and it will work.

If you want a debug build, use:

setup.py build_ext --debug --inplace

The name of the extension will be _gfg.cp37-win_amd64.pyd (or some variation on another platform). It must be named _gfg.pyd for a release version or _gfg_d.pyd for a debug version. I had to manually rename it to get it to work. I didn't find an option to force the name:

C:\>copy _gfg.cp37-win_amd64.pyd _gfg_d.pyd
        1 file(s) copied.

C:\>python_d
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 05:02:23) [MSC v.1914 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import gfg
>>> gfg.fact(5)
120
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.