473,385 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Header file included more than once?

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
3 2051
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: qazmlp | last post by:
I need to include a list of - C++ headers - headers of other modules - headers of my module - implementation specific ie.OS headers In what order, they should be included in my .CPP file?...
31
by: Steven T. Hatton | last post by:
If a header is not necessarily a source file, and the sequences delimited by < and > in header names aren't necessarily valid source file names, what exactly is a header? -- p->m == (*p).m == p.m...
16
by: matthurne | last post by:
I just started learning C++ on my own...I'm using Accelerated C++. Something it hasn't explained and I keep wondering about is how header files actually work. I suspect it doesn't get into it...
11
by: Steven T. Hatton | last post by:
In the past there have been lengthy discussiions regarding the role of header files in C++. People have been very adamat about header files serving as in interface to the implementation. I do...
18
by: Exits Funnel | last post by:
Hello, I'm a little confused about where I should include header files and was wondering whether there was some convention. Imagine I've written a class foo and put the definition in foo.h and...
11
by: wenmang | last post by:
Hi, all: What will happen if header files with the exact same names but with different contents and declared/defined without include guard reside in the include path for the compiler? Are these...
11
by: ambika | last post by:
Iam just trying to know "c". And I have a small doubt about these header files. The header files just contain the declaration part...Where is the definition for these declarations written??And how...
60
by: Derrick Coetzee | last post by:
It seems like, in every C source file I've ever seen, there has been a very definite include order, as follows: - include system headers - include application headers - include the header...
18
by: John Smith | last post by:
Hi all What does the group think of the practise of including one header file from inside another? I have some legacy code where this has been done, and it creates a dependency on a module...
3
by: fc2004 | last post by:
Hi, Is there any tools that could report where cyclic header dependency happens? this would be useful when working with a large project where tens or hundreds of headers files may form complex...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.