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

6

70

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?

Gnash answered 20/2, 2012 at 11:20 Comment(2)
With what compiler do you want to use the resulting lib with? Visual Studio? Which version?Junco
I am aiming to use Microsoft Visual studio 2010 to be the application to use between the DLL and Matlab. The DLL is called Wintab32, it is found when you use a graphics tablet.Gnash
I
97

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

Infinitesimal answered 21/4, 2013 at 3:9 Comment(3)
Using dumpbin.exe /EXPORTS yourfile.dll for step 1 and 2 instead of Dependency Walker works just as well for obtaining the exported function names.Wyn
dumpbin failed for me ("LINK : fatal error LNK1328: table de chaînes manquante") so I used depends and created the def file by hand.Grot
@Grot dumpbin works fine, that's just an environmental issue with your tooling installationChabot
I
23

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.

You can download it at https://ftp.digitalmars.com/bup.zip.

The command line is:

implib.exe /s mydll.lib mydll.dll
Individual answered 20/2, 2012 at 11:36 Comment(7)
Thanks, I am having a look at this IMPLIB that you have suggested. Following the link, a line of code suggested is: implib /s kernel32.lib kernel32.dll . I am a complete beginner with c++, how do I get started with implementing the code?Gnash
It's not the C++ code :) Just open up console (cmd) and execute implib using your dll (as shown in example with kernel32). Then get the resulting .lib file to the lib folder of your IDE (or to the projects folder & add the library path to the project). After that you will be free to use that library (just remember to keep the DLL is the same folder as the exe).Individual
This blog seems explain it better adrianhenke.wordpress.com/2008/12/05/create-lib-file-from-dll Or did you already try this? If yes, did it work?Malposition
Hi I used this method and got corrupted a file error in VS 2015. I had to use Sany Liew's solution to get the lib files working correctly.Detergency
I doubt if the Digital Mars solution will work. Whereas VS uses the PE-COFF format, DM uses the OEM format which requires an extra step to convert to a form usable in VS.Kore
Digital Mars Import Library Manager Version 7.6B1n Copyright (C) Digital Mars 2000. All Rights Reserved. Error: Unexpected char 'F' in command line.Chinoiserie
Error(10): Error: cannot read DLL input fileChinoiserie
M
5

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.

Margrettmarguerie answered 23/9, 2021 at 3:39 Comment(2)
Wow, thanks, that makes life a lot easier! Btw there's also a gendef package in Cygwin.Scalise
lib.exe in msvc solved my problem. I just ran lib.exe /DEF:.\src\build\windows\curl-windows\bin\libcurl-x64.def and after that I could use the library and resolve the implementations/dependency/external symbol __imp__curl_easy_init by passing the resulting .lib file as an input: cl.exe libcurl-x64.lib ...Brochure
S
3

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.

Scalise answered 16/4, 2022 at 10:15 Comment(0)
L
0

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

Langlauf answered 13/2, 2022 at 11:53 Comment(0)
J
-1

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!

Janusfaced answered 2/1, 2020 at 20:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.