最小工作示例 Ctypes 和 cmakes:找不到函数
Minimal working example Ctypes and cmakes: function not found
我正在尝试为 python 中的 运行 c++ 代码创建一个最小的工作示例,同时使用 Cmake 和 Ctypes。
这些是我的文件:
get_five.cpp
extern "C" {
int get_five(){
return 5;
}
}
get_five.py
import ctypes
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
dll_file = os.path.join(dir_path,'get_five.dll')
lib = ctypes.CDLL(dll_file)
print(lib.get_five())
CmakeLists.txt
CMAKE_MINIMUM_REQUIRED( VERSION 3.3 )
PROJECT( Test )
add_library(get_five SHARED get_five.cpp)
要编译和 运行 这段代码,我使用以下命令:
mkdir build
cd build
cmake ..
cmake --build .
然后,我将文件build/debug/test.dll复制到根目录和运行 get_five.py。这会产生以下错误:
AttributeError: function 'get_five' not found
通过命令使用 g++ 进行编译
g++ get_five.cpp -shared -o get_five.dll
工作正常,所以我假设我在 Cmake 部分犯了错误。有什么建议可以让这个 MWE 正常工作吗?
未导出函数 get_five。以下 MWE 工作正常:
get_five.cpp
extern "C" {
__declspec(dllexport) int __cdecl get_five(){
return 5;
}
}
get_five.py
import ctypes
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
dll_file = os.path.join(dir_path,'get_five.dll')
lib = ctypes.CDLL(dll_file)
print(lib.get_five())
CmakeLists.txt
CMAKE_MINIMUM_REQUIRED( VERSION 3.3 )
PROJECT( Test )
add_library(get_five SHARED get_five.cpp)
要编译和 运行 这段代码,我使用以下命令:
mkdir build
cd build
cmake ..
cmake --build .
然后,我将文件build/debug/test.dll复制到根目录和运行 get_five.py.
我正在尝试为 python 中的 运行 c++ 代码创建一个最小的工作示例,同时使用 Cmake 和 Ctypes。
这些是我的文件:
get_five.cpp
extern "C" {
int get_five(){
return 5;
}
}
get_five.py
import ctypes
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
dll_file = os.path.join(dir_path,'get_five.dll')
lib = ctypes.CDLL(dll_file)
print(lib.get_five())
CmakeLists.txt
CMAKE_MINIMUM_REQUIRED( VERSION 3.3 )
PROJECT( Test )
add_library(get_five SHARED get_five.cpp)
要编译和 运行 这段代码,我使用以下命令:
mkdir build
cd build
cmake ..
cmake --build .
然后,我将文件build/debug/test.dll复制到根目录和运行 get_five.py。这会产生以下错误:
AttributeError: function 'get_five' not found
通过命令使用 g++ 进行编译
g++ get_five.cpp -shared -o get_five.dll
工作正常,所以我假设我在 Cmake 部分犯了错误。有什么建议可以让这个 MWE 正常工作吗?
未导出函数 get_five。以下 MWE 工作正常:
get_five.cpp
extern "C" {
__declspec(dllexport) int __cdecl get_five(){
return 5;
}
}
get_five.py
import ctypes
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
dll_file = os.path.join(dir_path,'get_five.dll')
lib = ctypes.CDLL(dll_file)
print(lib.get_five())
CmakeLists.txt
CMAKE_MINIMUM_REQUIRED( VERSION 3.3 )
PROJECT( Test )
add_library(get_five SHARED get_five.cpp)
要编译和 运行 这段代码,我使用以下命令:
mkdir build
cd build
cmake ..
cmake --build .
然后,我将文件build/debug/test.dll复制到根目录和运行 get_five.py.