OK, so when you complete a program and compile to executable, you can't just run it on other peoples' computers because they don't have the libraries. Some compilers such as VC++ come with a redistributable that the user can install to operate any program compiled therein. But I have also heard of an alternative method that packs the libraries into the program, rendering it self-contained.
So my question is, how do I go about doing this? (And for specifics, how do I do it in VC++ 08 Express?)
I always liked GCC/++ for this, because, unlike Microsoft's compiler which requires the runtime library to be installed, it links standard library functions statically so the executable is stand-alone. Of course this only applies if you are using standard libraries.
This leads to larger code, but honestly, how much smaller are Microsoft executables if you take the size of the runtime into account, and it's much easier to distribute because you don't have the MS runtime as an external dependancy.
Of course, this kind of goes out the window when working with libraries like Gtk+ and QT which are coded around dynamic linking.
Long story short: While static linking is nice, there really is no way to completely do away with dynamic linking so you might as well learn how to do it sooner than later.
In percentage change, the change in filesize is dramatic if you drop MFC in. Example: Simple internet downloader tool written with a few MFC classes (CString and such). The console application was 10KB when linking dynamically; it was, however, 240KB when compiled statically. Yes! 2400% change.
Though an extra 230K can be irrelevant for a large application. And file size is pretty irrelevant anyway; the real
benefit is if every app statically linked the library, then there would be 1 copy of the library in RAM for each
process using it, which is wasteful.
But it should be noted that even gcc/g++ dynamically links against the C/C++ runtime libraries by default. You can
see this by running "ldd" on your favorite app. It will show you what .so files the application will load at runtime.
No. It's a +230K. The increase isn't in relative to the size.
the real benefit is if every app statically linked the library, then there would be 1 copy of the library in RAM for each process using it, which is wasteful.
jsmith: True, but you are assuming that the overhead for any project will always be 230KB. I believe that assumption is incorrect, as the compiler will add the used code only. Those 230KB were for just the few classes used. What if you use 20 or more different classes? The overhead will be larger.
helios, my bad for not subtracting the original file size. So it is a 2300% change. Is that what you meant?
Those 230KB were for just the few classes used. What if you use 20 or more different classes? The overhead will be larger.
Wrong. Standard classes are included completely in the standard headers. It's true that using more classes will increase the code size, but that will happen regardless of how the runtime is linked. The runtime contains code for I/O and other things related to interaction with the OS. Linking it statically is a one time cost. That's what I meant when I said "+230K". It's 230K regardless of what the project contains.
I see. I was under the impression that only the code required to make the EXE work was really added.
Does this happen for any static library? If I create a static library with 100 classes, and create an EXE that uses 2 of them, will the 100 be included?
Unless the linker is configured to remove unreferenced symbols, yes. Even with that optimization, there's no guarantee that it will actually be able to remove them.