尝试编译 MEX 文件时缺少 libkernel32

Posted

技术标签:

【中文标题】尝试编译 MEX 文件时缺少 libkernel32【英文标题】:Missing libkernel32 when trying to compile MEX files 【发布时间】:2021-08-17 16:15:47 【问题描述】:

GCC 版本:10.3.0

我有一个 C 和 C++ 项目,我为此编译 MEX 文件,以便它可以在 MATLAB 程序中运行。它是在 MATLAB 外部而不是在 MATLAB 内部编译的 makefile。

就在几天前,我能够很好地编译和运行这些 MEX 文件。现在(没有任何已知的环境或代码更改),我从链接器中收到了一个缺少 libkernel32 的错误。

这里是编译失败的步骤:(刚才添加的--verbose用于测试)

g++ -m64 -shared -Wl,-Bsymbolic,--verbose -Wl,--no-undefined -Wl,C:/Progra~1/MATLAB/R2019b/extern/lib/win64/mingw64/exportsmexfileversion.def -o testCreate.mexw64 (several .o files linked here) -pthread -LC:/Progra~1/MATLAB/R2019b/bin/win64 -LC:/Progra~1/MATLAB/R2019b/extern/lib/win64/mingw64 -lmex -lmx

(我已将链接的特定 .o 文件注释掉,因为在这种情况下它很长且无关紧要)

链接器在遇到这个障碍之前设法走得很远:

attempt to open C:/Progra~1/MATLAB/R2019b/bin/win64/libkernel32.dll.a failed
attempt to open C:/Progra~1/MATLAB/R2019b/bin/win64/kernel32.dll.a failed
attempt to open C:/Progra~1/MATLAB/R2019b/bin/win64/libkernel32.a failed
attempt to open C:/Progra~1/MATLAB/R2019b/bin/win64/kernel32.lib failed
attempt to open C:/Progra~1/MATLAB/R2019b/bin/win64/libkernel32.lib failed
attempt to open C:/Progra~1/MATLAB/R2019b/bin/win64/libkernel32.dll failed
attempt to open C:/Progra~1/MATLAB/R2019b/bin/win64/kernel32.dll failed
attempt to open C:/Progra~1/MATLAB/R2019b/bin/win64\libkernel32.a failed
attempt to open C:/Progra~1/MATLAB/R2019b/extern/lib/win64/mingw64/libkernel32.dll.a failed
attempt to open C:/Progra~1/MATLAB/R2019b/extern/lib/win64/mingw64/kernel32.dll.a failed
attempt to open C:/Progra~1/MATLAB/R2019b/extern/lib/win64/mingw64/libkernel32.a failed
attempt to open C:/Progra~1/MATLAB/R2019b/extern/lib/win64/mingw64/kernel32.lib failed
attempt to open C:/Progra~1/MATLAB/R2019b/extern/lib/win64/mingw64/libkernel32.lib failed
attempt to open C:/Progra~1/MATLAB/R2019b/extern/lib/win64/mingw64/libkernel32.dll failed
attempt to open C:/Progra~1/MATLAB/R2019b/extern/lib/win64/mingw64/kernel32.dll failed
attempt to open C:/Progra~1/MATLAB/R2019b/extern/lib/win64/mingw64\libkernel32.a failed
attempt to open C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/libkernel32.dll.a failed
attempt to open C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/kernel32.dll.a failed
attempt to open C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/libkernel32.a failed
attempt to open C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/kernel32.lib failed
attempt to open C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw3collect2.exe: error: ld returned 5 exit status

在网上找了some others,他遇到了类似的问题,但一直没找到解决办法。

我不知道 这个 libkernal 应该在哪里(MSYS、MATLAB 或其他地方?)所以我不知道该怎么做。

我尝试过的:

使用pacman -Syu 更新 msys 包 通过pacman -S mingw-w64-x86_64-crt-git重新安装有libkernel32的包(已经安装并且是最新的,重新安装没有修复) 删除指向 pthread 的链接,因为我不相信它已被使用。 (以防它在某种程度上需要 libkernel)- 不高兴。

更新:

添加以下行:-LC:/msys64/mingw64/x86_64-w64-mingw32/lib 已停止立即失败,但链接器随后稍有失败,其中:
attempt to open C:/msys64/mingw64/x86_64-w64-mingw32/lib/libmoldname.dll.a failed
attempt to open C:/msys64/mingw64/x86_64-w64-mingw32/lib/moldname.dll.a failed
attempt to open C:/msys64/mingw64/x86_64-w64-mingw32/lib/libmoldname.a succeeded
C:/msys64/mingw64/x86_64-w64-mingw32/lib/libmoldname.a
attempt to open C:/Progra~1/MATLAB/R2019b/bin/win64/libmingwex.dll.a failed
attempt to open C:/Progra~1/MATLAB/R2019b/bin/win64/mingwex.dll.a failed
attempt to open C:/Progra~1/MATLAB/R2019b/bin/win64/libmingwex.a failed
attempt to open C:/Progra~1/MATLAB/R2019b/bin/win64/mingwex.lib failed
attempt to open C:/Progra~1/MATLAB/R2019b/bin/win64/libmingwex.lib failed
attempt to open C:/Progra~1/MATLAB/R2019b/bin/win64/libmingwex.dll failed
attempt to open C:/Progra~1/MATLAB/R2019b/bin/win64/mingwex.dll failed
attempt to open C:/Progra~1/MATLAB/R2019b/bin/win64\libmingwex.a failed
attempt to open C:/Progra~1/MATLAB/R2019b/extern/lib/win64/mingw64/libmingwex.dll.a failed
attempt to open C:/Progra~1/MATLAB/R2019b/extern/lib/win64/mingw64/mingwex.dll.a failed
attempt to open C:/Progra~1/MATLAB/R2019b/extern/lib/win64/mingw64/libmingwex.a failed
attempt to open C:/Progra~1/MATLAB/R2019b/extern/lib/win64/mingw64/mingwex.lib failed
attempt to open C:/Progra~1/MATLAB/R2019b/extern/lib/win64/mingw64/libmingwex.lib failed
attempt to open C:/Progra~1/MATLAB/R2019b/extern/lib/win64/mingw64/libmingwex.dll failed
attempt to open C:/Progra~1/MATLAB/R2019b/extern/lib/win64/mingw64/mingwex.dll failed
attempt to open C:/Progra~1/MATLAB/R201collect2.exe: error: ld returned 5 exit status
make: *** [Makefile:151: testCreate.mexw64] Error 1

这很奇怪,因为如果您查看之前的文件 (libmoldname),它会正确地在 C:/msys64 中查找,但不会在那里查找这个新的丢失文件。 (并不是说这在过去几年中是必需的,所以不知道为什么我现在需要链接该路径无论如何

编辑:最后一行看起来也坏了(C:/Progra~1/MATLAB/R201collect2.exe),路径错误。视觉错误,还是别的什么?

【问题讨论】:

您是否在其他位置搜索过任何文件? 我使用的是 Windows,但无法安装 Everything 等便捷的搜索工具。有没有一种快速的方法来搜索这些文件可能在哪里? 使用资源管理器导航到一个合理的文件夹,例如C:/msys64,然后在搜索框中输入(如果看不到,请按 F3) 我在 C:/msys64/mingw64/x86_64-w64-mingw32/lib/libkernel32.a 下确实有一个 libkernel32.a(它不在它检查的路径中,来自日志)跨度> 【参考方案1】:

原来问题出在make clean 上。不久前,源代码的一部分被移到了它自己的文件夹中,make clean 没有清理。

里面的 .o 文件很旧,或者完全来自不同的架构,所以造成了混乱。删除它们并重新构建解决了所有问题。

【讨论】:

以上是关于尝试编译 MEX 文件时缺少 libkernel32的主要内容,如果未能解决你的问题,请参考以下文章

MATLAB:mex-compile 上缺少框架 – 架构 x86_64 的未定义符号

尝试在 MatLab 中编译 C mex 文件

mex 文件:编译失败并带有“//”注释;但是在使用“/* */”时编译得很好[重复]

Matlab 缺少依赖项 MEX 文件

Matlab 2017a 无法识别编译器(使用 mex 时出错,未找到支持的编译器或 SDK)

MATLAB + Mex + OpenCV:链接和编译正确,但在运行时找不到库