简体   繁体   English

Visual Studio - C++ - 以 32 位或 64 位形式构建和运行应用程序 - 从适当的 ProgramFiles 目录加载 DLL

[英]Visual Studio - C++ - build and run app as 32bit or 64bit - load DLL from appropripate ProgramFiles dir

I build my app either as x86 or x64.我将我的应用程序构建为 x86 或 x64。 This app uses external DLL.此应用程序使用外部 DLL。

I have x64 system (Windows 10) with the same DLL library installed for both platforms - x86 and x64.我有 x64 系统(Windows 10),并为两个平台(x86 和 x64)安装了相同的 DLL 库。 They are placed in same folders inside appropriate Program Files directory.它们被放置在适当的 Program Files 目录中的相同文件夹中。 I can manually set path to either one in environment variable PATH and it woks.我可以手动将路径设置为环境变量 PATH 中的任何一个,它就可以了。 But it is a little incovinient to rewrite PATH and reset computer when I switch platform and want to test the other one.但是当我切换平台并想测试另一个平台时,重写PATH并重置计算机有点不方便。 Is there any solution, how system automatically loads correct DLL from correct Program Files dir?有什么解决方案,系统如何从正确的程序文件目录中自动加载正确的 DLL?

Copy the DLL in the build location next to the executable, for Visual Studio this is typically \\Debug or \\Release in a architecture dependent sub directory (ie x86 or x64).复制可执行文件旁边的构建位置中的 DLL,对于 Visual Studio,这通常是体系结构相关子目录(即 x86 或 x64)中的\\Debug\\Release Just make sure the target / output locations are set correctly in the project settings.只需确保在项目设置中正确设置了目标/输出位置。

You have to copy only once, or more correctly: each time after you 'clean' the solution.您只需复制一次,或更准确地说:每次“清洁”解决方案后。 To make this easier, many people use a dll-copy script (use batch, ruby or python) and have it run automatically before building or after cleaning.为了使这更容易,许多人使用 dll 复制脚本(使用批处理、ruby 或 python)并让它在构建之前或清理之后自动运行。 You can execute the script in a pre-build step or post-build step that can be configured in your Visual Studio solution or project settings.您可以在可在 Visual Studio 解决方案或项目设置中配置的预构建步骤后构建步骤中执行脚本。

There is also a more robust way to handle build artifacts and peculiarities: I highly recommend the use of CMake to keep the build matrix organized.还有一种更强大的方法来处理构建工件和特性:我强烈建议使用CMake来保持构建矩阵的组织。 It is provides a general cross-platform approach to script pre-build and post-build actions such as tracking dependencies, copying files, packaging installers, deployment, version verification, versioning, etc.. it comes with an easy scripting language so you can build macro's and functions to do your copying.它提供了一种通用的跨平台方法来执行脚本预构建和构建后操作,例如跟踪依赖项、复制文件、打包安装程序、部署、版本验证、版本控制等。它带有简单的脚本语言,因此您可以构建宏和函数来进行复制。 It can be a bit of a learning curve to get it right, but once it's there, it provides a robust dependable way to build out your project build pipeline.正确使用它可能需要一些学习曲线,但是一旦它在那里,它就提供了一种强大可靠的方式来构建您的项目构建管道。

I have found solution for running apps from Visual Studio: How do I set the path to a DLL file in Visual Studio?我找到了从 Visual Studio 运行应用程序的解决方案: 如何在 Visual Studio 中设置 DLL 文件的路径?

It is working as epxected.它正在按预期工作。 If I run app outise Visual Studio, I set PATH variable by myself and is also working.如果我运行 app outise Visual Studio,我自己设置了 PATH 变量并且也在工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM