简体   繁体   English

C ++,Visual Studio 2012,LIB,DLL,包含,源和链接器

[英]C++, Visual Studio 2012, LIBs, DLLs, Includes, Source and Linker

I'm trying to understand what exactly all of these are and how they relate to each other (and most importantly, how to install them). 我试图了解这些是什么以及它们如何相互关联(最重要的是,如何安装它们)。

From what I've read, LIBs are libraries linked during the compilation of my project and DLLs are libraries linked during the runtime of my project. 根据我的阅读, LIB是在项目编译期间链接的库, DLL是在项目运行期间链接的库。

So for me to use a LIB , I have to have the actual .LIB file somewhere in my computer, go to Project -> Properties -> VC++ Directories and add the path to the file in the Library Directories , and after this I have to go to Linker -> Input -> Additional Dependencies add the .lib name in there, and finally I need to type #include in my code, right? 所以对我来说使用LIB ,我必须在我的计算机的某个地方有实际的.LIB文件,转到Project - > Properties - > VC ++ Directories并在Library Directories中添加文件的路径,然后我必须转到链接器 - > 输入 - > 附加依赖项在那里添加.lib名称,最后我需要在我的代码中键入#include ,对吧?

So, some questions: 所以,有些问题:

  1. When I finish and build the release of my program, will the .exe only run if the target platform has the .lib installed in their PC as well? 当我完成并构建我的程序版本时,如果目标平台的PC也安装了.lib,那么.exe只能运行吗? If yes, what steps do I need to do to make sure the .lib goes with the .exe? 如果是,我需要采取哪些步骤来确保.lib与.exe一起使用?

  2. When I get the source of a open source project, if I add them (using Add Existing Item... ) to my project, can I use them just by using #include as if the files were mine and it would be the same as having the .lib installed? 当我获得一个开源项目的源代码时,如果我将它们(使用Add Existing Item ...添加到我的项目中,我是否可以仅使用#include来使用它们,就像文件是我的一样,它将是相同的安装.lib? Or do I need to install the .lib file and still use these source files? 或者我是否需要安装.lib文件并仍然使用这些源文件?

  3. I have a project using OpenGL and I linked to glew32.lib , but I don't have the lib or any new directory added in the VC++ Directories , so I think this means I must've installed the .lib in the system folder or somewhere where the Visual Studio won't ask for another directory, should I worry about this when releasing a project? 我有一个使用OpenGL的项目,我链接到glew32.lib ,但我没有在VC ++目录中添加lib或任何新目录,所以我认为这意味着我必须在系统文件夹中安装.lib或在Visual Studio不会要求另一个目录的地方,在发布项目时我是否应该担心这个问题?

  4. How the above questions relate to DLLs and is there any reason why should I use DLLs over LIBs or the other way around? 以上问题如何与DLL相关 ,是否有任何理由为什么我应该使用DLL而不是LIB?

I'm starting to use more and more libraries and I noticed I just dragged, copied and included it everywhere so I could use them but never really understood how they "fit" in the project. 我开始使用越来越多的库,我注意到我只是拖动,复制并将它包含在任何地方,所以我可以使用它们,但从未真正理解它们如何“适合”项目。 Especially those open source libraries where they provide so many files and I don't really know what to do with them... 特别是那些提供如此多文件的开源库,我真的不知道如何处理它们......

  1. You don't need to have LIB files along with your EXE file for running in another computer, LIB files are static files and DLL files are dynamic. 您不需要将LIB文件与EXE文件一起在另一台计算机上运行,​​LIB文件是静态文件,DLL文件是动态的。 So when you compile all static codes will be included in your EXE file, but DLL files will be loaded and used dynamically in runtime, so you just need to have your DLL files with your EXE file. 因此,当您编译所有静态代码时,将包含在您的EXE文件中,但DLL文件将在运行时动态加载和使用,因此您只需要将您的DLL文件与EXE文件一起使用。 This way, your code will work and run properly in other computers. 这样,您的代码就可以在其他计算机上正常运行。

  2. Just adding another project is not enough, you need to compile them and generate LIB files out of them. 仅添加另一个项目是不够的,您需要编译它们并从中生成LIB文件。 Then you add the generated LIB file to your final project and include external projects in your final binary. 然后将生成的LIB文件添加到最终项目中,并在最终二进制文件中包含外部项目。 If you are compiling multiple projects together in a solution, you'll need to set project build order in solution properties in VS. 如果要在解决方案中一起编译多个项目,则需要在VS中的解决方案属性中设置项目构建顺序。

  3. No, that's OK. 不,那没关系。 It seems you've put LIB files in right folder and you don't need to have LIB file with your EXE file to run it in other computers. 您似乎已将LIB文件放在正确的文件夹中,并且您不需要将LIB文件与EXE文件一起在其他计算机上运行。

  4. DLLs are dynamic libraries, so you need to have them with your application. DLL是动态库,因此您需要将它们与您的应用程序一起使用。 Installers usually install EXE files with DLL files in the same folder, so your app will run properly, but no need to include LIB files at all. 安装程序通常会在同一文件夹中安装带有DLL文件的EXE文件,因此您的应用程序将正常运行,但根本不需要包含LIB文件。

Also you can include LIB files like this: 您还可以包含以下LIB文件:

#pragma comment(lib, "glew32.lib")

So you don't need to do it in project settings, but assuming you have your LIB file in "Library Directories" path. 因此,您不需要在项目设置中执行此操作,但假设您的LIB文件位于“库目录”路径中。

Using DLL files can be done in two ways: One is linking your application to DLL file and having DLL file's function entry in your EXE file's import table: like using 使用DLL文件可以通过两种方式完成:一种是将应用程序链接到DLL文件,并在EXE文件的导入表中包含DLL文件的函数:如使用

#include <windows.h>

then 然后

GetWindowsDirectory(windir, MAX_PATH);

So you'll have GetWindowsDirectory API entry in your EXE file's Import Table . 因此,您将在EXE文件的导入表中输入GetWindowsDirectory API。

Also you can do it dynamically: 你也可以动态地做到这一点:

hinstDLL = LoadLibrary("kernel32.dll");
if (hinstDLL != NULL)
{
    func_GetWindir = (DLLPROC) GetProcAddress(hinstDLL, "GetWindowsDirectoryA");
...

There is not much difference, only difference is: In first method, as it's in your EXE file's Import Table, if there was no kernel32.dll or there was no GetWindowsDirectory entry in kernel32.dll , your EXE will not run at all, it will show a critical error and will not run. 没有太大的区别,唯一的区别是:在第一种方法,因为它是在你的EXE文件的导入表,如果没有kernel32.dll或没有GetWindowsDirectory在进入kernel32.dll ,你的EXE不会跑的话,那将显示严重错误,不会运行。 But in dynamic way (second way), your app will run, but as soon as your code try to use GetWindowsDirectoryA API, it will fail. 但是以动态方式(第二种方式),您的应用程序将运行,但只要您的代码尝试使用GetWindowsDirectoryA API,它就会失败。 You will have 0x00 in func_GetWindir . 你将在func_GetWindir拥有0x00 If you attempt to call it, then program will crash. 如果您尝试调用它,程序将崩溃。

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

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