Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
wrapping c++ library
#1
Hello,

I want to use some c++ library in python code.
I used scipy dsp(digital signal processing) filter until now, but it was too slow and this part became a bottle neck of my code.
So I'm trying to change it to another c++ library to reduce filtering time.
(Under windows 10, python 3.7.6 32bit environment)

For example, I wanted to use dsp c++ library: https://github.com/berndporr/iir1
(or boost low pass filter. I'll try it later.)
so I'm trying to wrap the code using this libary written in c++ to make pyd as below (LPFtest.cpp).

#include <Python.h>
#include ".\iir1\Iir.h"
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include "path\numpy\core\include\numpy\ndarraytypes.h"
#include "path\numpy\core\include\numpy\ndarrayobject.h"
#include "path\numpy\core\include\numpy\arrayobject.h"

int test()
{

    printf("Hi"); // I'll put my code here to use IIR filter from iir1 library

    return 0;
}

static PyObject* ctest(PyObject* self, PyObject* args)
{
    int reply = test(); // this part of code also will be changed if I can use iir1 library..
    return Py_BuildValue("i", reply);
}

static PyObject* version(PyObject* self)
{
    return Py_BuildValue("s", "Version 0.1");
}

static PyMethodDef myMethods[] = {
    {"test", ctest, METH_VARARGS, "test LPF"},
    {"version", (PyCFunction)version, METH_NOARGS, "return the version."},
    {NULL, NULL, 0, NULL}
};

static struct PyModuleDef LPFtest = {
    PyModuleDef_HEAD_INIT,
    "LPFtest",
    "test",
    -1,
    myMethods
};

PyMODINIT_FUNC PyInit_LPFtest(void)
{
    return PyModule_Create(&LPFtest);
}
and made setup.py as below.

from distutils.core import setup, Extension

module = Extension("LPFtest", sources = ["LPFtest.cpp"])

setup(name = "LPFtest",
      version = "0.1",
      description = "LPFtest",
      ext_modules = [module])
However, if I executed setup.py as python setup.py build, it shows me a following linker error.


LPFtest.obj : error LNK2001: unresolved external symbol "public: __thiscall Iir::ChebyshevI::AnalogLowShelf::AnalogLowShelf(void)" (??0AnalogLowShelf@ChebyshevI@Iir@@QAE@XZ)
LPFtest.obj : error LNK2001: unresolved external symbol "public: __thiscall Iir::ChebyshevI::AnalogLowPass::AnalogLowPass(void)" (??0AnalogLowPass@ChebyshevI@Iir@@QAE@XZ)
...more error LNK2001 and
build\lib.win32-3.7\LPFtest.cp37-win32.pyd : fatal error LNK1120: 19 unresolved externals
error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Professional\\VC\\Tools\\MSVC\\14.21.27702\\bin\\HostX86\\x86\\link.exe' failed with exit status 1120


Should I include or let this code know where is dll or lib file?
Please let me know if you have any idea, or further information.

Thank you in advance!

Best wishes,
JESuh
Reply
#2
This documentation page suggests that you should use further keyword arguments in Extension() to indicate the libraries names, directory and include dirs.
JESuh likes this post
Reply
#3
(Jun-16-2022, 06:07 AM)Gribouillis Wrote: This documentation page suggests that you should use further keyword arguments in Extension() to indicate the libraries names, directory and include dirs.

Dear Gribouillis,

Thank you so much!
I added the following 3 lines in the Extension function in setup.py and it works well now.

include_dirs = [r'full path\iir1'],
libraries = ['iir'],
library_dirs = [r'full path\iir1\lib'],


Thank you so much! :)

Best wishes,
JESuh
Gribouillis likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Wrapping c/c++ dll Oolongtea 2 737 Aug-25-2023, 10:35 PM
Last Post: PyDan
  How to do "fixed size" (wrapping) math in Python? AlexanderWulf 13 1,899 Jul-19-2023, 04:13 PM
Last Post: deanhystad
  xml indent SubElements (wrapping) with multiple strings ctrldan 2 1,492 Jun-09-2023, 08:42 PM
Last Post: ctrldan
  Help Wrapping C Library LeftyGuitar 2 1,855 Oct-06-2019, 08:04 PM
Last Post: micseydel
  wrapping problem ibaad1406 2 2,028 Jun-19-2019, 07:22 AM
Last Post: Gribouillis
  PyInstaller, how to create library folder instead of library.zip file ? harun2525 2 4,815 May-06-2017, 11:29 AM
Last Post: harun2525

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020