Jim
Jim

Reputation: 2161

Build Succeeded, but no .lib file gets created

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!

Upvotes: 77

Views: 99487

Answers (15)

There a number of reasons why the client project may not be able find or connect to the base project DLL.

  1. The EXPORTS macro is not defined correctly. If this is a Visual Studio generated project, it should have this already correctly defined in the .vcxproj file, something like:
<PreprocessorDefinitions>WIN32;NDEBUG;MYLIB_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>

The key part is having MYLIB_EXPORTS there, as it will get defined.

Perhaps the project was renamed and not all the defines synced up correctly afterwards.

  1. The base project DLL doesn't have the __declspec defined correctly. These are usually defined in the main or 'public' header file that will also be included in the client project. These definitions should be something similar to the following:
#ifdef MYLIB_EXPORTS
#define MYLIB_API __declspec(dllexport)
#else
#define MYLIB_API __declspec(dllimport)
#endif

The first define will be defined for base project DLL itself, as it will find the MYLIB_EXPORTS macro defined in the vcxproj file. This will 'export' the symbols. The second define will be defined when the client projects include the header file in their projects. This will allow finding and using the symbols.

  1. All Classes and functions to be accessed from client projects must be prefixed with macro (in this example: MYLIB_API) like:
class MYLIB_API LibClass
{
public:
    LibClass();
    ~LibClass();
};

MYLIB_API void DoSomething();
  1. The LIB must be output in a directory where the client projects can find it. There a number of ways of going about this, such as explicitly including the base project DLLs output path in the client projects linker input path. I prefer to set all projects to use a common output directly, so I set all projects to the following:
<OutDir>$(SolutionDir)\Bin\$(Configuration)\$(PlatformTarget)\</OutDir>

This can be set from Project Properties -> General -> Output Directory. Make sure that the Configuration and Platform Drop Downs are set to all.

  1. Finally, the LIB must be included in the client project. Right clicking on the client project, choose Properties -> Linker -> Input -> Additional Dependencies. Paste in something like the following:
$(OutDir)MyLib.lib

If all is configured correctly, the linking should go smoothly.

Upvotes: 0

InfDreSta
InfDreSta

Reputation: 21

In my case, I was setup my project in property pages to build dll file in release mode and lib file in debug mode, but I set it importing and exporting dll file with __declspec while in debug mode, so there's a bun of linking and .lib not found error.

Upvotes: 0

David Constantine
David Constantine

Reputation: 587

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!

Upvotes: 0

LNW
LNW

Reputation: 3

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://learn.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 {};

Upvotes: 0

Samil
Samil

Reputation: 1013

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

Upvotes: 4

Hossein
Hossein

Reputation: 25924

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!

Upvotes: 0

Elliot Woods
Elliot Woods

Reputation: 844

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.

Upvotes: 0

zar
zar

Reputation: 12227

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.

Upvotes: 2

Gravitas
Gravitas

Reputation: 21

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;

Upvotes: 2

Crubuntu
Crubuntu

Reputation: 63

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.

Upvotes: 5

Chuck
Chuck

Reputation: 2102

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.

Upvotes: 19

snb
snb

Reputation: 681

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

Upvotes: 49

CHendrix
CHendrix

Reputation: 388

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.

Upvotes: 3

Grault
Grault

Reputation: 1110

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.

Upvotes: 23

Steve Townsend
Steve Townsend

Reputation: 54148

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.

Upvotes: 120

Related Questions