I have a dynamic library already generated with cmake in c++. How can I use it within another c++ project without having the .h or the .cpp file for the library. I only have the .dll file.
-
If you only have the DLL, you'll have to reverse engineer it to re-construct a .h for it. – Eljay Jul 14 '21 at 11:59
-
If you want to **load** the library **dynamically**, then see e.g. [that question](https://stackoverflow.com/questions/6997372). If you want to use the library in a usual way (by calling function defined in the library and **linking** with it), then you need to create a `.lib` file (*export* file). For that, see [another question](https://stackoverflow.com/questions/9360280). In both cases, for call a library's function you need to know its signature. And generally extracting signature from the library is not possible: [see there](https://stackoverflow.com/questions/1218718). – Tsyvarev Jul 14 '21 at 13:47
-
I have the .lib file so what do I do in order to use the functions defined in the library. Do I link the library with cmake or I use it directly? – Khaled Jul 14 '21 at 17:19
2 Answers
There are at least 2 ways:
- Generate .lib/.def (or other files) from your .dll file and use them into host application. You can see utilities like implib, mkexp etc.
- Use API: LoadLibrary for loading and GetProcAddress for getting function pointer. There is c++ wrapper for this: see boost::dll::import_alias.

- 1,072
- 6
- 6
As you mention dll
I'm assuming you are on a Windows platform. You can use tools like dumpbin /exports someLib.dll
to list the functions exported by the dll
and after that you can use GetProcAddress
to create a function pointers to those functions.
If you need something more complex, like using a C++ type this might get more complicated or even impossible as you need to link with the exact version of the standard library, compiler version and compiler flags.
For example if you have a function void myFunction(const std::string&)
in the dll
you must be sure the std::string
from your executable is binary compatible with the one in the dll
. If they are not binary compatible you'll have very difficult to find and solve run time errors. This applies even if you have the headers and this is the reason for libraries like QT
to deliver multiple versions of the same dll

- 20,260
- 32
- 123
- 211