Using Dynamic Libraries in C++

preview_player
Показать описание


Thank you to the following Patreon supporters:
- Samuel Egger
- Dominic Pace
- Kevin Gregory Agwaze
- Sébastien Bervoets
- Tobias Humig

Gear I use:
-----------------

Рекомендации по теме
Комментарии
Автор

If you use lib + dll,you don't need __declspec(dllimport) because all the functions or variables you wan't to import have defined in lib as pointer;but if you use LoadLibary API to import dll, with __declspec(dllimport) you can tell the compiler which function or variable you want to import, it will reduce the import time

洪鹏圳
Автор

In C++, when you use a dynamic link library (DLL), your code doesn’t need to directly include the DLL file to successfully use the functions within it. This is due to several crucial components and steps:
1. Import Library:
During compilation, the linker uses an import library ('.lib' file) to resolve references to functions in the DLL. The import library contains all the export function symbols but does not include the actual implementation of these functions. It informs the linker where these functions will be found in the corresponding DLL. You only need to include this import library in your project settings so the linker can resolve function references at compile time.
2. Header File:
The header file typically contains the declarations of all the functions you want to import from the DLL. The compiler needs this header file to check the correctness of the function calls. However, the header file does not contain the actual implementation, which is provided by the DLL at runtime.
3. Dynamic Loading:
At runtime, the system loads the required DLL based on references to functions from the DLL and resolves these references. When the executable starts running, the Windows operating system handles locating and loading the necessary DLL file and resolving the function addresses, allowing the program to correctly call the functions exported from the DLL.
4. Implicit and Explicit Linking:
- Implicit Linking: This method involves linking against an import library ('.lib') during compilation. At runtime, the operating system automatically loads and resolves the DLL. this is the linking method used in the video. You do need to provide the import library during the compile and link stage, such as "cl myprogram.cpp mylib.lib". For example:
'''
#include "mylib.h" // Header file containing the DLL function declarations

int main() {
MyFunction(); // Directly call the function from the DLL
return 0;
}
'''
- Explicit Linking: This method uses Windows API functions like 'LoadLibrary' and 'GetProcAddress' to dynamically load the DLL and manually resolve the function addresses at runtime. In this approach, you don’t need the import library at compile time. Instead, you dynamically load the DLL and resolve function addresses at runtime. omit the example.

to answer you question shortly, in this case, you are using Implicit linking and you provided the .lib file accordingly at compile time so that you don't need to specify the .dll in the program body. however, if you dont have the coordinated .lib file at the compile time (or any other reasons), them you have to specify where(in which dll) to find the function at runtime and call the function via the resolved function pointer.
kindly tell me if anything above is wrong.

hadenxu
Автор

this is literally the meme about programmers that goes
it's not working, why?
it's working, why?

medvfx
Автор

cherno is a blessing for programmer community. awesome tutorial

muhammadtaimourafzal
Автор

I see no pinned comments.. no one answered it correctly?

vertigo
Автор

damn 3 years now and no one answered the question 😂😂

mohamedmostafa
Автор

Absolute legend! I kept getting .dll missing errors, and this solved it.

ksawery
Автор

8:38 That french throat at "exactly" killed me :D

bulentgercek
Автор

This is because the DLL still has an IAT, Import Address Table. It will still load the functions from the IAT, but as Yuval said if you specify it as import when compiling the compiler will generate imports as imp__func_decl and it will do optimizations to ensure import is efficient.

However what hasn't been said yet is this is only optional for function declarations so if you use public data members there is no IAT entry for them, and you need dllimport or else the data will be local to the compilation unit or compiling program depending on what other modifiers are used.

joachimolsson
Автор

Me: It's not working, why?
The Cherno: It works, why?
Todd Howard: It just works!

friedrichwagner
Автор

You literally saved me. Thank you for your work!

barbosikd
Автор

Because you already linked the static library that came with the dll with all the declarations of functions within the dll, it knows where to look for them, except it's maybe slightly less efficient.

AgentM
Автор

I am currently binge watching this and one thing that amazes me: The hair dude. How did he manage to mess up a hair within 10 days????

leynenslucker
Автор

Concerning the __declspec(dllimport), if we dont set it the compiler compiles the function call

glfwInit();

to something like

call glfwInit

inside the object file. The linkers job is to resolve this. If we don't add the __declspec(dllimport) then the linker generates something like

call

which directs the flow of the program to a function thunk. A thunk is something like a dummyfunction without ret statement at the end which simply jumps to another address. This thunk could look like

jmp DWORD PTR __imp_glfwInit

and the pointer __imp_glfwInit will then be set when the dll is loaded into memory using the IAT to point to the correct function.

On the other hand, if we set __declspec(dllimport) we explicitly tell the compiler at compiletime that we want to use a function inside a dll. Thus we get the asm code

call DWORD PTR __imp_glfwInit

again __imp_glfwInit beeing set at runtime through the IAT. This saves the thunk memory and the jmp instruction.

crushdices
Автор

I was one of those asking for hw/challenges. I'm glad you started doing that!

laad
Автор

Thanks for the video, most of the videos posted in YouTube have similar contents. But yours is really interesting and felt I have learnt something new. I am currently try to do something with depth camera, since the technology is something advance its hard to find tutorial online. So, thought of digging deeper into every components of C++ and start doing the project by myself. You saved me!!!! thanks for the help...

arunkumaran
Автор

Previous video: new haircut
This video: crazy hair
Me: You are a wizard, Cherno!

alisonrae
Автор

I followed the file configuration in your previous video and my .exe file is in the project/bin/Win32/Debug, so I need to paste .dll file into this directory and it worked. Actually many places have the .exe file and only this one work. Thanks!

junyangliu
Автор

This has been really helpful, thank you

wezo
Автор

i checked the disassembly, if we dont define declespec dllimport by adding glfwdll in preprocessor, disassembly will have one more code "qword ptr [imp_glfwinit]", we are treating our library like a extern function. if we use declspec it will have directly one code "call glfwinit" this generate more efficient code because it have one less instruction

serNoob-wu