[英]how to write C++ DLL file for python correctly?
這是我的 test.cpp 文件。(我還使用 makefile 創建 dll 文件)
//#include <iostream>
int foo(int a)
{
if (a > 1)
return a + 1;
else
return a + 2;
}
當我在 python 中運行以下代碼時
import os
import ctypes
lib_bfs = ctypes.WinDLL("D:\\advanced_programming\\HW7\\cpp_part\\libtest.dll")
print(lib_bfs)
一切正常,我從 python 得到這個 output。
<CDLL 'D:\advanced_programming\HW7\cpp_part\libtest.dll', handle 69d00000 at 0x2b7677e6400>
但是當我以這種方式更改我的 test.cpp 時(我只是取消注釋 iostream):
#include <iostream>
int foo(int a)
{
if (a > 1)
return a + 1;
else
return a + 2;
}
我從 python 收到以下錯誤。
Traceback (most recent call last):
File "d:/advanced_programming/HW7/cpp_part/connector.py", line 4, in <module>
lib_bfs = ctypes.WinDLL("D:\\advanced_programming\\HW7\\cpp_part\\libtest.dll")
File "c:\users\ali\appdata\local\programs\python\python38\lib\ctypes\__init__.py", line 373, in __init__
self._handle = _dlopen(self._name, mode)
FileNotFoundError: Could not find module 'D:\advanced_programming\HW7\cpp_part\libtest.dll' (or one of its dependencies). Try using the full path with constructor syntax.
這也是我的依賴項 在此處輸入圖像描述
順便提一下,當我使用 Linux 時,一切都很好。(我只是使用了.so 文件)但是 windows 可能有一些問題
我感謝您的幫助。
感謝@DhirajWishal,我首先在我的 makefile 中添加了以下行。
g++ -std=c++17 -O3 -I./h -g -fPIC -shared ./cpp/test.cpp -o ./libtest.dll -static-libgcc -static-libstdc++
然后我將libwindpthread-1.dll
(位於F:/strawberry/c/bin/
dir 中)放在我的 makefile 旁邊,問題就解決了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.