Build Succeeded, but no .lib file gets created

Viewed 78608

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!

13 Answers

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.

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.

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

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;

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.

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.

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!

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 {};

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!

Related