1

I'm starting to program in Rust and one of the first things I noticed is that Rust produces large binaries. For example, Rust's "Hello world!" binary is ~600K large, while the equivalent C binary is ~8K large.

After some searching I found this SO post which explains that Rust binaries are large because all the needed libraries are statically linked. But isn't that the case for C as well? When I write #include <stdio.h> in C, aren't I statically linking the relevant I/O libraries as well? I have always assumed that answer is 'yes' but now I am doubting myself.

Community
  • 1
  • 1
Alessandro Power
  • 2,395
  • 2
  • 19
  • 39
  • No, not always. Usually the `-l` goes for dynamic linking of `.so` files. – Sourav Ghosh Mar 16 '16 at 15:05
  • 1
    Linking is different from header file inclusion. (`#include ` is not a linking command/statement.) – callyalater Mar 16 '16 at 15:05
  • No, static vs. dynamic linking are options you choose. – Carey Gregory Mar 16 '16 at 15:05
  • @callyalater Doesn't `#include` essentially copy and paste the library into the source file? Maybe I'm being dense but I cannot see how that wouldn't statically link the library. – Alessandro Power Mar 16 '16 at 15:11
  • `#include` copies the *file contents* to the source file, but if the header is nothing more than function declarations, all that would do is tell the program that those functions are available to be called in your code. The actual implementation may be defined in another file that would need to be linked in (either statically or dynamically) to your executable. If you look at the header for [`stdio.h`](https://www.gnu.org/software/m68hc11/examples/stdio_8h-source.html) you would see that it only contains function prototypes. – callyalater Mar 16 '16 at 15:15
  • @callyalater Okay that makes sense. I'll accept that explanation if you include it as an answer. – Alessandro Power Mar 16 '16 at 15:17

1 Answers1

4

#include copies the file contents to the source file, but if the header is nothing more than function declarations, all that would do is tell the program that those functions are available to be called in your code. The actual implementation may be defined in another file that would need to be linked in (either statically or dynamically) to your executable. If you look at the header for stdio.h you would see that it only contains function prototypes.

Many compilers provide options to do either static or dynamic linking for the standard libraries.

callyalater
  • 3,102
  • 8
  • 20
  • 27
  • Thanks. Final question: I have a project which uses some Boost libraries. Is there a way I can tell if those libraries were statically or dynamically linked? – Alessandro Power Mar 16 '16 at 15:22
  • 1
    @AlessandroPower: If you're on a Unix-like system, run `ldd` on the executable. – Keith Thompson Mar 16 '16 at 15:23
  • @AlessandroPower `boost` are header only library (expect some specific parts). So, there is no need of linkage for most part of boost – Garf365 Mar 16 '16 at 15:27
  • 1
    Most boost libraries are intended for C++ use. Because they tend to use templates for their classes and functions, they are "header-only" libraries that only require you to include the `boost_header_file.hpp` file in your code. Some libraries (like `regex`, `filesystems`, and `boost_threads`) require precompilation as a library before use, but you can compile them as either static or dynamic libraries (I have done both) and link them accordingly. – callyalater Mar 16 '16 at 15:27
  • You can read more about which libraries require precompilation on the [Boost official website](http://www.boost.org/doc/libs/1_60_0/more/getting_started/unix-variants.html#header-only-libraries). – callyalater Mar 16 '16 at 15:29