How to make a .lib file when have a .dll file and a header file

C++DllHeaderFunction

C++ Problem Overview


I am trying to create an application in visual studio that will be able to access a .dll file that already exists. I need the application to call up routines. I also have a header file that already exists.

I have been researching on the internet and have found that I need to create a .lib file. Looking at similar questions on here I found a link: http://support.microsoft.com/kb/131313 I cannot however follow the directions.

The information in the link says to make a DEF file ( I read elsewhere that this needs to be compiled as a DLL with the same name, but not sure what that name is, the same name as the .dll file?). But I do not understand the first direction, to 'Use DUMPBIN /EXPORTS'. I then need to 'stub out' functions, and then something to do with .OBJ files (I do not know what these files are).

Are there any step-by-step directions, similar to the link above, that are easy to follow?

C++ Solutions


Solution 1 - C++

You're going to need Microsoft Visual C++ 2010 Express (or any other source of MSVC command line tools), and your DLL.

Steps:

  1. dumpbin /EXPORTS yourfile.dll > yourfile.exports
  2. Paste the names of the needed functions from yourfile.exports into a new yourfile.def file. Add a line with the word EXPORTS at the top of this file.
  3. Run the following commands from VC\bin directory (the one where lib.exe and other compile tools reside).

 

 vcvars32.bat

 lib /def:yourfile.def /out:yourfile.lib

or for x64 builds

 lib /def:yourfile.def /machine:x64 /out:yourfile64.lib

You should get two files generated: yourfile.lib and yourfile.exp

Solution 2 - C++

You can use Digital Mars's IMPLIB tool. It can create a lib file using only the dll, without any need for a .def file.

The download link is http://ftp.digitalmars.com/bup.zip.

The command line is:

implib.exe /s mydll.lib mydll.dll

Solution 3 - C++

There is a much simpler way to create a .def file. Search the web for the gendef utility (you may already have it if you have Mingw-64). Once you have it, just go to a command line and type,

gendef myfile.dll

And it will create a myfile.def. After that, just use lib.exe to create the myfile.lib file as explained by John.

Solution 4 - C++

I might have the answer. I did this when I was creating a .exe console application that needed a .dll file. I ran into the problem as well. When I tried the IMPLIB application, it couldn't find any export files. You need to add an #ifdef FILENAME_EXPORTS (replace FILENAME with your .dll file name) and create an _API. Here is the code for the #ifdef export api commands:

#ifdef FILENAME_EXPORTS
#define FILENAME_API __declspec(dllexport)
#else
#define FILENAME_API __declspec(dllimport)
#endif

Now that you have the Export API defined, you need to apply it to all the functions in your header file in the .dll project. For example:

void FILENAME_API function();

Declare your export functions normally, but include the API between the declarer type and the function name.

For defining the function in the .cpp file in the .dll project, you don't need the API in the declaration.

Here is an example of filename.h and filename.cpp with all the code.

// Code for filename.h
#pragma once

// Define your Export API
#ifdef FILENAME_EXPORTS
#define FILENAME_API __declspec(dllexport)
#else
#define FILENAME_API __declspec(dllimport)
#endif

// Declare your functions with your API

void FILENAME_API function1();
void FILENAME_API function2();
void FILENAME_API function3();

-------------------------------------------------------------------------------------------
// Code for filename.cpp
#include <iostream>
#include "pch.h"
#include "filename.h"
using namespace std;

void function1()
{
    cout << "Hello Function1!";
}

void function2()
{
    cout << "Hello Function2!";
}

void function3()
{
    cout << "Hello Function3!";
}

Now when you compile the project, you should see the .dll, .lib, and .exp files in the folder where the compiled files are saved to. Now you can link the .exe file with the .lib file. You're Welcome!

Solution 5 - C++

First type

#define YOURPROJECT_API _declspec(dllexport)

void yourf()

cpp

#include "pch.h"
#include "YOURPROJECT.H"
void yourf() {}

then include them

linker->input->def file then inhert from parent or project default

compile

Solution 6 - C++

Here is a solution using Cygwin:

  • install gendef and mingw64-x86_64-binutils Cygwin packages
  • run in Cygwin console:
    gendef.exe awesome.dll
    x86_64-w64-mingw32-dlltool.exe -d awesome.def -l awesome.lib
    
  • you get awesome.lib in the same dir.

VS 2019 linker linked just fine using this file.

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
QuestionhdeView Question on Stackoverflow
Solution 1 - C++Sany LiewView Answer on Stackoverflow
Solution 2 - C++Desu_Never_LiesView Answer on Stackoverflow
Solution 3 - C++G. Adam StanislavView Answer on Stackoverflow
Solution 4 - C++ScotthomasPrimeView Answer on Stackoverflow
Solution 5 - C++Ahmed OsamaView Answer on Stackoverflow
Solution 6 - C++user2745509View Answer on Stackoverflow