Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Header problems

Reply
Thread Tools

Header problems

 
 
gamehack
Guest
Posts: n/a
 
      02-15-2006
Hi all,

Let me describe the problem. I've a header file util.h which contains
the following:
#ifndef UTIL_H_
#define UTIL_H_

const char DEF_TITLE[] = "My Title";

int num_chars(int num);
char* get_default_title(void);

#endif /* UTIL_H_ */

When I include the header from two or more files in complains that
DEF_TITLE is defined more than once: "multiple definition of
`DEF_TITLE`" is printed for both files which use DEF_TITLE. I cannot
understand why this happens since I've put the header guards. Platform
is MinGW 3.4.2 & Win XP Pro.

Regards

 
Reply With Quote
 
 
 
 
osmium
Guest
Posts: n/a
 
      02-15-2006
"gamehack" writes:

> Let me describe the problem. I've a header file util.h which contains
> the following:
> #ifndef UTIL_H_
> #define UTIL_H_
>
> const char DEF_TITLE[] = "My Title";


Header guards do not protect against multiple definitions. Each program
segment that includes this header gets exactly one definition for DEF_TITLE.
There should only be *type* definitions or variable *declarations* in a
header.

Change it to external (q.v.) which will demote it to a declaration. Put a
definition in one of the files where it makes sense.

>
> int num_chars(int num);
>
> char* get_default_title(void);
>
> #endif /* UTIL_H_ */
>
> When I include the header from two or more files in complains that
> DEF_TITLE is defined more than once: "multiple definition of
> `DEF_TITLE`" is printed for both files which use DEF_TITLE. I cannot
> understand why this happens since I've put the header guards. Platform
> is MinGW 3.4.2 & Win XP Pro.
>
> Regards
>



 
Reply With Quote
 
 
 
 
Michael Mair
Guest
Posts: n/a
 
      02-15-2006
gamehack schrieb:
> Hi all,
>
> Let me describe the problem. I've a header file util.h which contains
> the following:
> #ifndef UTIL_H_
> #define UTIL_H_
>
> const char DEF_TITLE[] = "My Title";


DEF_TITLE is a file scope identifier with external linkage.
It must not be used more than once as such in the whole
programme. So, if you #include "util.h" in more than one
translation unit, this thing breaks.
>
> int num_chars(int num);
> char* get_default_title(void);
>
> #endif /* UTIL_H_ */
>
> When I include the header from two or more files in complains that
> DEF_TITLE is defined more than once: "multiple definition of
> `DEF_TITLE`" is printed for both files which use DEF_TITLE. I cannot
> understand why this happens since I've put the header guards.


Header guards protect against
-- a.h --
#include "b.h"
---------
-- b.h --
#include "a.h"
---------
and multiple definitions of types.

Consider

-- util.h --
#ifndef UTIL_H_
#define UTIL_H_

extern const char *DEF_TITLE;

int num_chars(int num);
char* get_default_title(void);

#endif /* UTIL_H_ */
------------
-- util.c --
#include "util.h"

const char *DEF_TITLE = "My Title";

.....

------------

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
Pedro Graca
Guest
Posts: n/a
 
      02-15-2006
gamehack wrote:
> Let me describe the problem. I've a header file util.h which contains
> the following:
> #ifndef UTIL_H_
> #define UTIL_H_
>
> const char DEF_TITLE[] = "My Title";


Why not

#define DEF_TITLE "My Title" /* no semicolon */

> int num_chars(int num);
> char* get_default_title(void);
>
> #endif /* UTIL_H_ */


And then use it in your program as a constant string

/* ... */
char title1[] = DEF_TITLE;
char *title2 = NULL;
/* ... including allocating space for title2 ... */
strcpy(title2, DEF_TITLE);
/* ... */
printf("Title: %s\n", title2 ? title2 : DEF_TITLE);
/* ... */

--
If you're posting through Google read <http://cfaj.freeshell.org/google>
 
Reply With Quote
 
Michael Mair
Guest
Posts: n/a
 
      02-16-2006
Pedro Graca schrieb:
> gamehack wrote:
>
>>Let me describe the problem. I've a header file util.h which contains
>>the following:
>>#ifndef UTIL_H_
>>#define UTIL_H_
>>
>>const char DEF_TITLE[] = "My Title";

>
>
> Why not
>
> #define DEF_TITLE "My Title" /* no semicolon */
>
>
>>int num_chars(int num);
>>char* get_default_title(void);
>>
>>#endif /* UTIL_H_ */

>
>
> And then use it in your program as a constant string
>
> /* ... */
> char title1[] = DEF_TITLE;
> char *title2 = NULL;
> /* ... including allocating space for title2 ... */
> strcpy(title2, DEF_TITLE);
> /* ... */
> printf("Title: %s\n", title2 ? title2 : DEF_TITLE);
> /* ... */


This
- does not protect you against inadvertently trying
to modify DEF_TITLE
- potentially wastes storage if "My Title" is stored
multiple times (e.g. once per translation unit, once
per function, once per use...)

String literals should be used with care.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
Dave Thompson
Guest
Posts: n/a
 
      02-27-2006
On Wed, 15 Feb 2006 10:24:38 -0800, "osmium" <(E-Mail Removed)>
wrote:

> "gamehack" writes:
>
> > Let me describe the problem. I've a header file util.h which contains
> > the following:
> > #ifndef UTIL_H_
> > #define UTIL_H_
> >
> > const char DEF_TITLE[] = "My Title";

>
> Header guards do not protect against multiple definitions. Each program
> segment that includes this header gets exactly one definition for DEF_TITLE.


More specifically, each 'translation unit' = source file + includes.

> There should only be *type* definitions or variable *declarations* in a
> header.
>
> Change it to external (q.v.) which will demote it to a declaration. Put a
> definition in one of the files where it makes sense.
>

Just adding 'extern' (not 'external', and not replacing anything
already there) to a declaration with an initializer doesn't help; it's
still a definition. You need to declare with extern and no
initializer, and then (as you say) define in one t.u.

It may be a problem (or not) that in that scheme the declaration
extern const char DEF_TITLE [] ;
has unknown bound/size, whereas with the initializer it has a known
size from the initial value even though not explicitly written. If
this matters you can duplicate the value in each t.u. as
static const char DEF_TITLE [] = "Value";
(It is even possible, and certainly permitted though not required,
that the implementation might be clever enough to merge these.)


- David.Thompson1 at worldnet.att.net
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Header files with "header.h" or <header.h> ?? mlt C++ 2 01-31-2009 02:54 PM
UNIX header files to Windows header files anand.ba@gmail.com C Programming 3 05-01-2006 03:57 PM
Header files included in header files John Smith C Programming 18 07-24-2004 04:55 AM
What is better /standard for creating files. a cpp file with header or cpp and seperate file for header DrUg13 C++ 1 02-10-2004 09:20 AM
how to avoid using another header file inside a header file? Newsgroup - Ann C++ 4 11-02-2003 01:20 PM



Advertisments