Connecting Tech Pros Worldwide Forums | Help | Site Map

name conflict

John
Guest
 
Posts: n/a
#1: Oct 1 '05
I am using two large libraries which both have an implementation
of a "matrix" class. I have the source code for both.
I need to use them in the same project and when I try to compile
I get a double declaration conflict. I tried to wrap the smaller
library into a namespace but its a nightmare. Is there anyway
I could solve this problem without giving up on one of the libraries?

Thanks,
--j


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Jack Klein
Guest
 
Posts: n/a
#2: Oct 1 '05

re: name conflict


On 30 Sep 2005 21:55:24 -0400, "John" <weekender_ny@yahoo.com> wrote
in comp.lang.c++:
[color=blue]
> I am using two large libraries which both have an implementation
> of a "matrix" class. I have the source code for both.
> I need to use them in the same project and when I try to compile
> I get a double declaration conflict. I tried to wrap the smaller
> library into a namespace but its a nightmare. Is there anyway
> I could solve this problem without giving up on one of the libraries?
>
> Thanks,
> --j[/color]

Yes, if you have the source code for both of them, do a search and
replace in one source code tree and change the conflicting name.

It's a bit of work, but this is only a real problem when you don't
have and can't get the source code.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Rolf Magnus
Guest
 
Posts: n/a
#3: Oct 1 '05

re: name conflict


John wrote:
[color=blue]
> I am using two large libraries which both have an implementation
> of a "matrix" class. I have the source code for both.
> I need to use them in the same project and when I try to compile
> I get a double declaration conflict. I tried to wrap the smaller
> library into a namespace but its a nightmare.[/color]

Why is that a nightmare?
[color=blue]
> Is there anyway I could solve this problem without giving up on one of the
> libraries?[/color]

No standard C++ solution, AFAICS. Maybe there is something in your compiler
or linker that might help.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

benben
Guest
 
Posts: n/a
#4: Oct 2 '05

re: name conflict



"John" <weekender_ny@yahoo.com> wrote in message
news:1128053226.420703.320830@o13g2000cwo.googlegr oups.com...[color=blue]
>I am using two large libraries which both have an implementation
> of a "matrix" class. I have the source code for both.
> I need to use them in the same project and when I try to compile
> I get a double declaration conflict. I tried to wrap the smaller
> library into a namespace but its a nightmare. Is there anyway
> I could solve this problem without giving up on one of the libraries?
>
> Thanks,[/color]

Option 1 Do not use them together. Look for alternative libraries. This
option is preferred when you have the freedom to choose between libraries.

Option 2 Use them separately. This means #include'ing header(s) of only
one of the conflicting libraries in any one translation units. This option
is preferred when the two conflicting libraries can be used in separate
places of your program, i.e. the uses of them are not intertwined.

Option 3 Provide wrapper classes/functions for one of the two libraries or
both. Put your own abstractions in separate namespaces and only #include the
troublesome library headers in the implementation of your own abstraction
layer (so by #including your own abstraction layer headers won't pull in the
library headers). This potentially can be expensive but it always works.

Ben





[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

albrecht.fritzsche
Guest
 
Posts: n/a
#5: Oct 2 '05

re: name conflict


John wrote:[color=blue]
> I am using two large libraries which both have an implementation
> of a "matrix" class. I have the source code for both.
> I need to use them in the same project and when I try to compile
> I get a double declaration conflict. I tried to wrap the smaller
> library into a namespace but its a nightmare.[/color]

Why is this a nightmare? Inserting in every header and source file
of both implementations

namespace SmallMatrix { // or namespace BigMatrix {

and

}

should be straigtforward, shouldn't it?

Ali

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Greg
Guest
 
Posts: n/a
#6: Oct 2 '05

re: name conflict



John wrote:[color=blue]
> I am using two large libraries which both have an implementation
> of a "matrix" class. I have the source code for both.
> I need to use them in the same project and when I try to compile
> I get a double declaration conflict. I tried to wrap the smaller
> library into a namespace but its a nightmare. Is there anyway
> I could solve this problem without giving up on one of the libraries?
>
> Thanks,
> --j[/color]

This is exactly the type of problem that C++ namespaces should be able
to handle. It's unclear what what part of the process turned into a
nightmare, or even how the name conflict manifests itself in the first
place. Does it cause a compiler error or just a linker error? Are both
matrix classes used in the source files, or is it an include file
problem? If both matrix classes are in use, does any single source file
use both?

Ideally, it should be possible to place both the interface and the
implementation of one of the libraries into a namespace. Just place a
"namespace MathLib { " at the top of the header and the source file and
a closing } at the end of both files (of course the namespace's name
doesn't have to be "Mathlib", in fact it's possible to alias
namespaces, so more than one name is possible). And although header
files should not have "using" directives that import names into the
global namespace; there is no problem importing global (or names from
other namespaces) into a namespace. Doing so may resolve difficulties
caused by having placed the interface into a namespace.

Greg


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

André Kempe
Guest
 
Posts: n/a
#7: Oct 2 '05

re: name conflict


John schrieb:[color=blue]
> I am using two large libraries which both have an implementation
> of a "matrix" class. I have the source code for both.
> I need to use them in the same project and when I try to compile
> I get a double declaration conflict. I tried to wrap the smaller
> library into a namespace but its a nightmare. Is there anyway
> I could solve this problem without giving up on one of the libraries?
>
> Thanks,
> --j[/color]
Are these libraries "headers-only" libraries like MTL?
Maybe a

namespace matrix_lib_1 {
#inlude "headers_of_lib_1.hpp"
}

could do it?

Andre

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

James Kanze
Guest
 
Posts: n/a
#8: Oct 2 '05

re: name conflict


Jack Klein wrote:[color=blue]
> On 30 Sep 2005 21:55:24 -0400, "John" <weekender_ny@yahoo.com> wrote
> in comp.lang.c++:[/color]
[color=blue][color=green]
>>I am using two large libraries which both have an
>>implementation of a "matrix" class. I have the source code for
>>both. I need to use them in the same project and when I try
>>to compile I get a double declaration conflict. I tried to
>>wrap the smaller library into a namespace but its a
>>nightmare. Is there anyway I could solve this problem without
>>giving up on one of the libraries?[/color][/color]
[color=blue]
> Yes, if you have the source code for both of them, do a search
> and replace in one source code tree and change the conflicting
> name.[/color]

If you have the sources, wrapping one (or both) of them in a
namespace is fairly straight forward.
[color=blue]
> It's a bit of work, but this is only a real problem when you
> don't have and can't get the source code.[/color]

If you don't have the sources, there's not really much you can
do.

--
James Kanze mailto: james.kanze@free.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 pl. Pierre Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

werasmus
Guest
 
Posts: n/a
#9: Oct 3 '05

re: name conflict



John wrote:[color=blue]
> I am using two large libraries which both have an implementation
> of a "matrix" class. I have the source code for both.
> I need to use them in the same project and when I try to compile
> I get a double declaration conflict. I tried to wrap the smaller
> library into a namespace but its a nightmare. Is there anyway
> I could solve this problem without giving up on one of the libraries?[/color]

namespace SmallLib{

#include <mySmallLib.h>

}//end namespace SmallLib

Hope this helps,

W

[color=blue]
>
> Thanks,
> --j[/color]


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

kanze
Guest
 
Posts: n/a
#10: Oct 3 '05

re: name conflict


benben wrote:[color=blue]
> "John" <weekender_ny@yahoo.com> wrote in message
> news:1128053226.420703.320830@o13g2000cwo.googlegr oups.com...[color=green]
> >I am using two large libraries which both have an
> >implementation of a "matrix" class. I have the source code
> >for both. I need to use them in the same project and when I
> >try to compile I get a double declaration conflict. I tried
> >to wrap the smaller library into a namespace but its a
> >nightmare. Is there anyway I could solve this problem without
> >giving up on one of the libraries?[/color][/color]

[...][color=blue]
> Option 2 Use them separately. This means #include'ing
> header(s) of only one of the conflicting libraries in any one
> translation units. This option is preferred when the two
> conflicting libraries can be used in separate places of your
> program, i.e. the uses of them are not intertwined.[/color]

That's not necessarily sufficient. What happens if the two
classes both have defaut constructors, for example. The linker
will pull in at most one, and you'll end up with the constructor
for one of the classes invoked to construct instances of the
other.
[color=blue]
> Option 3 Provide wrapper classes/functions for one of the two
> libraries or both. Put your own abstractions in separate
> namespaces and only #include the troublesome library headers
> in the implementation of your own abstraction layer (so by
> #including your own abstraction layer headers won't pull in
> the library headers). This potentially can be expensive but it
> always works.[/color]

It shares the same problem with linking as the above.

It can be made to work if you put the two matrix classes in
separate dynamically loaded objects.

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Closed Thread