By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,985 Members | 1,777 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,985 IT Pros & Developers. It's quick & easy.

Header file included more than once?

P: n/a
Hi Group,

suppose test1.c, test2.c and test.h

/*** BEGIN TEST.H ***/
#ifndef _TEST_H
#define _TEST_H

typedef struct
{
unsigned int code;
const char *name;
} test_struct;

test_struct structs[NOF_STRUCTS] =
{
{ 12, "twelve" },
{ 11, "eleven" }
};

int test_func(void);

#endif /* !_TEST_H */
/*** END TEST.H ***/

/*** BEGIN TEST1.C ***/
#include <stdio.h>
#include "test.h"

int main(void)
{
return (0);
}
/*** END TEST1.C ***/

/*** BEGIN TEST2.C ***/
#include "test.h"

int test_func(void)
{
return (2);
}
/*** TEST2.C ***/
gcc -ggdb -o test test1.c test2.c
/var/tmp//ccZgBLuP.o(.data+0x0): multiple definition of `structs'
/var/tmp//ccLatz7e.o(.data+0x0): first defined here

Why is the header file parsed twice (and thus structs defined twice),
even when I started the header file with #ifndef etc etc statements?

Thank you!

--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp
Aug 9 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Pietro Cerutti wrote:
Hi Group,

suppose test1.c, test2.c and test.h

/*** BEGIN TEST.H ***/
#ifndef _TEST_H
#define _TEST_H

typedef struct
{
unsigned int code;
const char *name;
} test_struct;

test_struct structs[NOF_STRUCTS] =
{
{ 12, "twelve" },
{ 11, "eleven" }
};

int test_func(void);

#endif /* !_TEST_H */
/*** END TEST.H ***/

/*** BEGIN TEST1.C ***/
#include <stdio.h>
#include "test.h"

int main(void)
{
return (0);
}
/*** END TEST1.C ***/

/*** BEGIN TEST2.C ***/
#include "test.h"

int test_func(void)
{
return (2);
}
/*** TEST2.C ***/

>>gcc -ggdb -o test test1.c test2.c

/var/tmp//ccZgBLuP.o(.data+0x0): multiple definition of `structs'
/var/tmp//ccLatz7e.o(.data+0x0): first defined here

Why is the header file parsed twice (and thus structs defined twice),
because the header file is included in two independent source files
which are compiled individually.
even when I started the header file with #ifndef etc etc statements?
Aug 9 '07 #2

P: n/a
Pietro Cerutti <ga**@gahr.chwrote:
suppose test1.c, test2.c and test.h
/*** BEGIN TEST.H ***/
#ifndef _TEST_H
#define _TEST_H
typedef struct
{
unsigned int code;
const char *name;
} test_struct;
test_struct structs[NOF_STRUCTS] =
{
{ 12, "twelve" },
{ 11, "eleven" }
};
int test_func(void);
#endif /* !_TEST_H */
/*** END TEST.H ***/
/*** BEGIN TEST1.C ***/
#include <stdio.h>
#include "test.h"
int main(void)
{
return (0);
}
/*** END TEST1.C ***/
/*** BEGIN TEST2.C ***/
#include "test.h"
int test_func(void)
{
return (2);
}
/*** TEST2.C ***/
gcc -ggdb -o test test1.c test2.c
/var/tmp//ccZgBLuP.o(.data+0x0): multiple definition of `structs'
/var/tmp//ccLatz7e.o(.data+0x0): first defined here
Why is the header file parsed twice (and thus structs defined twice),
even when I started the header file with #ifndef etc etc statements?
You already got the answer, i.e. the files test1.c and test2.c get
compiled individually and a macro set in test1.c (via the included
file) doesn't exist anymore when the compiler is restarted to
compile test2.c and you thus end up with two instances of structs.

To avoid that never, ever define variables (or anything else) in
a header file. Define structs in one of the .c files and declare
it as extern in the other (or in the header file, it doesn't hurt
if a variable is both declared as extern and defined). An extern
declaration doesn't create an instance of structs, it just tells
the compiler that it is defined somewhere else, so it won't com-
plain that the variable doesn't exist.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Aug 9 '07 #3

P: n/a
Pietro Cerutti wrote:
>
suppose test1.c, test2.c and test.h

/*** BEGIN TEST.H ***/
#ifndef _TEST_H
#define _TEST_H

typedef struct
{
unsigned int code;
const char *name;
} test_struct;

test_struct structs[NOF_STRUCTS] =
{
{ 12, "twelve" },
{ 11, "eleven" }
};

int test_func(void);

#endif /* !_TEST_H */
/*** END TEST.H ***/

/*** BEGIN TEST1.C ***/
#include <stdio.h>
#include "test.h"

int main(void)
{
return (0);
}
/*** END TEST1.C ***/

/*** BEGIN TEST2.C ***/
#include "test.h"

int test_func(void)
{
return (2);
}
/*** TEST2.C ***/
gcc -ggdb -o test test1.c test2.c
/var/tmp//ccZgBLuP.o(.data+0x0): multiple definition of `structs'
/var/tmp//ccLatz7e.o(.data+0x0): first defined here

Why is the header file parsed twice (and thus structs defined twice),
even when I started the header file with #ifndef etc etc statements?
Because the ifndef statements only apply to the individual
compiles. You have committed the ultimate sin of defining a data
object in a header file. gcc is performing two compiles and then
calling the linker module.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Aug 9 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.