473,388 Members | 1,327 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,388 software developers and data experts.

Header problems

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

Feb 15 '06 #1
6 1356
Banfa
9,065 Expert Mod 8TB
The header guards protect you from including the same header more than once into a single c file.

It does not stop the header being included into multiple C files otherwise only 1 file would be able to access the definitions in any header file.

The reason you have multiple definitions is that you have put a data definition in the header file, header files should only contain declarations (of data or functions), the data should be defined in a C file else where.

const char DEF_TITLE[] = "My Title";

should be

extern const char DEF_TITLE[] = "My Title";

you should put

const char DEF_TITLE[] = "My Title";

in 1 of your c files.
Feb 15 '06 #2
"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

Feb 15 '06 #3
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.
Feb 15 '06 #4
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>
Feb 15 '06 #5
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.
Feb 16 '06 #6
On Wed, 15 Feb 2006 10:24:38 -0800, "osmium" <r1********@comcast.net>
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
Feb 27 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: John | last post by:
Hi. I am having a few header problems at the moment with a login page. I don't have my headers at the top of the page where I've learned I need to have them. However, I also know I'm supposed...
3
by: marsboee | last post by:
This must have been the MFC (or basic C++) class I slept through: I have a typedef in a header file (for a CView subclass). I want to use this type in another .h file. How do I make this type...
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...
0
by: Francis Glassborow | last post by:
'You Can Do It!' has just been published in French as 'Je me lance!', not this is not an advertising pitch but an appeal for your help. Quite sensibly the translators have augmented my library by...
12
by: Ben | last post by:
I'm kind of new to creating templates. I've made some small class and function templates in the past and I have used quite of bit of the STL, but I am having problems tyring to create templates. ...
2
by: DCC700 | last post by:
VS 2005 Converted Header causing error when publishing After converting from Visual Studio 2003 to 2005, I have had several issues with a header that is used throughout the project. The...
36
by: zouyongbin | last post by:
Stanley B Lippman in his "C++ Primer" that a definition like this should not appear in a header file: int ix; The inclusion of any of these definitions in two or more files of the same...
4
by: Joseph Geretz | last post by:
We use a Soap Header to pass a token class (m_Token) back and forth with authenticated session information. Given the following implementation for our Logout method, I vastly prefer to simply code...
16
by: wdh3rd | last post by:
Hi everyone. I'm new to C and I have a few questions: I am making files for permutations and combinations. Files to be made are perm.c, perm.h, combo.c, and combo.h. Since both combinations...
5
by: =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?= | last post by:
I have a sub in vb.net that adds extra headers to a gridview and it works very well. however, i tried to translate it to c# and i'm getting the header inserting itself over the first datarows and...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.