views:

364

answers:

4

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.

+1  A: 
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.

dirkgently
I thought the `#ifndef TIME_CONFIG_H` protected against multiple inclusions?
Schwern
Only in the same compilation unit - these are different compilation units which define the symbol
George Shore
@Schwern: It does, but then your SYSTEM_MKTIME_MAX is defined twice in _different_ translation units (i.e. source files) and that's why linker complains.
Alexander Poluektov
I was afraid that was the answer. The header file is generated by a program that probes the system. Having to alter the .c file as well complicates the build. There's no way to keep it in the header file?
Schwern
Compile it with C++ compiler? (Ok, just kidding)
Alexander Poluektov
Portability's a bitch. :(
Schwern
What I wound up doing is putting the definition into time_config.c, compiling that into time_config.o and linking it with mytime.o. Then I can generate time_config.c rather than rewrite mytime.c on the fly. Thanks for the help.
Schwern
Damn, that didn't work.
Schwern
@Schwern: Create a `.lib` or a `.so` for your time class.
dirkgently
@dirkgently (Class? Oh I wish.) This is intended to be shippd with and compiled into a binary such as perl or sqlite rather than installed as a separate library and then linked in via a .so. Unless a shared library can be linked static? I'm not going to pretend to understand linking.
Schwern
+2  A: 

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 };
Paul R
A: 

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 = {...
Dewfy
Note, we are talking about C, not C++ (in which constants have internal linkage and thus example is valid in C++). So probably not cpp, but c files.
Alexander Poluektov
@Alexander Poluektov neither cpp, nor c supports twice declaration, so keyword is "extern"
Dewfy
1) You mixed up declaration and definition. Twice declaration is perfectly acceptable _both_ in C and C++
Alexander Poluektov
2) "Twice definition" of const is perfectly acceptable in C++ as constants have internal linkage.
Alexander Poluektov
+1  A: 

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.

Alexander Poluektov