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>
extern 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:

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

9
  • stackoverflow.com/questions/145270/calling-c-c-from-python Commented Oct 22, 2019 at 13:45
  • 2
    this is likely a name mangling issue, try making add use C calling convention as in the first comment Commented Oct 22, 2019 at 13:50
  • 1
    In other words: The functions you want to call should be made extern "C". Commented Oct 22, 2019 at 14:31
  • @MaxLanghof I already did it but nothing changed Commented Oct 22, 2019 at 14:40
  • Well with the code you show it can't possibly work (unless you give ctypes the mangled name). Commented Oct 22, 2019 at 14:44

2 Answers 2

3

C++ mangles exported names. Here's an example that should compile on Windows and Linux. __declspec(dllexport) is required to export a function on Windows, but not Linux. extern "C" is required to export the function name using C conventions, instead of the name-mangled C++ conventions. The name-mangling is required in C++ to indicate the function parameters and return type, since C++ can have multiple functions with the same name but take different parameters. The C convention does not support multiple functions with the same name.

fun.cpp:

#ifdef _WIN32
#   define API __declspec(dllexport)
#else
#   define API
#endif

extern "C" API int add(int a, int b) {
    return a+b;
}

Python:

from ctypes import *

dll = CDLL('fun')
result = dll.add(10,1)
print('Addition value:',result)

Output:

Addition value: 11
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you thank you so much you're the best It's working
-1

I think you should check Python's LoadLibrary is calling "fun.dll"'s main() function correctly. It should look for the DllMain() function when loading, then you need to get the function address to call the function.

1 Comment

Can you give me an example please!

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.