C++ 有.dll文件和头文件时如何制作.lib文件

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

How to make a .lib file when have a .dll file and a header file

c++dllheaderfunction

提问by hde

I am trying to create an application in visual studio that will be able to access a .dll file that already exists. I need the application to call up routines. I also have a header file that already exists.

我正在尝试在 Visual Studio 中创建一个应用程序,该应用程序将能够访问已存在的 .dll 文件。我需要应用程序来调用例程。我还有一个已经存在的头文件。

I have been researching on the internet and have found that I need to create a .lib file. Looking at similar questions on here I found a link: http://support.microsoft.com/kb/131313I cannot however follow the directions.

我一直在互联网上研究,发现我需要创建一个 .lib 文件。在这里查看类似问题时,我找到了一个链接:http: //support.microsoft.com/kb/131313但是我无法按照说明进行操作。

The information in the link says to make a DEF file ( I read elsewhere that this needs to be compiled as a DLL with the same name, but not sure what that name is, the same name as the .dll file?). But I do not understand the first direction, to 'Use DUMPBIN /EXPORTS'. I then need to 'stub out' functions, and then something to do with .OBJ files (I do not know what these files are).

链接中的信息说要制作一个 DEF 文件(我在别处读到这需要编译为同名的 DLL,但不确定该名称是什么,与 .dll 文件同名?)。但我不明白第一个方向,即“使用 DUMPBIN /EXPORTS”。然后我需要“存根”函数,然后处理 .OBJ 文件(我不知道这些文件是什么)。

Are there any step-by-step directions, similar to the link above, that are easy to follow?

是否有任何易于遵循的分步说明,类似于上面的链接?

回答by Sany Liew

You're going to need Microsoft Visual C++ 2010 Express(or any other source of MSVC command line tools), and your DLL.

您将需要Microsoft Visual C++ 2010 Express(或任何其他 MSVC 命令行工具来源)和 DLL。

Steps:

脚步:

  1. dumpbin /EXPORTS yourfile.dll > yourfile.exports
  2. Paste the names of the needed functions from yourfile.exportsinto a new yourfile.deffile. Add a line with the word EXPORTSat the top of this file.
  3. Run the following commands from VC\bindirectory (the one where lib.exeand other compile tools reside).
  1. dumpbin /EXPORTS yourfile.dll > yourfile.exports
  2. 将所需函数的名称粘贴yourfile.exports到新yourfile.def文件中。EXPORTS在此文件的顶部添加一行带有单词的行。
  3. VC\bin目录(lib.exe和其他编译工具所在的目录)运行以下命令。

 

 

 vcvars32.bat

 lib /def:yourfile.def /out:yourfile.lib

or for x64 builds

或 x64 版本

 lib /def:yourfile.def /machine:x64 /out:yourfile64.lib

You should get two files generated: yourfile.liband yourfile.exp

您应该生成两个文件:yourfile.libyourfile.exp

回答by Desu_Never_Lies

You can use Digital Mars's IMPLIBtool. It can create a lib file using only the dll, without any need for a .def file.

您可以使用 Digital Mars 的IMPLIB工具。它可以仅使用 dll 创建一个 lib 文件,而无需任何 .def 文件。

The download link is http://ftp.digitalmars.com/bup.zip.

下载链接是http://ftp.digitalmars.com/bup.zip

The command line is:

命令行是:

implib.exe /s mydll.lib mydll.dll

回答by ScotthomasPrime

I might have the answer. I did this when I was creating a .exe console application that needed a .dll file. I ran into the problem as well. When I tried the IMPLIB application, it couldn't find any export files. You need to add an #ifdef FILENAME_EXPORTS(replace FILENAME with your .dll file name) and create an _API. Here is the code for the #ifdefexport api commands:

我可能有答案。我在创建需要 .dll 文件的 .exe 控制台应用程序时这样做了。我也遇到了这个问题。当我尝试使用 IMPLIB 应用程序时,它找不到任何导出文件。您需要添加一个#ifdef FILENAME_EXPORTS(用您的 .dll 文件名替换 FILENAME)并创建一个_API. 这是#ifdef导出 api 命令的代码:

#ifdef FILENAME_EXPORTS
#define FILENAME_API __declspec(dllexport)
#else
#define FILENAME_API __declspec(dllimport)
#endif

Now that you have the Export API defined, you need to apply it to all the functions in your header file in the .dll project. For example:

现在您已经定义了导出 API,您需要将它应用到 .dll 项目的头文件中的所有函数。例如:

void FILENAME_API function();

Declare your export functions normally, but include the API between the declarer type and the function name.

正常声明您的导出函数,但在声明器类型和函数名称之间包含 API。

For defining the function in the .cpp file in the .dll project, you don't need the API in the declaration.

在.dll 项目的.cpp 文件中定义函数,不需要声明中的API。

Here is an example of filename.h and filename.cppwith all the code.

这是文件名.h 和filename.cpp的示例,其中包含所有代码。

// Code for filename.h
#pragma once

// Define your Export API
#ifdef FILENAME_EXPORTS
#define FILENAME_API __declspec(dllexport)
#else
#define FILENAME_API __declspec(dllimport)
#endif

// Declare your functions with your API

void FILENAME_API function1();
void FILENAME_API function2();
void FILENAME_API function3();

-------------------------------------------------------------------------------------------
// Code for filename.cpp
#include <iostream>
#include "pch.h"
#include "filename.h"
using namespace std;

void function1()
{
    cout << "Hello Function1!";
}

void function2()
{
    cout << "Hello Function2!";
}

void function3()
{
    cout << "Hello Function3!";
}

Now when you compile the project, you should see the .dll, .lib, and .exp files in the folder where the compiled files are saved to. Now you can link the .exe file with the .lib file. You're Welcome!

现在,当您编译项目时,您应该会在保存编译文件的文件夹中看到 .dll、.lib 和 .exp 文件。现在您可以将 .exe 文件与 .lib 文件链接起来。别客气!

回答by SridharKritha

Instead of creating .def, you can create .lib file from .dll file by exporting the functions / classes defined in the .dll file by __declspec(dllexport) which were referred in the application code.

您可以通过导出应用程序代码中引用的 __declspec(dllexport) 在 .dll 文件中定义的函数/类,而不是创建 .def,从 .dll 文件创建 .lib 文件。

For example (Pseudo code)

例如(伪代码)

PROJECT for creating X.dll file (say, X is a dll name):

用于创建 X.dll 文件的项目(例如,X 是一个 dll 名称):

A.h:

啊:

// Function declaration
__declspec(dllexport) void  foo(void);

A.cpp:

A.cpp:

// Function definition 
#include <A.h>
void foo(void) {
; // definition
}

If you build the above dll project in Visual Studio then the compiler will generate X.dlland also X.lib[which has exported function fooby __declspec(dllexport) ].

如果您在 Visual Studio 中构建上述 dll 项目,那么编译器将生成X.dllX.lib[已通过 __declspec(dllexport)导出函数foo]。

App.cpp:

应用程序.cpp:

// Load time dynamic linking:
// Application should include X.lib (not X.dll) in the project setting
 #include <A.h>
 int main() {
 foo();
 return 0;
}

For further study please refer the following links for better understanding:

如需进一步研究,请参阅以下链接以获得更好的理解:

http://www.codeproject.com/Articles/28969/HowTo-Export-C-classes-from-a-DLL#CppMatureApproach

http://www.codeproject.com/Articles/28969/HowTo-Export-C-classes-from-a-DLL#CppMatureApproach

http://msdn.microsoft.com/en-us/library/ms686923(v=vs.85).aspx

http://msdn.microsoft.com/en-us/library/ms686923(v=vs.85).aspx