Note that there are some explanatory texts on larger screens.

plurals
  1. POStruct with a value in a header file causing "duplicate symbol" linker error
    text
    copied!<p>This is a cut down example of a much larger project. You can see it <a href="http://github.com/schwern/y2038" rel="nofollow noreferrer">here</a>.</p> <p>I have a header file containing the limits of the system time functions. Call it time_config.h.</p> <pre><code>#ifndef TIME_CONFIG_H #define TIME_CONFIG_H #define HAS_TIMEGM #define SYSTEM_LOCALTIME_MAX 2147483647 #define SYSTEM_LOCALTIME_MIN -2147483648 #define SYSTEM_GMTIME_MAX 2147483647 #define SYSTEM_GMTIME_MIN -2147483648 const struct tm SYSTEM_MKTIME_MAX = { 7, 14, 19, 18, 0, 138, 0, 0, 0, 0, 0 }; const struct tm SYSTEM_MKTIME_MIN = { 52, 45, 12, 13, 11, 1, 0, 0, 0, 0, 0 }; #endif </code></pre> <p>Then there's a header file defining my time functions. Call it mytime.h. It includes time_config.h.</p> <pre><code>#ifndef MYTIME_H #define MYTIME_H #include "time_config.h" #ifndef HAS_TIMEGM time_t timegm(const struct tm*); #endif #endif </code></pre> <p>There's mytime.c which includes mytime.h and defines timegm(), if necessary.</p> <p>I compile it into an object file...</p> <pre><code>gcc &lt;a lot of warning flags&gt; -I. -c -o mytime.o mytime.c </code></pre> <p>And link that into a test binary, t/year_limit.t.c also includes mytime.h.</p> <pre><code>gcc &lt;a lot of warning flags&gt; -I. mytime.o t/year_limit.t.c -o t/year_limit.t </code></pre> <p>Which errors out with:</p> <pre><code>ld: duplicate symbol _SYSTEM_MKTIME_MAX in /var/folders/eJ/eJzTVP7oG7GVsKYHJtMprE+++TI/-Tmp-//ccMe5DXb.o and mytime.o collect2: ld returned 1 exit status </code></pre> <p>Because time_config.h is generated during the build process by a probe of the system, it would be very convenient to keep all the values together in one header file, or even multiple header files. Altering the .c file during the build process is more difficult.</p> <p>It works fine without the structs. How do I declare the min/max date structs in a header file without causing this clash? Or am I compiling and linking incorrectly?</p> <p>PS This is ANSI C89.</p>
 

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