Schwern
Schwern

Reputation: 164809

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.

Upvotes: 2

Views: 7628

Answers (4)

Alexander Poluektov
Alexander Poluektov

Reputation: 8053

In C constants have external linkage and thus should be defined in .c files.

In C++ they have internal linkage and thus could be defined in header.

Upvotes: 2

Dewfy
Dewfy

Reputation: 23624

Header must only contain declaration. So change header to:

extern const struct tm SYSTEM_MKTIME_MAX;

And in cpp make assignment

const struct tm SYSTEM_MKTIME_MAX = {...

Upvotes: 0

Paul R
Paul R

Reputation: 212969

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 };

Upvotes: 5

dirkgently
dirkgently

Reputation: 111130

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.

Upvotes: 4

Related Questions