Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How do .lib and .dll work on Win?  (Read 660 times)

0 Members and 1 Guest are viewing this topic.

abcnb

  • Newbie
  • *
  • Posts: 12
    • View Profile
How do .lib and .dll work on Win?
« on: October 13, 2022, 07:32:18 am »
Suppose, I compiled SFML with CMake and MinGW.
Now I have .lib and .dll libraries.

I create main.cpp, add #include and want to compile with mingw32-make.
all: compile link
compile:
        g++ -c main.cpp -I "C:\Users\G\Downloads\SFML-2.5.1\include"
link:
        g++ main.o -o main -L "C:\Users\G\Downloads\SFML builds\lib" -l sfml-graphics -l sfml-window -l sfml-system
 

Do I understand correctly that .lib files are using like a wrapper on linking stage for future working with .dll files?

kojack

  • Sr. Member
  • ****
  • Posts: 315
  • C++/C# game dev teacher.
    • View Profile
Re: How do .lib and .dll work on Win?
« Reply #1 on: October 13, 2022, 12:48:00 pm »
On windows there are two kinds of .lib files: static libraries and import libraries.

A static library .lib contains a collection of .obj files. It's code that was compiled but not yet linked.

An import library is a .lib that works with a matching .dll. The .lib contains linking information (used at link time), while the .dll contains all the actual compiled code (used at run time).

When you run a program that was linked with an import library, it will automatically search the path for the .dll file and load it for you. This means you can replace a dll with an updated version without recompiling the main program.

It's also possible to manually load a dll and connect to it's functions without using a .lib, which is how plugin systems often work. It's less convenient though, needs way more manual code.

So yep, you are right, .libs are used when linking to prepare the executable for future use of the dll.

abcnb

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: How do .lib and .dll work on Win?
« Reply #2 on: October 14, 2022, 04:41:11 am »
Brilliant answer! Thank you.