将我的 DLL 添加到 C++ 中的 Visual Studio 项目中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31596315/
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-08-28 13:59:55  来源:igfitidea点击:

Adding my DLL into a Visual Studio project in C++

c++dllprojectadd-invisual-studio-2015

提问by Reaper9806

I am working on a project that involves making a dynamic link library, so I want to test it in a console app in Visual Studio.

我正在处理一个涉及制作动态链接库的项目,因此我想在 Visual Studio 的控制台应用程序中对其进行测试。

The DLL is also made in Visual Studio, it doesn't have much, just a few functions in it. I'm not sure if I'm just supposed to include the librarys header in the includedirectories panel in Properties, or do something else

DLL 也是在 Visual Studio 中制作的,它没有太多,只有几个函数。我不确定我是否只是应该在Properties包含目录面板中包含库标题,或者做其他事情

A lot of people say I'm supposed to add its corresponding .libfile in the Library or Reference directory, but I'm not sure that VS generates a .libfile alongside the DLL. I'm using VS 2015.

很多人说我应该将其对应的.lib文件添加到 Library 或 Reference 目录中,但我不确定 VS 会.lib在 DLL 旁边生成一个文件。我正在使用 VS 2015。

采纳答案by V-R

I don't have VS in front of me this very moment, but these should be the general steps to set it up:

此刻我面前没有 VS,但这些应该是设置它的一般步骤:

Properties->Linker->Input: your.lib
Properties->Linker->Additional Library Directories: ../your/bin
Properties->General->Compiler->Additional Include Directories: ../your/include

To build your app, the DLL's API headers must be in the include for the compile-time, it's LIB files in the bin for the link-time. Once you have your app EXE, all you need is the DLL to be in the same folder as your EXE when it executes.

要构建您的应用程序,DLL 的 API 标头必须在编译时包含在包含中,在链接时它是 bin 中的 LIB 文件。一旦你有了你的应用程序 EXE,你所需要的只是 DLL 在它执行时与你的 EXE 位于同一个文件夹中。

You might also want to add the dll project and the app project into a common solution in VS and add (right click) Project Dependency from the app to the dll. This ensures correct order of building, assuming you are going to build the dll at all.

您可能还想将 dll 项目和应用程序项目添加到 VS 中的通用解决方案中,并从应用程序添加(右键单击)项目依赖项到 dll。这确保了正确的构建顺序,假设您要构建 dll。

回答by Francisco Chavez-Tejeda

You can also do what I did.

你也可以做我做的。

  1. You can create a Libsdirectory inside of your Solution directory.
  2. You can then place your .DLL files inside of the Libsdirectory or some sub-directory inside of Libs

    • In my case, I added the entire SFML-2.3.2directory in there, which included the source-code, .lib files, and .dll files.
    • I did link up what I could in the project properties, but I used Visual Studio's macros to fill in the path name to the Solution directory. Just in case I wanted to put this in version control and work on it from multiple machines.
  3. Then I opened up the Project's Property Page.

  4. Within the property page, I went to Build Events -> Post-Build Event -> Command Line
  5. Within the Command Line, you can add a copy command that will copy any needed files into the same directory as the executable that will need them.
    • In my case I used: copy "$(SolutionDir)Libs\SFML-2.3.2\bin\*" "$(TargetDir)"
    • I could have written multiple commands to copy just the individual files that I needed, but I had spent a good three hours trying to get SFML to work without actually installing it.
  1. 您可以Libs在解决方案目录中创建一个目录。
  2. 然后,您可以将您的 .DLL 文件放在该Libs目录或某些子目录中Libs

    • 就我而言,我SFML-2.3.2在其中添加了整个目录,其中包括源代码、.lib 文件和 .dll 文件。
    • 我确实在项目属性中链接了我可以链接的内容,但是我使用 Visual Studio 的宏来填写解决方案目录的路径名。以防万一我想把它放在版本控制中并从多台机器上处理它。
  3. 然后我打开了项目的属性页。

  4. 在属性页中,我转到Build Events -> Post-Build Event -> Command Line
  5. 命令行中,您可以添加一个复制命令,该命令将任何需要的文件复制到与需要它们的可执行文件相同的目录中。
    • 就我而言,我使用了: copy "$(SolutionDir)Libs\SFML-2.3.2\bin\*" "$(TargetDir)"
    • 我可以编写多个命令来只复制我需要的单个文件,但是我花了三个小时试图让 SFML 工作而不实际安装它。