I am using python ctypes to call a function in c++ from python. Currently, I have the following c++ file:
five.cpp
extern "C" {
int get_five(){
return 5;
}
}
And python file:
five.py
import ctypes
from pathlib import Path
lib = ctypes.CDLL(Path(Path.cwd(),'five.dll').as_posix())
print(lib.get_five())
Which works and prints the number 5 when i run it.
However, as soon as I include any headers in the c++ file, it breaks down. So if I change the file to:
#include <iostream>
extern "C" {
int get_five(){
return 5;
}
}
It breaks, and I get the following error:
FileNotFoundError: Could not find module '...\five.dll' (or one of its dependencies). Try using the full path with constructor syntax.
I am compiling on Windows, with the following command:
g++ -shared five.cpp -o five.dll
I am probably missing something obvious, since I am very new to programming in c++. However, I can't seem to find what it is.
The answer should be this by Mark Tolonen:
Your DLL is dynamically linking to the C++ runtime DLL due to including iostream.h. That DLL isn't found in the DLL search path. Try using
-static
when building your DLL.