-
Multiply defined symbol
The following generates a multiply defined symbol error (LNK2005) in my
linker (MSVC6), leaving me mystified!
There are 3 files:
header.h
--------
#ifndef INCLUDE_TEST
#define INCLUDE_TEST
namespace A
{
int myinteger; //problem
enum token
{
NAME, NUMBER, END
};
int f();
}
#endif
imp.cpp
-------
#include "header.h"
A::f()
{
return 1;
}
driver.cpp
----------
#include "header.h"
int main()
{
return 1;
}
Apparently A::myinteger is already defined in driver.obj. Any ideas, as
I think I've got the wrong end of this particular stick.
Thanks, Chris
--
Ascent Software Limited Registered in Scotland: SC201671
www.ascent.zetnet.co.uk Bank Of Scotland Buildings
Lerwick, Shetland, ZE1 0EB
-
Re: Multiply defined symbol
Christopher Thomas McGinlay <ascent@zetnet.co.uk> wrote:
>The following generates a multiply defined symbol error (LNK2005) in my
>linker (MSVC6), leaving me mystified!
>
>There are 3 files:
>
>header.h
>--------
>#ifndef INCLUDE_TEST
>#define INCLUDE_TEST
>
>namespace A
>{
> int myinteger; //problem
> enum token
> {
> NAME, NUMBER, END
> };
> int f();
>}
>
>#endif
>
>imp.cpp
>-------
>#include "header.h"
>
>A::f()
>{
> return 1;
>}
>
>driver.cpp
>----------
>#include "header.h"
>
>int main()
>{
> return 1;
>}
>
>Apparently A::myinteger is already defined in driver.obj. Any ideas, as
>I think I've got the wrong end of this particular stick.
>
>Thanks, Chris
>
>--
>Ascent Software Limited Registered in Scotland: SC201671
>www.ascent.zetnet.co.uk Bank Of Scotland Buildings
> Lerwick, Shetland, ZE1 0EB
>
Somebody must have told you that 'namespaces' would resolve your 'extern'
declarations in multiple files. They do not.
They help to resolve ambiguities with-in a translation unit. Princple reason
for the existence of namespaces is to allow you to create multiple libraries
and worry less about name-collision. Not to KEEP it from happening but to
provide a way to resolve it when it does.
However, when the smoke clears and it is time to put all the units together
- you can have only "one" globally defined object/type/etc. with the same
name. Think of it as having to resolve to "one and only one" address where
the thing is kept in the final compiled code.
In the example above each .cpp file (translation unit) has the following:
// imp.cpp
int myinteger; // courtesy of header.h
// declared and defined global - there be storage
...
// driver.cpp
int myinteger; // courtesy of header.h
// declared and defined global - there be storage
...
// program.exe
// Error! Both translation units declare and define an
// address for a global int by the same name.
Use either 'extern', const, #define, or enum, as other respondents have suggested.
-
Re: Multiply defined symbol
>
> Somebody must have told you that 'namespaces' would resolve your 'extern'
> declarations in multiple files. They do not.
Aahh, well I have to be honest, I made up this misconception all by
myself! Now I do feel 'a bit of a wally' as it were. Never mind.
Thanks though!
Chris
--
Ascent Software Limited Registered in Scotland: SC201671
www.ascent.zetnet.co.uk Bank Of Scotland Buildings
Lerwick, Shetland, ZE1 0EB
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks