是否有与 -rpath 链接器标志等效的 Windows/MSVC?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/107888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:15:56  来源:igfitidea点击:

Is there a Windows/MSVC equivalent to the -rpath linker flag?

windowsdllsharedrpath

提问by

On Linux/GCC I can use the -rpath flag to change an executables search path for shared libraries without tempering with environment variables.

在 Linux/GCC 上,我可以使用 -rpath 标志来更改共享库的可执行文件搜索路径,而无需调整环境变量。

Can this also be accomplished on Windows? As far as I know, dlls are always searched in the executable's directory and in PATH.

这也可以在 Windows 上完成吗?据我所知,总是在可执行文件的目录和 PATH 中搜索 dll。

My scenario: I would like to put shared libraries into locations according to their properties (32/64bit/Debug/Release) without taking care of unique names. On Linux, this is easily be done via rpath, but I haven't found any way doing this on Windows yet.

我的场景:我想根据共享库的属性(32/64 位/调试/发布)将共享库放入位置,而无需考虑唯一名称。在 Linux 上,这很容易通过 rpath 完成,但我还没有在 Windows 上找到任何方法。

Thanks for any hints!

感谢您的任何提示!

回答by kbluck

Sadly there is no direct analogue to RPATH. There are a number of alternative possibilities, each of them most likely undesirable to you in its own special way.

遗憾的是,没有与 RPATH 的直接类似物。有许多替代的可能性,其中每一种都以自己的特殊方式对您来说很可能是不受欢迎的。

Given that you need a different exe for each build flavor anyway to avoid runtime library clashes, as you might guess the easiest thing to do is to put each exe in the same folder as each set of DLLs.

鉴于无论如何您都需要为每个构建风格使用不同的 exe 以避免运行时库冲突,因为您可能猜到最简单的方法是将每个 exe 与每组 DLL 放在同一文件夹中。

As you also mentioned, the most universal method is to change the PATH variable by using a batch file to bootstrap the exe.

正如您还提到的,最通用的方法是通过使用批处理文件来引导 exe 来更改 PATH 变量。

You could instead change the current working directory before running the program to the desired DLL folder.

您可以改为在将程序运行到所需的 DLL 文件夹之前更改当前工作目录。

You can use the function SetDllDirectoryor AddDllDirectoryinside your exe. This is probably the closest to an RPATH, but only works on WinXP SP1 or later.

您可以在 exe 中使用SetDllDirectoryAddDllDirectory函数。这可能是最接近 RPATH 的,但仅适用于 WinXP SP1 或更高版本。

If you're willing to alter the file name of each exe flavor, you can use the "App Paths" registry key. Each exe would need a unique filename.

如果您愿意更改每个 exe 风格的文件名,您可以使用“App Paths”注册表项。每个 exe 都需要一个唯一的文件名。

回答by ChrisN

The search order for DLLs in Windows is described on this pageon MSDN. If you're using run-time dynamic linking, you can specify the folder when you call LoadLibrary.

MSDN上的此页面描述了 Windows 中 DLL 的搜索顺序。如果您使用运行时动态链接,则可以在调用LoadLibrary.

回答by Adam Mitz

"Isolated applications"is a mechanism for embedding an XML manifest that describes the DLL dependencies.

“隔离应用程序”是一种用于嵌入描述 DLL 依赖项的 XML 清单的机制。