如何在 Visual C++ 中构建导入库 (.lib) 和 DLL?

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

How do I build an import library (.lib) AND a DLL in Visual C++?

c++visual-studiodllimport

提问by David Grayson

I want to have a single Visual Studio project that builds a DLL file and an import library (.lib) file. (An import library is a statically-linked library that takes care of loading that DLL file in other projects that use it).

我想要一个单独的 Visual Studio 项目来构建一个 DLL 文件和一个导入库 (.lib) 文件。(导入库是一个静态链接库,负责在使用它的其他项目中加载该 DLL 文件)。

So I went to Visual Studio C++ 2008 Express Edition, created a New Project of type Class Library, and set the "Configuration Type" to be "Dyanamic Library (.dll)".

于是我去了Visual Studio C++ 2008 Express Edition,新建了一个类库类型的新项目,将“配置类型”设置为“动态库(.dll)”。

But when I build the solution, the only relevant output file I see is a DLL file; I don't see any LIB file getting generated. I looked in the project directory and all subdirectories (Release and Debug).

但是当我构建解决方案时,我看到的唯一相关输出文件是一个 DLL 文件;我没有看到生成任何 LIB 文件。我查看了项目目录和所有子目录(发布和调试)。

I believe that it is possible to build a LIB and a DLL file at the same time because on the MSDN it says "The linker creates the import library when the DLL is built."Also, another user of this website is creating LIB and DLL files at the same time using Visual C++.

我相信可以同时构建 LIB 和 DLL 文件,因为在 MSDN 上它说“链接器在构建 DLL 时创建导入库”。此外,该网站的另一个用户正在使用 Visual C++ 同时创建 LIB 和 DLL 文件

So how can I do it?

那么我该怎么做呢?

采纳答案by Joe

By selecting 'Class Library' you were accidentally telling it to make a .Net Library using the CLI (managed) extenstion of C++.

通过选择“类库”,您不小心告诉它使用 C++ 的 CLI(托管)扩展来创建 .Net 库。

Instead, create a Win32 project, and in the Application Settings on the next page, choose 'DLL'.

相反,创建一个 Win32 项目,然后在下一页的“应用程序设置”中选择“DLL”。

You can also make an MFC DLL or ATL DLL from those library choices if you want to go that route, but it sounds like you don't.

如果你想走那条路,你也可以从这些库选择中制作 MFC DLL 或 ATL DLL,但听起来你没有。

回答by Michael Burr

Does your DLL project have any actual exports? If there are no exports, the linker will not generate an import library .lib file.

您的 DLL 项目是否有任何实际导出?如果没有导出,链接器将不会生成导入库 .lib 文件。

In the non-Express version of VS, the import libray name is specfied in the project settings here:

在非 Express 版本的 VS 中,导入库名称在此处的项目设置中指定:

Configuration Properties/Linker/Advanced/Import Library

I assume it's the same in Express (if it even provides the ability to configure the name).

我认为它在 Express 中是相同的(如果它甚至提供配置名称的能力)。

回答by Thom

OK, so I found the answer from http://binglongx.wordpress.com/2009/01/26/visual-c-does-not-generate-lib-file-for-a-dll-project/says that this problem was caused by not exporting any symbols and further instructs on how to export symbols to create the lib file. To do so, add the following code to your .h file for your DLL.

好的,所以我从http://binglongx.wordpress.com/2009/01/26/visual-c-does-not-generate-lib-file-for-a-dll-project/找到了答案说这个问题是由于未导出任何符号引起的,并进一步说明了如何导出符号以创建 lib 文件。为此,请将以下代码添加到 DLL 的 .h 文件中。

#ifdef BARNABY_EXPORTS
#define BARNABY_API __declspec(dllexport)
#else
#define BARNABY_API __declspec(dllimport)
#endif

Where BARNABY_EXPORTS and BARNABY_API are unique definitions for your project. Then, each function you export you simply precede by:

其中 BARNABY_EXPORTS 和 BARNABY_API 是您项目的唯一定义。然后,您导出的每个函数都简单地在前面加上:

BARNABY_API int add(){
}

This problem could have been prevented either by clicking the Export Symbols box on the new project DLL Wizard or by voting yes for lobotomies for computer programmers.

可以通过单击新项目 DLL 向导上的导出符号框或为计算机程序员的脑叶切除术投赞成票来防止此问题。

回答by Chris Long Gue

you also should specify def name in the project settings here:

您还应该在此处的项目设置中指定 def name:

Configuration > Properties/Input/Advanced/Module > Definition File

配置 > 属性/输入/高级/模块 > 定义文件