Windows Vista Beta | WinVistaBeta.com - Message | multiple symbol linker issue w/VC++ 2005

November 19, 2008  
Subject: multiple symbol linker issue w/VC++ 2005
Group: microsoft.public.vstudio.general
Date: 7/23/2008 6:44:47 PM
From: Ted Byers [Email Address Protection]

I have been programming C++ for years, but mostly with other tools. I
do not recall encountering this issue, e.g. with Borland's C++ Builder
or with gcc.

I was surprised today to encounter multiple symbol definitions at link
time because I happened to use <iostream> in multiple compilation
units. I found a way to get around it, but I am not happy with it.
It seems silly to me. I use <iosfwd> in the headers, and <iostream>
in the corresponding compilation units, to provide an overload to the
output operator for each of a number of classes; and this error
strikes. I used the /force option for the linker, so now it compiles;
a compromise for me since I know the symbols are identical, from the
same source code, but I tend to regard all warnings as obscenities to
be purged from my code as vigorously as errors (indeed, my preference
is to pass my code through a number of tools, to take advantage of the
differing strengths of different tool chains, and mitigate their
weaknesses).

It seems odd that VC++ 2005 would have a problem with this one part of
the standard library.

Is there a way to avoid such an issue without using /force (and the
concommitant transformation of the linker error into a warning)?

Thanks

Ted

Back
Subject: Re: multiple symbol linker issue w/VC++ 2005
Group: microsoft.public.vstudio.general
Date: 7/23/2008 7:00:34 PM
From: David Wilkinson [Email Address Protection]

Ted Byers wrote:
> I have been programming C++ for years, but mostly with other tools. I
> do not recall encountering this issue, e.g. with Borland's C++ Builder
> or with gcc.
>
> I was surprised today to encounter multiple symbol definitions at link
> time because I happened to use <iostream> in multiple compilation
> units. I found a way to get around it, but I am not happy with it.
> It seems silly to me. I use <iosfwd> in the headers, and <iostream>
> in the corresponding compilation units, to provide an overload to the
> output operator for each of a number of classes; and this error
> strikes. I used the /force option for the linker, so now it compiles;
> a compromise for me since I know the symbols are identical, from the
> same source code, but I tend to regard all warnings as obscenities to
> be purged from my code as vigorously as errors (indeed, my preference
> is to pass my code through a number of tools, to take advantage of the
> differing strengths of different tool chains, and mitigate their
> weaknesses).
>
> It seems odd that VC++ 2005 would have a problem with this one part of
> the standard library.
>
> Is there a way to avoid such an issue without using /force (and the
> concommitant transformation of the linker error into a warning)?

Ted:

One of the Visual C++ groups would be better for this question.

This should not be happening. What do the linker errors say exactly?

--
David Wilkinson
Visual C++ MVP

Back
Subject: Re: multiple symbol linker issue w/VC++ 2005
Group: microsoft.public.vstudio.general
Date: 7/23/2008 7:27:39 PM
From: Ted Byers [Email Address Protection]

On Jul 23, 10:00=A0pm, David Wilkinson <no-re...@effisols.com> wrote:
> Ted Byers wrote:
> > I have been programming C++ for years, but mostly with other tools. =A0=
I
> > do not recall encountering this issue, e.g. with Borland's C++ Builder
> > or with gcc.
>
> > I was surprised today to encounter multiple symbol definitions at link
> > time because I happened to use <iostream> in multiple compilation
> > units. =A0I found a way to get around it, but I am not happy with it.
> > It seems silly to me. =A0I use <iosfwd> in the headers, and <iostream>
> > in the corresponding compilation units, to provide an overload to the
> > output operator for each of a number of classes; and this error
> > strikes. =A0I used the /force option for the linker, so now it compiles=
;
> > a compromise for me since I know the symbols are identical, from the
> > same source code, but I tend to regard all warnings as obscenities to
> > be purged from my code as vigorously as errors (indeed, my preference
> > is to pass my code through a number of tools, to take advantage of the
> > differing strengths of different tool chains, and mitigate their
> > weaknesses).
>
> > It seems odd that VC++ 2005 would have a problem with this one part of
> > the standard library.
>
> > Is there a way to avoid such an issue without using /force (and the
> > concommitant transformation of the linker error into a warning)?
>
> Ted:
>
> One of the Visual C++ groups would be better for this question.
>
> This should not be happening. What do the linker errors say exactly?
>
> --
> David Wilkinson
> Visual C++ MVP- Hide quoted text -
>
> - Show quoted text -

Hi David

The first is:

Error 3 error LNK2005: "class std::basic_ostream<char,struct
std::char_traits<char> > & __cdecl operator<<(class
std::basic_ostream<char,struct std::char_traits<char> > &,class
investment_holding const &)" (??6@YAAAV?$basic_ostream@DU?
$char_traits@D@std@@@std@@AAV01@ABVinvestment_holding@@@Z) already
defined in MemoryBasedPortfolio.obj portfolio_snapshot.obj


The second is:

Error 4 error LNK2005: "class std::basic_ostream<char,struct
std::char_traits<char> > & __cdecl operator<<(class
std::basic_ostream<char,struct std::char_traits<char> > &,class
investment_holding const &)" (??6@YAAAV?$basic_ostream@DU?
$char_traits@D@std@@@std@@AAV01@ABVinvestment_holding@@@Z) already
defined in MemoryBasedPortfolio.obj portfolio.obj

And of course these are followed by:

Error 5 fatal error LNK1169: one or more multiply defined symbols
found K:\MSVSProjects\MemoryBasedPortfolio\Debug
\MemoryBasedPortfolio.exe 1


The investment_holding compilation unit has an operator<<, as do a
couple others that happen to contain a container (usually an STL set)
of investment_holding instances.

Any ideas?

I can show you the operator<<() overloads, if that would help.

Thanks

Ted

Back
Subject: Re: multiple symbol linker issue w/VC++ 2005
Group: microsoft.public.vstudio.general
Date: 7/24/2008 3:14:14 AM
From: David Wilkinson [Email Address Protection]

Ted Byers wrote:
> On Jul 23, 10:00�pm, David Wilkinson <no-re...@effisols.com> wrote:
>> Ted Byers wrote:
>>> I have been programming C++ for years, but mostly with other tools. �I
>>> do not recall encountering this issue, e.g. with Borland's C++ Builder
>>> or with gcc.
>>> I was surprised today to encounter multiple symbol definitions at link
>>> time because I happened to use <iostream> in multiple compilation
>>> units. �I found a way to get around it, but I am not happy with it.
>>> It seems silly to me. �I use <iosfwd> in the headers, and <iostream>
>>> in the corresponding compilation units, to provide an overload to the
>>> output operator for each of a number of classes; and this error
>>> strikes. �I used the /force option for the linker, so now it compiles;
>>> a compromise for me since I know the symbols are identical, from the
>>> same source code, but I tend to regard all warnings as obscenities to
>>> be purged from my code as vigorously as errors (indeed, my preference
>>> is to pass my code through a number of tools, to take advantage of the
>>> differing strengths of different tool chains, and mitigate their
>>> weaknesses).
>>> It seems odd that VC++ 2005 would have a problem with this one part of
>>> the standard library.
>>> Is there a way to avoid such an issue without using /force (and the
>>> concommitant transformation of the linker error into a warning)?
>> Ted:
>>
>> One of the Visual C++ groups would be better for this question.
>>
>> This should not be happening. What do the linker errors say exactly?
>>
>> --
>> David Wilkinson
>> Visual C++ MVP- Hide quoted text -
>>
>> - Show quoted text -
>
> Hi David
>
> The first is:
>
> Error 3 error LNK2005: "class std::basic_ostream<char,struct
> std::char_traits<char> > & __cdecl operator<<(class
> std::basic_ostream<char,struct std::char_traits<char> > &,class
> investment_holding const &)" (??6@YAAAV?$basic_ostream@DU?
> $char_traits@D@std@@@std@@AAV01@ABVinvestment_holding@@@Z) already
> defined in MemoryBasedPortfolio.obj portfolio_snapshot.obj
>
>
> The second is:
>
> Error 4 error LNK2005: "class std::basic_ostream<char,struct
> std::char_traits<char> > & __cdecl operator<<(class
> std::basic_ostream<char,struct std::char_traits<char> > &,class
> investment_holding const &)" (??6@YAAAV?$basic_ostream@DU?
> $char_traits@D@std@@@std@@AAV01@ABVinvestment_holding@@@Z) already
> defined in MemoryBasedPortfolio.obj portfolio.obj
>
> And of course these are followed by:
>
> Error 5 fatal error LNK1169: one or more multiply defined symbols
> found K:\MSVSProjects\MemoryBasedPortfolio\Debug
> \MemoryBasedPortfolio.exe 1
>
>
> The investment_holding compilation unit has an operator<<, as do a
> couple others that happen to contain a container (usually an STL set)
> of investment_holding instances.
>
> Any ideas?
>
> I can show you the operator<<() overloads, if that would help.
>
> Thanks

Ted:

These errors have to do with your own code, so it is "premature" to blame the
compiler.

If these operator <<'s are defined in the header file but not inside the class
definition, then they must be declared inline, or moved to the implementation file.

--
David Wilkinson
Visual C++ MVP

Back