visual-studio 构建成功,但没有创建 .lib 文件

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

Build Succeeded, but no .lib file gets created

visual-studiovisual-studio-2008visual-c++

提问by Jim

I inherited a substantial amount of code, including a visual studio project that is supposed to (as best as I can tell) build a .lib file. Visual studio says "... Generating Code... Creating Library... Creating browse information file...", and at the end, it says the build succeeded. In the release/debug folder, it has a bunch of .obj files, but it doesn't have a .lib file. What could I be missing?

我继承了大量代码,包括一个应该(尽我所知)构建 .lib 文件的 Visual Studio 项目。Visual Studio 说“...生成代码...创建库...创建浏览信息文件...”,最后,它说构建成功。在 release/debug 文件夹中,它有一堆 .obj 文件,但它没有 .lib 文件。我可能会错过什么?

Thanks!

谢谢!

回答by Steve Townsend

Open the Project Properties (right-click the project in Solution Explorer, select 'Properties'). Under 'Librarian', check 'Output File' - that's where the output should go.

打开项目属性(在解决方案资源管理器中右键单击项目,选择“属性”)。在“图书管理员”下,选中“输出文件” - 这就是输出应该去的地方。

If this looks right, try dir /s *.libin a suitable subdirectory for your project, to see if you can locate the output library by date and time. If you still can't find it, try a clean rebuild (right click project, select 'Rebuild').

如果这看起来正确,请dir /s *.lib在适合您的项目的子目录中尝试,看看您是否可以按日期和时间找到输出库。如果仍然找不到,请尝试干净重建(右键单击项目,选择“重建”)。

For DLLs, a .Lib file is not created if the DLL exports nothing for external usage. I don't think this applies for static lib builds but I would make sure you are exporting something public from your library project source code.

对于 DLL,如果 DLL 不为外部使用导出任何内容,则不会创建 .Lib 文件。我认为这不适用于静态库构建,但我会确保您从库项目源代码中导出公共内容。

回答by snb

.lib will not get generated if you miss to add prefix __declspec(dllexport) for methods.

如果您错过为方法添加前缀 __declspec(dllexport),则不会生成 .lib。

回答by Grault

My static library contains nothing but two template classes, so I didn't have a .cpp file. This caused Visual Studio 2015 to not output a .lib file. To solve this, I made a file huh.cpp which includes all of the headers.

我的静态库只包含两个模板类,所以我没有 .cpp 文件。这导致 Visual Studio 2015 不输出 .lib 文件。为了解决这个问题,我创建了一个包含所有标题的文件 huh.cpp。

回答by Chuck

I had the same problem, even though I was already using the __declspec(dllexport)function.

即使我已经在使用该__declspec(dllexport)功能,我也遇到了同样的问题。

Your ProjectName.cppfile needs to #include "ProjectName.h". If you don't include the header file then the functions don't get exported. The DLL builds fine, no errors or warnings (at least in VS2017 15.8), but you don't get a LIB file.

您的ProjectName.cpp文件需要#include "ProjectName.h". 如果不包含头文件,则不会导出这些函数。DLL 构建良好,没有错误或警告(至少在 VS2017 15.8 中),但您没有得到 LIB 文件。

Include the header and boom - LIB file is generated. A rookie mistake I'm sure, but everyone has to start learning somewhere.

包括标题和吊杆 - 生成 LIB 文件。我敢肯定,这是一个新手错误,但每个人都必须从某个地方开始学习。

回答by Crubuntu

If the Methods you want to export are in a class, you have to __declspec(dllexport)on the class. Otherwise no .lib will be created.

如果要导出的方法在类中,则必须__declspec(dllexport)在类中。否则不会创建 .lib。

回答by CHendrix

I just ran across this problem as well.

我刚刚也遇到了这个问题。

It was due to using an invalid macro in the output directory definition. In my case, it was enter image description here

这是由于在输出目录定义中使用了无效的宏。就我而言,它是 在此处输入图片说明

when it should have been

应该是什么时候

enter image description here

在此处输入图片说明

I had to blank out the full path in the second screen shot. I had an incorrect macro. I was using MsBuildProjectDirwhen I should have been using MsBuildProjectDirectory. The read-only text box will show the full path (eg: C:\Development\blah\blah\blah\) when the output directory is valid. If the output directory is not valid, you'll get something like the first screenshot.

我不得不在第二个屏幕截图中清除完整路径。我有一个不正确的宏。我用MsBuildProjectDir的时候,我应该一直在使用MsBuildProjectDirectoryC:\Development\blah\blah\blah\当输出目录有效时,只读文本框将显示完整路径(例如:)。如果输出目录无效,您将得到类似于第一个屏幕截图的内容。

回答by Gravitas

In the DLL project, put __declspec(dllexport) beginnings of methods defined in .h and .cpp files.

在 DLL 项目中,将__declspec(dllexport) 放在 .h 和 .cpp 文件中定义的方法的开头。

After all, compile your dll again, so .lib file will be generated and ready for linking.

毕竟,再次编译您的 dll,因此将生成 .lib 文件并准备好链接。

put Class Foo
{
public:
    __declspec(dllexport) int GetFoo() const;

回答by zar

I was exporting a class from the dll but had declared the class inline in .h file. The .cpp file was there but empty. This setup was causing the .lib file to be not generated.

我正在从 dll 导出一个类,但已在 .h 文件中声明了该类内联。.cpp 文件在那里,但为空。此设置导致未生成 .lib 文件。

I moved the implementation of functions to .cpp file and now lib file is generated.

我将函数的实现移到 .cpp 文件中,现在生成了 lib 文件。

This is in VS2019.

这是在 VS2019 中。

回答by Elliot Woods

Had the same issue here with VS2019. In my case I had built a few times with no symbols defined (i.e. cpp files were empty).

VS2019 也有同样的问题。就我而言,我构建了几次没有定义符号(即 cpp 文件是空的)。

After I added symbol definitions into the cpp files I began to notice this issue (no lib file was being generated).

在我将符号定义添加到 cpp 文件后,我开始注意到这个问题(没有生成 lib 文件)。

A simple clean via 'Rebuild all' fixed it. Perhaps if you build whilst there are no symbols defined, something gets cached somewhere that you have an empty product DLL, and you need to clean the solution to reset that cached state.

通过“全部重建”进行的简单清理修复了它。也许如果您在未定义符号的情况下进行构建,则某些内容会在您拥有空产品 DLL 的某处缓存,并且您需要清理解决方案以重置该缓存状态。