'How to make a .lib file when have a .dll file and a header file
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?
Solution 1:[1]
You're going to need Microsoft Visual C++ 2010 Express (or any other source of MSVC command line tools), and your DLL.
Steps:
dumpbin /EXPORTS yourfile.dll > yourfile.exports
- Paste the names of the needed functions from
yourfile.exports
into a newyourfile.def
file. Add a line with the wordEXPORTS
at the top of this file. - Run the following commands from
VC\bin
directory (the one wherelib.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:[2]
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:[3]
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:[4]
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:[5]
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:[6]
Here is a solution using Cygwin:
- install
gendef
andmingw64-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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | John |
Solution 2 | sashoalm |
Solution 3 | cursorrux |
Solution 4 | ScotthomasPrime |
Solution 5 | Ahmed Osama |
Solution 6 | user2745509 |