Build Succeeded, but no .lib file gets created

Visual StudioVisual Studio-2008Visual C++

Visual Studio Problem Overview


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?

Thanks!

Visual Studio Solutions


Solution 1 - Visual Studio

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 *.lib in 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').

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.

Solution 2 - Visual Studio

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

Solution 3 - Visual Studio

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.

Solution 4 - Visual Studio

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

Your ProjectName.cpp file 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.

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

Solution 5 - Visual Studio

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.

Solution 6 - Visual Studio

In my case (Visual Studio 2019), when #include "pch.h" was not the very first include statement in cpp, lib file was not created.

Solution 7 - Visual Studio

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 MsBuildProjectDir when 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.

Solution 8 - Visual Studio

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

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

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

Solution 9 - Visual Studio

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.

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

This is in VS2019.

Solution 10 - Visual Studio

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).

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

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.

Solution 11 - Visual Studio

My issue was that in the projects Properties>C/C++>CommandLine, I had specfied the switch incorrectly. That is instead of writting /D_HASHING_BUILD_DLL I had written /D_Hashing_BUILD_DLL.

Side note:
This is how I build my DLL/Lib files in Visual studio : (and my Hashing.h looks like this: )

#ifndef HASHING_H
#define HASHING_H

/* If we are we on Windows, we want a single define for it.*/
#if !defined(_WIN32) && (defined(__WIN32__) || defined(WIN32) || defined(__MINGW32__))
#define _WIN32
#endif /* _WIN32 */

#if defined(_WIN32) && defined(_HASHING_BUILD_DLL)
/* We are building Hashing as a Win32 DLL */
#define HASHING_API __declspec(dllexport)
#elif defined(_WIN32) && defined(HASHING_DLL)
/* We are calling Hashing as a Win32 DLL */
#define HASHING_API __declspec(dllimport)
#elif defined(__GNUC__) && defined(_HASHING_BUILD_DLL)
/* We are building Hashing as a shared / dynamic library */
#define HASHING_API __attribute__((visibility("default")))
#else
/* We are building or calling HASHING as a static library */
#define HASHING_API
#endif

//your inlcudes

class HASHING_API MyClass
{
//...
};

#endif // !HASHING_H

and in the path I stated earlier, I just use the switch I defined here and there you go, the DLL is built just fine!

Solution 12 - Visual Studio

I'm not so familiar with C++ and I had the same issue. I thought I would share what worked for me. It appears that the import/export must come after the class statement, according to the following page. https://docs.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-1-c4091?view=msvc-160

// Warning C4091
// No error but didn't produce a .lib file
__declspec(dllimport) class X {};

// __declspec attribute after the class or struct keyword
// applies to user defined type worked
class __declspec(dllimport) X3 {};

Solution 13 - Visual Studio

Sometimes when you break you head in a new project why .lib is not created - it can be some "past games" issue. I was creating a new .dll but before I decided to use #define with __declspec(dllexport)/__declspec(dllimport) I tried to play with .def After removing .def file it probably left some misconfiguration in the project file and it wasn't creating .lib file... Till I decided to just remove the project completly and recreate - and walla, works perfectly!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJimView Question on Stackoverflow
Solution 1 - Visual StudioSteve TownsendView Answer on Stackoverflow
Solution 2 - Visual StudiosnbView Answer on Stackoverflow
Solution 3 - Visual StudioGraultView Answer on Stackoverflow
Solution 4 - Visual StudioChuckView Answer on Stackoverflow
Solution 5 - Visual StudioCrubuntuView Answer on Stackoverflow
Solution 6 - Visual StudioSamilView Answer on Stackoverflow
Solution 7 - Visual StudioCHendrixView Answer on Stackoverflow
Solution 8 - Visual StudioGravitasView Answer on Stackoverflow
Solution 9 - Visual StudiozarView Answer on Stackoverflow
Solution 10 - Visual StudioElliot WoodsView Answer on Stackoverflow
Solution 11 - Visual StudioHosseinView Answer on Stackoverflow
Solution 12 - Visual StudioLNWView Answer on Stackoverflow
Solution 13 - Visual StudioDavid ConstantineView Answer on Stackoverflow