.lib file inclusion in application when DLLs are used - c++

I have a c++ application which is using a DLL. Now, I want to access a method from that DLL in my application. But, compiler is giving me "unresolved external symbol" error as it is unable to find the method even though my function in DLL is exported properly. When I include .lib file path of respective DLL in my application's additional dependencies, the error got resolved and application is working properly.
But, some people told me that I should not include .lib file when I am using a DLL. It is just like statically linking the DLL.
1) Is inclusion of .lib file when using DLLs in application, is good practice or not ?
To avoid this .lib file inclusion and to avoid calling GetProcAddess for every exportable method, they suggested me to write some wrapper class and create a virtual method to access the method from the class in DLL. I didn't understand this approach.
2) Can anybody explain why I need the wrapper class and how this abstract class can provide me the function I required ?
Thanks in advance.

1) Is inclusion of .lib file when using DLLs in application, is good practice or not?
According to msdn you are required to link to .lib files when using dll. Consideration of good or bad practice comes in when you have choices. Which you don't have here.
they suggested me to write some wrapper class and create a virtual
method to access the method from the class in DLL.
Well if you create a wrapper class then you will have to link that wrapper class with the dll and that will bring you in the same place again.

Inclusion of .lib file when using dll is used by all the shared libraries I've used on Windows.
It's important to understand that, on Windows platform, not all the libraries with .lib extension are static. Here is a discussion that explains how to understand if your .lib library is a static library or an import library.

Related

why there is a .lib file when create dynamic .dll file?

I'm try to build a dynamic dll library on on windows using visual studio, but there are two file generated, one is dll file, another is .lib file;
My knowledege of using dll library is privode it to linker, I don't know what is ther purpose of .lib file, it has the same file extension as static lib, and it definitely is not static lib;
Does visual studio generate a .lib file when create dynamic dll library at all situtation?
Do I need to use the .lib file in my application?
How do I do use the .lib file in my application?
It has to do with the difference between "implicit" linking and "explicit" linking. The one sentence answer to your question is that that .lib file, often called a "stub lib" and officially called an "import library", is necessary when you do implicit linking but not otherwise.
In implicit linking, at compile time the compiler generates external function references for calls into the DLL, then the linker needs to link those to something: it uses the import library to help it do that. The linker treats these calls as special cases; it inserts code in the executable file for finding the DLL and for calling into the DLL for specific functions.
From the point-of-view of the programmer, implicit linking behaves like static linking except the DLL needs to be somewhere where the system can find it when the application is run or the system will pop up an error dialog at application startup.
Explicit linking means that it is the programmer's responsibility to find and load the DLL and to know what functions are in the DLL and how to call into them. See LoadLibrary and GetProcAddress for details. Explicit linking is useful if usage of a library is conditional or for applications with plugin architectures in which the actual DLLs to be used may not even be known at compile time.
In general though, if implicit linking does what you need, it is easier to just do it that way.

Using a dll in a qt application program

Everybody, I am a beginner and I have still have some things I am confusing about.
I have a program qt which I want to include a extern library. Generaly, to include a extern library I use the macros :
INCLUDEPATH += "path/to/the/include/headers/file"
LIBS += -L"path/to/where/the/lib/are" \
-llibname // .a for gcc .lib for mscv
Then I can use the library in my program. But at the end the qt aplication program use the .dll associated to the lib name. So my question, why can't we use directly the .dll in Qt ? I don't know very much the difference between a ".lib" and ".dll" execpt a ".dll" is used at the runtime.
My problemen is have a library with only the dll and the include headers file. Is there a way to use this library like this or I must have the .a or .lib associated ?
EDIT :
Ok now,thanks to the useful advice, I understand better the difference between, .lib and .dll and how to use a dll without a .lib with only header. But I am having a issue. With the header, I can create the object, the compilator doesn't bother about it can't find the object and its method, but When I compile, I have some problems about "undefined reference to a method".
If I correctly understand, the reference cannot be found because the reference are defined in the .lib, that's why it can't find it. So my question :
How can I say to the compilator that the reference to a method will be defined at runtime and not at compile time ?
EDIT2 :
Ok so apparently you can get a pointer of an object with a dll but you can't use its methods, so I think I am facing the wrong way. Thanks again for you answer.
Best regards,
There are 2 types of lib file, see this asnwer: https://stackoverflow.com/a/2375144/2064646
You can just use a .dll without .lib file, but through GetProcAddress function.

How to use a DLL without the need of its .h and .lib files in a VC++ 6.0 Project?

I don't know how to do the following:
I'm using MS Visual C++ 6.0
I have a Win32 DLL project which is compilable.
I have another project, this time a Win32 Console project which uses
the DLL by including it's header file and linking the .lib file of
the DLL.
Now I want to have another project, similar to the second BUT without using the header file and the lib file.
Is that possible? Everywhere I read you need either dll+lib+h or dll+h. If thought if you know the interfaces, a DLL file is sufficient?
Btw, by "using a DLL" I mean, using the Classes and Functions defined in the DLL.
It is possible if you just have plain "extern C" functions. If this is the case the approach could be loading the dll with LoadLibrary, and then import each function with GetProcAddress, of course you need to know the function signature to create a properly declared function pointer. Using classes per contrary is almost impossible.
If your DLL contains classes, there are good chances that it is a COM component.
If this is the case, the #import directive (that you use like #include) builds some temporary include files containing the interface details. You should use COM to access your objects.
Otherwise, if you have a 'plain' DLL with C++ classes, you could access the exported symbols using linker: instruct it to dump the map (see here), to know the mangled names. But I don't think that's possible to build manually the interface...

Is it possible to link lib dynamically like a DLL?

This is interview question.
Is it possible to link lib dynamically like a DLL ?
For example, for DLL, we use LoadLibrary and call exported functions.
Is it possible to use lib file in same manner?
No. .lib library are statically linked, and that is the purpose they're created for, to resolve the name symbols at link-time by Linker, and link-time occurs before runtime. They're often referred to as "static libraries" (that is why I added this tag in your question!). That is the brief story of lib.
However, you can create a DLL wrapper, if you really want to link at runtime.
No. Create a DLL instead or, if you do not have the source, wrap the functionality in the .lib with own DLL interface.
No. It's not possible. A DLL is module with a PE32 header with all information to load it into a process. A LIB is only a archive of OBJ files.
And despite others say it's easy to wrap a DLL around it, this can be quite difficult. The reason is, that a .LIB not only resolves some dependencies but can also have unresolved externals.
As long as these unresolved externals require only the compiler's runtime library wrapping in a DLL might work. You can check this when you create a DLL project, probably with a minimal C++ source, and try to compile. Than you see, if further externals must be resolved with other libraries.
One important problem may arise with memory management. When yo link statically with a .LIB you will use all the same definitions. If your library comes with own implementation, let's say of malloc-stlye functions, this will not be linked to your application as long as you add all these symbols to the EXPORT list. Finding the list of public symbols that should be included in the EXPORT table may be a pain.
Yes - not directly, but with a very small amount of work.
Create a new .DLL project, link the .lib, define which functions you want to export in a .DEF file then compile.

Why dll can't be used in c++?

It's pointed out by this answer:
Failed to link mysql5.1.39\bin\libmySQL.dll
But I don't understand why,.dll is essentially the same as .lib except for there is only one copy of it used by different processes.
Does it have anything to do with the IDE?I'm using visual c++ 2008 express
UPDATE
Anyone know a free tool to convert .dll into .lib in windows?
You are wrong on two counts. Firstly, DLLs and LIBs (static libraries) are very different beasts. The LIB you are talking about (I think) is the export library, which is simply a list of names in the DLL. This library is typically produced when the DLL is compiled and is shipped with the DLL if the DLL is intended to be linked to by other developers.
To use a DLL with a modern IDE (I don't use VS) you typically include the matching .LIB (export library) in the project. At run-time you must make sure the DLL is available to your program - the simplest way to do this is to put the DLL in the same directory as the executable.
And secondly, DLLs can be used with C++.
DLL are specific windows executables which load on runtime. Their equivalent in Linux is the *.so .
If you want to understand what's the difference between a DLL and a static lib see here.
Main reason has probably something to do with dll-file being an output of the linker (link.exe). This command line utility doesnt know how to read dlls, only how to write them.
Actually sometimes .lib-files contain more than a list of functions. For example libpng.lib works as a standalone file, without any dll file.
In order to use a DLL in C/C++ you need to link with what is called an import lib. This is a small .lib that contains the names/ordinals the DLL exports in a format that the linker understands. Once this has been linked in, the resulting binary will be able to use the DLL. If you do not have the appropriate import lib, your C/C++ compiler should come with a tool to generate one from the DLL.
You can use the following analogy to understand the difference between a dll and a lib file.
the dll is like the source .cpp file while the lib is the header .h file.
While this is not exactly true, the DLL holds the executable code while the LIB file tells the linker how to glue the code together.
It is also possible (in some cases) to generate the lib from the dll. Basically, all you need to know to call a function is the function entry point in the dll, the number of parameters and the size of each parameters. Sending relevant information is then your own problem.