Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling a static lib inside a exe

Tags:

c++

c

dll

linker

I have a dll and an exe, both of which I have the sources to.

For the DLL I have compiled completely statically and therefore, I would assume that the the .lib is also static. However, when I include that lib in my C++ VC++ 2008 project under Linker > Input > Additional Dependencies . I set the compile mode to /MT (multi-threaded) for the exe.

Everything compiles, but when I try to run the exe, it asks for the dll! To the best of my (limited) understanding, that shouldn't be happening.

Why should I do?

like image 218
Alec Gorge Avatar asked Apr 28 '26 07:04

Alec Gorge


2 Answers

The 'compile mode' setting that you are referring to is the setting for the runtime library that gets linked with whatever library or executable you produce.

If your project is set up to produce a DLL (check the main project page), then it'll still produce a DLL no matter what you're putting into the runtime library setting. What I think you want to do is change the setting on the DLL's main project page from DLL to Static Library instead of changing the runtime library setting.

Once you've done this, make sure that both the executable and library projects have the same runtime library setting (the /MT switch you refer to), otherwise you'll get tons of strange error messages if the linker is trying to match up two different runtime libraries in the same executable.

like image 110
Timo Geusch Avatar answered Apr 30 '26 20:04

Timo Geusch


The .lib file that is created with a "static" DLL is just an import library that handles automatic dynamic linking to all the symbols in the library. The DLL itself (that is, the .dll file) still contains all the code/symbols/etc. that you expect.

Statically linking to the .lib file just saves you from manually calling LoadLibrary()/GetProcAddress(), etc. to resolve symbols within the DLL.

You'll still need the DLL itself unless you build a true static library (that is, with all the symbols & code, rather than just the imports).

like image 29
Drew Hall Avatar answered Apr 30 '26 22:04

Drew Hall



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!