Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Struct with a value in a header file causing "duplicate symbol" linker error

This is a cut down example of a much larger project. You can see it here.

I have a header file containing the limits of the system time functions. Call it time_config.h.

#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

Then there's a header file defining my time functions. Call it mytime.h. It includes time_config.h.

#ifndef MYTIME_H
#define MYTIME_H

#include "time_config.h"

#ifndef HAS_TIMEGM
time_t timegm(const struct tm*);
#endif

#endif

There's mytime.c which includes mytime.h and defines timegm(), if necessary.

I compile it into an object file...

gcc <a lot of warning flags> -I. -c -o mytime.o mytime.c

And link that into a test binary, t/year_limit.t.c also includes mytime.h.

gcc <a lot of warning flags> -I. mytime.o t/year_limit.t.c -o t/year_limit.t

Which errors out with:

ld: duplicate symbol _SYSTEM_MKTIME_MAX in /var/folders/eJ/eJzTVP7oG7GVsKYHJtMprE+++TI/-Tmp-//ccMe5DXb.o and mytime.o
collect2: ld returned 1 exit status

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.

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?

PS This is ANSI C89.

like image 460
Schwern Avatar asked Jan 31 '26 00:01

Schwern


2 Answers

In your header (.h) you need:

extern const struct tm SYSTEM_MKTIME_MAX;
extern const struct tm SYSTEM_MKTIME_MIN;

In your implementation (.c) you need:

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 };
like image 175
Paul R Avatar answered Feb 02 '26 13:02

Paul R


const struct tm SYSTEM_MKTIME_MAX = { 7, 14, 19, 18, 0, 138, 0, 0, 0, 0, 0 };

The above declares and defines the object SYSTEM_MKTIME_MAX; Multiple inclusions of the header leads to multiple definitions.

Put in an extern in the header and put the definition in the implementation file.

like image 43
dirkgently Avatar answered Feb 02 '26 12:02

dirkgently



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!