Python Forum
How can I call a c++ function using python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I call a c++ function using python
#1
I want to know if there's a way to use a c++ function inside a python code

For example, after doing my research I did find this solution using .dll file. But It can't find the function My code:

fun.cpp:

#include <iostream>
__declspec( dllexport ) int add(int a, int b) {
return a+b;
}
int main()
{
std::cout << "Hello World! from C++" << std::endl;
return 0;
}

compiling it using cmd:

g++ fun.cpp -o fun.dll

Calling the function using Python, ctypes:

from ctypes import *
import os
mydll = cdll.LoadLibrary("C:/Users/User/Desktop/ctypes/fun.dll")

result= mydll.add(10,1)
print("Addition value:-"+result)
But I had this error:

Error:
Traceback (most recent call last): File "c:\Users\User.vscode\extensions\ms-python.python-2019.10.41019\pythonFiles\ptvsd_launcher.py", line 43, in main(ptvsdArgs) File "c:\Users\User.vscode\extensions\ms-python.python-2019.10.41019\pythonFiles\lib\python\old_ptvsd\ptvsd__main__.py", line 432, in main run() File "c:\Users\User.vscode\extensions\ms-python.python-2019.10.41019\pythonFiles\lib\python\old_ptvsd\ptvsd__main__.py", line 316, in run_file runpy.run_path(target, run_name='main') File "C:\Python36\lib\runpy.py", line 263, in run_path pkg_name=pkg_name, script_name=fname) File "C:\Python36\lib\runpy.py", line 96, in _run_module_code mod_name, mod_spec, pkg_name, script_name) File "C:\Python36\lib\runpy.py", line 85, in _run_code exec(code, run_globals) File "c:\Users\User\Desktop\ctypes\test.py", line 5, in result= mydll.add(10,1) File "C:\Python36\lib\ctypes__init__.py", line 361, in getattr func = self.getitem(name) File "C:\Python36\lib\ctypes__init__.py", line 366, in getitem func = self._FuncPtr((name_or_ordinal, self)) AttributeError: function 'add' not found
Reply
#2
see: https://docs.python.org/3.7/extending/embedding.html
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I call sys.argv list inside a function, from the CLI? billykid999 3 753 May-02-2023, 08:40 AM
Last Post: Gribouillis
  how to call an object in another function in Maya bstout 0 2,042 Apr-05-2021, 07:12 PM
Last Post: bstout
  In this function y initially has no value, but a call to foo() gives no error. Why? Pedroski55 8 3,417 Dec-19-2020, 07:30 AM
Last Post: ndc85430
  Struggling for the past hour to define function and call it back godlyredwall 2 2,160 Oct-29-2020, 02:45 PM
Last Post: deanhystad
  list call problem in generator function using iteration and recursive calls postta 1 1,863 Oct-24-2020, 09:33 PM
Last Post: bowlofred
  function call at defined system time? Holon 5 3,158 Oct-06-2020, 03:58 PM
Last Post: snippsat
  How to call/read function for all elements in my list in python johnny_sav1992 1 2,040 Jul-27-2020, 04:19 PM
Last Post: buran
  Python: Call function with variabele? Ending in error. efclem 5 2,880 Apr-22-2020, 02:35 PM
Last Post: buran
  How to mock an object that is created during function call? Schlangenversteher 0 1,948 Jan-31-2020, 01:36 PM
Last Post: Schlangenversteher
  Is there a way to search for function call? mtran 2 2,222 Jan-14-2020, 02:07 AM
Last Post: mtran

Forum Jump:

User Panel Messages

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