Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.h, .dll and .lib confusion

Tags:

visual-c++

I'm new to vc++. I've just built a software and it generated a .dll and a .lib. I need to use functions from this in my code. Do I need to link to both .lib and .dll to build my code? What project properties do I have to alter to do this linking?

like image 473
aks Avatar asked Aug 30 '10 08:08

aks


People also ask

What is the difference between H and Lib and DLL?

1 H Declares the interface to a library - including functions, structures, and constants. ... 2 LIB Either declares the binary interface to a dynamic library (DLL) or contains the binary code of a library. 3 DLL A dynamic library - your application shares these with the system or you use them to keep your code base organized. More items...

What is the difference between a library and a DLL?

H Declares the interface to a library - including functions, structures, and constants. Written in the C language. LIB Either declares the binary interface to a dynamic library (DLL) or contains the binary code of a library. DLL A dynamic library - your application shares these with the system or you use them to keep your code base organized.

Is it possible to call a function from a DLL?

If the dll export its functions by ordinal, still you can call it. Simply you set a new name for the ordinal In addition, you can export your function. The compiler and linker don't complain, but the Operating System's loader will complain.

What is libcdeclfun?

It means that: LIBcdeclfun is an alias of cdeclfun, note that Visual Basic can't accept '_' (underscore), also, left name is more meaningful. What is [DATA] ? It means that: vcdata is data, not function.


1 Answers

Actually, you need only the .dll file. It contains all the necessary code and data to run it's functions. It also contains a table that links the symbolic names of the functions (e.g. the function PrintMe), their ordinals (the number of that function in the DLL) and their addresses in the DLL.
If you want to use only the DLL, you have to "manually" get the symbols resolved:
Let's say you want to use the function PrintMe of the DLL. What you had to do is to resolve it's name (PrintMe) or it's ordinal (PrintMe is the 1st function of the DLL) to it's address. For this, you could use LoadLibrary, GetModuleHandle and GetProcAdress from the Win32 API (aka Windows SDK). Additionally, this method allows you to load the DLL at runtime (see below).

The easier way is to use the MSVC(++) features __declspec(dllexport) and __declspec(dllimport), e.g.


// your DLL
__declspec(dllexport) void PrintMe()
{
    printf("Hello World!");
}

// you project to use the DLL
__declspec(dllimport) void PrintMe();

The first one (dllexport) tells the compiler to export the function. The second one (dllimport) is the interesting one: It creates all the necessary code to be able to use the function from the DLL.
For this, you need the .lib file in your project (which wants to use the DLL). The .lib file contains information for the linker to resolve the symbol name (PrintMe) to its address in the DLL. Since the .lib is statically bound, the linker can make use of it - the DLL on the contrary is bound at runtime / loading time, so the linker cannot use it. (Yes, the information in the .lib file is redundant.). Note: You cannot change the whole DLL when using this method w/o rebuilding your project with the new .lib file. Some structure changes affect the addresses of the functions in the DLL, see this SO answer.
One last difference between using the Win32 API (LoadLibrary...) and the MSVC method via __declspec is the loading of the DLL. When you use LoadLibrary, the DLL is loaded at runtime, of course (so you can catch exceptions when it cannot be found and so on). The other method loads the DLL at loading time, so you program will terminate (will not run) when Windows cannot find the DLL.

When you create a project in VS, you can activate the "export symbols" checkbox at the end of a wizard (Win32 project). That gives you some examples of exported symbols. Additionally, it introduces a macro plus a preprocessor defition plus some directives that are very useful:


// DLL header

#ifdef _YOUR_DLL_EXPORTS
#define YOUR_DLL_API __declspec(dllexport)
#else
#define YOUR_DLL_API __declspec(dllimport)
#endif

YOUR_DLL_API PrintMe();

You now can use this header file to build you DLL as your DLL project has that _YOUR_DLL_EXPORTS definition (see project properties page, C++, preprocessor). The project that uses the DLL can use this header, too, but then must not have such a name defined. When you include the header file in the project in which you want to use the DLL, the macro is resolved to __declspec(dllimport). This instructs the linker to look for this function (which is found in the .lib file) and create all the necessary code to load the DLL at runtime and resolve the symbol name.

like image 179
dyp Avatar answered Sep 20 '22 07:09

dyp