Am I understanding dll and lib files right?

In order to statically link, you need to compile your object file with the lib file. Then your exe has all the informations...am I correct?

In order to dynamically link, you need to ckmpile your object file with the lib file and in order to run your program, you need a dll file next to your exe file...am I correct?

But if you want to link dynamically without linking with a lib file, your header file should declare all the functions/classes then you can compile your object file with dll file. To run your program, you'll need your dll file next to exe file...am I correct?
Last edited on
In order to statically link, you need to compile your object file with the lib file. Then your exe has all the informations...am I correct?

Technically, you need to link (not compile) your object file with the static library, but yes, you are correct.

In order to dynamically link, you need to compile your object file with the lib file and in order to run your program, you need a dll file next to your exe file...am I correct?

The way that the dynamic library gets found varies by operating system, so you don't really "need a dll file next to your exe file." For example, on Linux and Solaris, the OS will search for the library in the directories specified by the LD_LIBRARY_PATH environment variable. So it can be in any of those directories.

But if you want to link dynamically without linking with a lib file....

I don't follow you here. If you want to "link dynamically" then that seems to imply linking with a library, so I don't get what you mean by "without linking with a lib file." To me, it's like saying "if you want to swimming without water." Can you clarify what you mean?
Game libraries such as Irrlicht don't requires to link with lib files. You can just link directly with dll file.

So instead of doing this, g++ main.cpp -lMyLib.lib

you can just do this, g++ main.cpp -lMyDll.dll

But I thought if you want to link dynamically, you always need lib files...can you explain why some libraries don't need lib files?
But I thought if you want to link dynamically, you always need lib files...

Actually, you don't need an import library when you are using runtime dynamic linking (aka "explicit linking"), you just need to know what the declaration of the exported functions, etc are. But you do have to load the DLL or shared object (SO) (or whatever it's called elsewhere?) and resolve the required functions explicitly (in WinAPI terms you use LoadLibrary() to load the DLL and GetProcAddress() to obtain pointers to the required DLL functions.)

But if you want to resolve DLL or SO functions when building your application (or DLL, SO, etc) -- so the module is loaded automatically when your program is run (as opposed to calling LoadLibrary(), etc), that is "implicit linking" -- then you do need to link against the import library.

(Aside: (question for Linux folk) does an SO act as its own import library? Does this even make sense in Linux terms??)

Based on what you say it appears that the Irrlicht guys have packaged the import library into the DLL somehow so you can use it (the DLL) rather than a separate import LIB file to resolve the dependencies. That is, their DLL file is what what usually be two files: the DLL plus its LIB (import library) file.

Edit: Not sure about this last paragraph. This might be possible, but I've never come across it myself.

Andy
Last edited on
@GCC folk

So, does GCC know how to use a DLL to resolve dependencies against an application? This thread makes it sound like it's possible.

g++ prefers to link against LIB instead of DLL
http://mingw.5.n7.nabble.com/g-prefers-to-link-against-LIB-instead-of-DLL-td30880.html

It's not something I've come across, but then I work with Visual Studio predominantly.

@yj1214

Game libraries such as Irrlicht don't requires to link with lib files. You can just link directly with dll file.

Out of interest, where does your information come from?

And are you working with Windows, yes?

I have just checked the irrlicht-1.8.1 SDK and they are providing Irrlicht.lib as well as Irrlicht.dll for Visual Studio and libIrrlicht.a and Irrlicht.dll for the MinGW build. And building one of the sample with MinGW GCC I see they use -lIrrlicht which will result in linking against libIrrlicht.a. So they prefer to work with an import library.

But it is possible that the MinGW version of GCC can work with the DLL rather than the LIB. (Hopefully someone can clarify this!)

Andy
Last edited on
Game libraries such as Irrlicht don't requires to link with lib files. You can just link directly with dll file.

Just bumped into the answer to this issue!

From:ld and WIN32 (cygwin/mingw)
https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/4/html/Using_ld_the_GNU_Linker/win32.html

direct linking to a dll

The cygwin/mingw ports of ld support the direct linking, including data symbols, to a dll without the usage of any import libraries. This is much faster and uses much less memory than does the traditional import library method, expecially when linking large libraries or applications. When ld creates an import lib, each function or variable exported from the dll is stored in its own bfd, even though a single bfd could contain many exports. The overhead involved in storing, loading, and processing so many bfd's is quite large, and explains the tremendous time, memory, and storage needed to link against particularly large or complex libraries when using import libs.

Unless something has changed recently, The Visual Studio C++ linker is not this clever.

Andy
Last edited on
Topic archived. No new replies allowed.