Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>In general, compilation with different symbols compiled different code</h2> <p>Consider the following code. If <code>FOOBAR</code> is defined, then there's <em>more</em> code in the file to compile (after the preprocessor preprocesses it):</p> <pre class="lang-c prettyprint-override"><code>#ifdef FOOBAR int foo(int bar) { return bar + bar; } #endif int bar(int baz) { return 1+baz; } </code></pre> <p>Compiling with FOOBAR defined changes the size of the output. Without FOOBAR, it's 1232, and with FOOBAR, it's 1328. That's not a huge difference, but it is a difference.</p> <pre class="lang-none prettyprint-override"><code>$ gcc -c code.c -o code.o $ ls -l code.o -rw-rw-r-- 1 user user 1232 Oct 29 13:19 code.o $ gcc -DFOOBAR -c code.c -o code.o $ ls -l code.o -rw-rw-r-- 1 user 1328 Oct 29 13:19 code.o </code></pre> <p>If there's lots of conditional code, this could be very significant. For instance, defining symbols might cause lots of platform specific code to be included, and not defining symbols might leave function implementations as stubs.</p> <h2>Compiling different <em>types</em> of code produces different code sizes</h2> <p><em>Note: this portion is based on <a href="https://stackoverflow.com/a/19665374/1281433">Urhixidur's (the OP's) answer</a>. I felt that a bit more elaboration on it was in order.</em></p> <p>Another aspect that can lead to different compiled object sizes is what GCC is actually compiling. In your example </p> <pre><code>gcc -I. -c -D SOME_CONTROL_FLAG -o thefile.o thefile.h </code></pre> <p>a header file is being compiled, and GCC detects that it's compiling with the <code>c-header</code> language based on the file extension. However, the fact that you're compiling a header file and producing a <code>.o</code> file suggests that you want to compile this as C, in which case you should use GCC's <code>-x</code> option. About it the man page says:</p> <pre><code> -x language Specify explicitly the language for the following input files (rather than letting the compiler choose a default based on the file name suffix). This option applies to all following input files until the next -x option. Possible values for language are: c c-header cpp-output c++ c++-header c++-cpp-output objective-c objective-c-header objective-c-cpp-output objective-c++ objective-c++-header objective-c++-cpp-output assembler assembler-with-cpp ada f77 f77-cpp-input f95 f95-cpp-input go java -x none Turn off any specification of a language, so that subsequent files are handled according to their file name suffixes (as they are if -x has not been used at all). </code></pre> <p>Based on this, and the code I used in the first section, we can observe the <em>drastic</em> size difference that happens when we compile your code as <code>c</code> or as <code>c-header</code>:</p> <pre><code>$ gcc -c code.h -o code.o # as a header $ ls -l code.o -rw-rw-r-- 1 user user 1470864 Oct 29 14:04 code.o $ gcc -c -x c code.h -o code.o # as c code $ ls -l code.o -rw-rw-r-- 1 user user 1232 Oct 29 14:04 code.o </code></pre> <p>Note that compiling (as a header) doesn't seem to be affected by the symbol definition though:</p> <pre><code>$ gcc -c code.h -o code.o $ ls -l code.o -rw-rw-r-- 1 user user 1470864 Oct 29 14:06 code.o $ gcc -DFOOBAR -c code.h -o code.o $ ls -l code.o -rw-rw-r-- 1 user user 1470864 Oct 29 14:06 code.o </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload