MinGW编译器/链接器为什么需要正确设置运行时库路径?

huangapple go评论61阅读模式
英文:

Why does MinGW compiler/linker requires runtime library path to be properly set up?

问题

我正在Windows上构建一个CMake项目,使用MSYS2/MinGW和VSCode,其中包括链接到它们的库和可执行文件。
在构建时没有问题,但是尝试运行可执行文件时,由于系统找不到所需的库,我会得到“缺少*.dll”错误。

当我在Linux机器上构建相同的代码时,所需的库路径被“嵌入”到可执行文件中,即我可以将它移动并从不同的路径执行('ldd'列出了依赖项的绝对路径)。Windows为什么行为不同?

更重要的是,是否有一种方法可以在不将自定义项目构建位置添加到PATH变量的情况下解决这个问题?

英文:

I'm building a CMake project in Windows, using MSYS2/MinGW and VSCode, which includes both libraries and executables that link to them.
There no issues while building, however when attempting to run the executable I get "missing *.dll" errors as the shell because the required libraries can't be found by the system.

When I build the same code in a Linux machine, the required library paths are "baked" into the executable i.e. I can move it and execute from different paths ('ldd' lists the dependency's absolute path). Any particular reason why Windows behaves differently?

And more importantly, is there a way to solve this without adding custom project build locations to the PATH variable?

mingw-w64-x86_64-g++

答案1

得分: 1

.dll.exe 文件可能依赖于其他 .dll 文件。

如果运行你的 .exe 文件,它将寻找依赖项,如果找不到任何依赖的 .dll 文件,它将无法启动。通常首先在与 .exe 文件相同的文件夹中搜索,如果在PATH环境变量中指定的位置中找不到,则会继续搜索。

因此,最干净的解决方案是将依赖项复制到相同的文件夹中。

一个可以做到这一点的工具是 copedeps -r,它可以从 https://github.com/brechtsanders/pedeps 获取。-r 用于递归复制,以便获取依赖的 .dll 文件等等...

英文:

.dll and .exe files may depend on other .dll files.

If you run your .exe file it will look for the dependencies and fail to start if any of the dependency .dll files can't be found. It usually searches in the same folder as the .exe first, and if not found in the locations specified in the PATH environment variable.

So the cleanest solution is to copy dependencies to the same folder.

One tool that can do this is copedeps -r from https://github.com/brechtsanders/pedeps
The -r is to copy recursively so you also get the .dll files .dll depend on etc...

huangapple
  • 本文由 发表于 2023年6月26日 06:41:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76552682.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定