简体   繁体   English

构建微型 C++ static 库项目后生成的 No.lib 文件

[英]No .lib file generated after building tiny C++ static library project

I decided on adding a tiny extra project to my visual studio solution which includes only a single header file(for now) with a mutex which allows only 1 thread to output to the console at a time.我决定在我的 Visual Studio 解决方案中添加一个额外的小项目,其中仅包含一个 header 文件(目前),其中一个互斥锁一次只允许一个线程到 output 到控制台。 Since this is a functionality which all of my projects in my solution will need so I thought it will be best to add a separate project for it and add references to it on the other projects.由于这是我的解决方案中的所有项目都需要的功能,所以我认为最好为其添加一个单独的项目并在其他项目中添加对它的引用。

So I created an empty project named Commons, then added an header file logger_mutex.h and wrote the following code inside it.所以我创建了一个名为 Commons 的空项目,然后添加了一个 header 文件 logger_mutex.h 并在其中编写了以下代码。

#pragma once

#ifdef _DEBUG
#include <mutex>
std::mutex loggerMutex;
#endif

I changed the project type in properties from Application(.exe) to Static Library (.lib).我将属性中的项目类型从 Application(.exe) 更改为 Static Library (.lib)。 Then I added the include path to this file in the other project properties.然后我在其他项目属性中添加了该文件的包含路径。 Also I added this Commons project as a reference to all my other projects.我还添加了这个 Commons 项目作为对我所有其他项目的参考。 But now when I try to build the solution it gave the error LINK1104 cannot open file../path/to/my/output/directory/Commons.lib但是现在当我尝试构建解决方案时,它给出了错误 LINK1104 cannot open file../path/to/my/output/directory/Commons.lib

I investigated on the output directory and there was no file in there named Commons.lib.我调查了 output 目录,里面没有名为 Commons.lib 的文件。 I tried rebuilding the Commons project separately, and even though visual studio said it built successfully I did not see the Commons.lib file appear on the output directory.我尝试单独重建 Commons 项目,即使 Visual Studio 说它构建成功,我也没有看到 Commons.lib 文件出现在 output 目录中。

I tried it even without the other projects, in a completely different solution.即使没有其他项目,我也尝试过,采用完全不同的解决方案。 It still did not generate any.lib file in the output directory.它仍然没有在 output 目录下生成任何.lib 文件。 I think this should be verifiable as well.我认为这也应该是可验证的。

So what am I missing here, is there some kind of minimum requirement needed to have to get a.lib output file generated?那么我在这里缺少什么,是否需要某种最低要求才能生成 a.lib output 文件? Is my code too small to generate a.lib output?我的代码是否太小而无法生成 a.lib output? I am using Visual Studio 2022.我正在使用 Visual Studio 2022。

Add one empty.cpp file in your lib project and a lib will be generated.在你的 lib 项目中添加一个 empty.cpp 文件,就会生成一个 lib。 But as far as I am concerned, it will be better to #include the logger_mutex.h to other project's pch.h instead as a library.但就我而言,最好将 logger_mutex.h 作为库#include到其他项目的 pch.h 中。

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

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