473,748 Members | 2,670 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_tit le(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 1375
Banfa
9,065 Recognized Expert Moderator Expert
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_tit le(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_tit le(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_tit le(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_tit le(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_tit le(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********@com cast.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.ne t
Feb 27 '06 #7

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

Similar topics

6
3434
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 to have the session_start(); at the top of the page as well. So when you have two things that need to be the first which do you put first? And if I put one before the other will that cause problems? Below is a copy of the php from my page. It...
3
3617
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 available to another header (as a member variable, for instance)? It (obviously) works to just include the .h file with the typedef in the desired header, but I know this is bad style and might cause problems in the future.
11
2760
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 understand the objective. This example may represent an interface in need of a bit of refactoring, but it goes to demonstrate the basic idea as I understand it. http://developer.kde.org/documentation/library/cvs-api/kdevelop/html/ast_8h-source.html...
0
999
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 adding functions named in French that delegate to the English equivalent. That makes life easier for the target reader and is consistent with the pedagogical philosophy of the original book. However the person responsible for adding code to...
12
2093
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. I'm trying to make class templates in seperate header files with pointers to each other, and I am getting strange errors. Is it possible to do?
2
1707
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 conversion automatically created an abstract base class for the header in the Migrated folder of the App_Code directory. One of the major problems this caused was regarding the fact that I pass the header to a standard error handling class to process the...
36
3851
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 program will result in a linker error complaining about multiple definitions. So this kind of definition should be avoided as much as possible. But as we know, the definition of a class is always in a header file. And we can use "#ifndef" to eliminate...
4
2485
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 m_Token = null in order to destroy the session token when the user logs out. However, I'm finding that setting class instance to null results in no header being sent back to the client, with the result that the client actually remains with an...
16
2086
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 and permutations use factorial to solve their problems, factorial is supposed to be in both perm.c and combo.c, and declared private. (1) You wouldn't ever put a prototype in a header if that function was
5
1971
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 inserting blank rows at the bottom. i can post the code if necessary for the C# but it basicly mirrors the vb.net -- (i''ll be asking a lot of these, but I find C# totally way cooler than vb
0
8995
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8832
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9561
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9381
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6799
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3316
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.