python - 处理错误 LNK2001:Python 扩展中未解析的外部符号 __imp__ExitWindowsEx@8
问题描述
我正在尝试构建一个 C 项目。我认为来源很好,但是我收到了这个错误:
错误 LNK2001:未解析的外部符号 __imp__ExitWindowsEx@8
或者对于完整的回溯:
C:\Users\Simon\Desktop\Learn>python setup.py build
running build
running build_ext
building 'sys_shutdown' extension
C:\Program Files (x86)\Microsoft Visual Studio\2017\WDExpress\VC\Tools\MSVC\14.14.26428\bin\HostX86\x86\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MT -IC:\Users\Simon\AppData\Local\Programs\Python\Python36-32\include -IC:\Users\Simon\AppData\Local\Programs\Python\Python36-32\include "-IC:\Program Files (x86)\Microsoft Visual Studio\2017\WDExpress\VC\Tools\MSVC\14.14.26428\include" "-IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.6.1\include\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17134.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17134.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17134.0\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17134.0\winrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17134.0\cppwinrt" /Tcmain.c /Fobuild\temp.win32-3.6\Release\main.obj
main.c
C:\Program Files (x86)\Microsoft Visual Studio\2017\WDExpress\VC\Tools\MSVC\14.14.26428\bin\HostX86\x86\link.exe /nologo /INCREMENTAL:NO /LTCG /nodefaultlib:libucrt.lib ucrt.lib /DLL /MANIFEST:EMBED,ID=2 /MANIFESTUAC:NO /LIBPATH:C:\Users\Simon\AppData\Local\Programs\Python\Python36-32\libs /LIBPATH:C:\Users\Simon\AppData\Local\Programs\Python\Python36-32\PCbuild\win32 "/LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio\2017\WDExpress\VC\Tools\MSVC\14.14.26428\lib\x86" "/LIBPATH:C:\Program Files (x86)\Windows Kits\NETFXSDK\4.6.1\lib\um\x86" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\ucrt\x86" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86" /EXPORT:PyInit_sys_shutdown build\temp.win32-3.6\Release\main.obj /OUT:build\lib.win32-3.6\sys_shutdown.cp36-win32.pyd /IMPLIB:build\temp.win32-3.6\Release\sys_shutdown.cp36-win32.lib
Creating library build\temp.win32-3.6\Release\sys_shutdown.cp36-win32.lib and object build\temp.win32-3.6\Release\sys_shutdown.cp36-win32.exp
main.obj : error LNK2001: unresolved external symbol __imp__ExitWindowsEx@8
build\lib.win32-3.6\sys_shutdown.cp36-win32.pyd : fatal error LNK1120: 1 unresolved externals
error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\WDExpress\\VC\\Tools\\MSVC\\14.14.26428\\bin\\HostX86\\x86\\link.exe' failed with exit status 1120
我从这里计算出这__imp__ExitWindowsEx@8
是由于找不到必要的运行时引起的。
不要使用#using 链接,而是使用链接器命令行链接。您可以通过将 user32.lib 添加到链接器命令来做到这一点
我在我的设置脚本中尝试了这个extra_objects
:
import setup, Extension
module = Extension(
"sys_shutdown",
sources = ["main.c"],
extra_objects = ["C:\\Program Files (x86)\\Windows Kits\\10\Lib\\10.0.17134.0\\um\\arm64\\User32.Lib"]
)
setup (
name = "sys_shutdown",
version = "1.0",
ext_modules = [module])
我的main.c
文件(所以你知道为什么以及我需要链接到什么):
#include <Python.h>
#include <Windows.h>
/* The functions that need to be created */
static PyObject * sys_shutdown(PyObject *self) {
ExitWindowsEx(EWX_POWEROFF, SHTDN_REASON_MINOR_OTHER); // Shutdown
return Py_BuildValue("");
}
static PyObject * sys_restart(PyObject *self) {
ExitWindowsEx(EWX_REBOOT, SHTDN_REASON_MINOR_OTHER); // Restart
return Py_BuildValue("");
}
static PyObject * sys_log_out(PyObject *self) {
ExitWindowsEx(EWX_LOGOFF, SHTDN_REASON_MINOR_OTHER); // Log out
return Py_BuildValue("");
}
static PyMethodDef allMethods[] = {
{"sys_shutdown", (PyCFunction)sys_shutdown, METH_NOARGS, "Shuts down the device"},
{"sys_restart", (PyCFunction)sys_restart, METH_NOARGS, "Restarts the device"},
{"sys_log_out", (PyCFunction)sys_log_out, METH_NOARGS, "Closes all processes and logs out the user from the device"},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef allModule = {
PyModuleDef_HEAD_INIT,
"System Functions",
"sys_shutdown",
-1,
allMethods
};
PyMODINIT_FUNC PyInit_sys_shutdown(void) {
return PyModule_Create(&allModule);
}
此扩展需要ExitWindowsEx()
提供的功能user32.dll
我怎样才能链接user32.dll
到我的扩展(或者如果我完全错过了情节,我怎样才能让它正确编译)?
解决方案
对,我找到了解决方案。已为系统运行时设置链接器路径。我所要做的就是链接运行时本身。这可以使用library
设置脚本中的属性来完成:
from distutils.core import setup, Extension
module = Extension(
"sys_shutdown",
sources = ["main.c"],
libraries = ["user32"] # <-- Here it is
)
setup (
name = "sys_shutdown",
version = "1.0",
ext_modules = [module])
推荐阅读
- html - CSS 中心 ul 而 li 是文本左对齐
- javascript - 是否可以在 React 中使用流和箭头函数?
- javascript -
全部替换后和在javascript中如何检查字符串是否为空? - javascript - mongoose 上的 save() 函数不起作用
- r - 在 R 中按组使用具有特定样本大小的列表并将其应用于数据集
- excel - 如何从多个文本文件中搜索列表字符串
- apache-flink - 在 RichCoFlatMapFunction 中更新外部数据库
- c# - 在 C# 中的另一个对象内实例化用户定义的对象
- python - 为什么我需要同时从 Crypto 导入 Hash 和从 Crypto.Cipher 导入 PKCS1_OAEP 才能使 Hash.MD5 工作?
- reactjs - 将道具传递给组件:“对象作为 React 子项无效”