Note that there are some explanatory texts on larger screens.

plurals
  1. POInclusion of unused symbols in object files by compiler in C vs C++
    primarykey
    data
    text
    <p>This might be a dumb question, but maybe someone can provide some insight.</p> <p>I have some global variables defined in a header file (yes yes I know that's bad, but this is just a hypothetical situation). I include this header file in two source files, which are then compiled into two object files. The global symbols are not referenced anywhere in the code.</p> <p>If the source files are C, then it looks like the compiler omits the global symbols and everything links without errors. If the source files are C++, the symbols are included in both object files and then I get linker errors. For C++ I am using extern "C" when I include the header.</p> <p>I am using the Microsoft compiler from VS2005.</p> <p>Here is my code:</p> <p>Header file (test.h):</p> <pre><code>#ifndef __TEST_H #define __TEST_H /* declaration in header file */ void *ptr; #endif </code></pre> <p>C Source files:</p> <p>test1.c</p> <pre><code>#include "test.h" int main( ) { return 0; } </code></pre> <p>test2.c</p> <pre><code>#include "test.h" </code></pre> <p>C++ Source Files:</p> <p>test1.cpp</p> <pre><code>extern "C" { #include "test.h" } int main( ) { return 0; } </code></pre> <p>test2.cpp</p> <pre><code>extern "C" { #include "test.h" } </code></pre> <p>For C, the object files look something like this:</p> <pre><code>Dump of file test1.obj File Type: COFF OBJECT COFF SYMBOL TABLE 000 006DC627 ABS notype Static | @comp.id 001 00000001 ABS notype Static | @feat.00 002 00000000 SECT1 notype Static | .drectve Section length 2F, #relocs 0, #linenums 0, checksum 0 004 00000000 SECT2 notype Static | .debug$S Section length 228, #relocs 7, #linenums 0, checksum 0 006 00000004 UNDEF notype External | _ptr 007 00000000 SECT3 notype Static | .text Section length 7, #relocs 0, #linenums 0, checksum 96F779C9 009 00000000 SECT3 notype () External | _main 00A 00000000 SECT4 notype Static | .debug$T Section length 1C, #relocs 0, #linenums 0, checksum 0 String Table Size = 0x0 bytes </code></pre> <p>And for C++ they look something like this:</p> <pre><code>Dump of file test1.obj File Type: COFF OBJECT COFF SYMBOL TABLE 000 006EC627 ABS notype Static | @comp.id 001 00000001 ABS notype Static | @feat.00 002 00000000 SECT1 notype Static | .drectve Section length 2F, #relocs 0, #linenums 0, checksum 0 004 00000000 SECT2 notype Static | .debug$S Section length 228, #relocs 7, #linenums 0, checksum 0 006 00000000 SECT3 notype Static | .bss Section length 4, #relocs 0, #linenums 0, checksum 0 008 00000000 SECT3 notype External | _ptr 009 00000000 SECT4 notype Static | .text Section length 7, #relocs 0, #linenums 0, checksum 96F779C9 00B 00000000 SECT4 notype () External | _main 00C 00000000 SECT5 notype Static | .debug$T Section length 1C, #relocs 0, #linenums 0, checksum 0 String Table Size = 0x0 bytes </code></pre> <p>I notice that _ptr is listed as UNDEF when I compile the C source, and it is defined when I compile the C++ source, which results in linker errors.</p> <p>I understand that this is not a good thing to do in real life, I am just trying to understand why this is different.</p> <p>Thanks.</p>
    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.
 

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