how to deal with duplicate class names

4

4MLA1FN

i'm somewhat of a c++ newbie. i'm linking some static libraries into
my app. it turns out that two of the libraries (from different
suppliers) share a class name; e.g. they both have a class named
'SomeClass'. the compiler/linker is complaining. is there an easy
way deal with this so my app can link? thanks.
 
A

Alan Johnson

4MLA1FN said:
i'm somewhat of a c++ newbie. i'm linking some static libraries into
my app. it turns out that two of the libraries (from different
suppliers) share a class name; e.g. they both have a class named
'SomeClass'. the compiler/linker is complaining. is there an easy
way deal with this so my app can link? thanks.

Create proxy class. That is, one that implements all of the same
methods as one of the classes with which you are having problems, and
have it simply forward all method calls to an instance of the class for
which it is proxying. If needed, use the PIMPL idiom (see Google).
 
A

Alf P. Steinbach

* 4MLA1FN:
i'm somewhat of a c++ newbie. i'm linking some static libraries into
my app. it turns out that two of the libraries (from different
suppliers) share a class name; e.g. they both have a class named
'SomeClass'. the compiler/linker is complaining. is there an easy
way deal with this so my app can link?

No, not as far as I know, sorry.

But you can pursue solutions outside the language.

For example, in Windows you can isolate the usage of one of the classes
to a dynamically linked library.

Or, for example, you can check whether the same functionality is
available from some higher quality libraries, which you should probably
do anyway -- using low-quality libraries will yield more and more
problems, and can force you to use less than good design.

A quality C++ library will use a namespace to greatly reduce the chance
of name collisions, so that's one thing to look for.
 
M

Maxim Yegorushkin

Alan said:
Create proxy class. That is, one that implements all of the same
methods as one of the classes with which you are having problems, and
have it simply forward all method calls to an instance of the class for
which it is proxying. If needed, use the PIMPL idiom (see Google).

This does not solve the problem. The problem is that One Definition
Rule is violated and you have several different external linkage
entities with the same name in your application. Adding a proxy to the
mix does not make the entity being proxied go away.
 
R

Rolf Magnus

Maxim said:
This does not solve the problem. The problem is that One Definition
Rule is violated and you have several different external linkage
entities with the same name in your application. Adding a proxy to the
mix does not make the entity being proxied go away.

Well, with standard C++, you're right. But the linker might offer some way
to only export symbols that are explicitly marked for that (e.g. in a
linker script). That can be used to make only the proxy class visible for
the main program.
 
B

benben

use namespaces, this is exactly what they are for.

I think the OP doesn't have that choice. Read the post again and you'll
find it is the library code (not OP's code) that caused the problem.

Putting library code in your own namespace is not only a substantial
work but is also unsupported, tedious, and error-prone.

Regards,
Ben
 
M

Maxim Yegorushkin

Rolf said:
Well, with standard C++, you're right. But the linker might offer some way
to only export symbols that are explicitly marked for that (e.g. in a
linker script). That can be used to make only the proxy class visible for
the main program.

In standard C++ one had best be using namespaces to solve the problem.
 
R

Rolf Magnus

Maxim said:
In standard C++ one had best be using namespaces to solve the problem.

With "one" in this case being those that wrote the libraries that the OP is
using. Noting that namespaces are there to avoid that problem won't help
the OP solving it.
 
T

Tomás

4MLA1FN posted:
i'm somewhat of a c++ newbie. i'm linking some static libraries into
my app. it turns out that two of the libraries (from different
suppliers) share a class name; e.g. they both have a class named
'SomeClass'. the compiler/linker is complaining. is there an easy
way deal with this so my app can link? thanks.


Maybe you could find some tool for altering the static library file? (Even
a text editor?). If you find such a tool, the handiest solution would be:

1) Put everything in a namespace.


Failing that, you could:

2) Rename the classes.


You'd probably need a fancy tool to achieve the first choice, but I'd say
that something as trivial as a HexEditor could achieve the second choice.


-Tomás
 
4

4MLA1FN

No, not as far as I know, sorry.

thank you very much alf and all others that posted. i was
hoping/fantasizing that there was a way to prefix symbols in a library
at link time. guess not. i suspect i can get the source for the
smaller of the two libraries, so i'll try to do the namespace
solution. (not exactly sure how just yet; i think i need to put the
class in a namespace x, then in each .h/.cpp where the class is used,
i need to add a 'using x' declaration?? ). alternatively, i'll make a
little perl script to search-and-replace the class name to something
unique. (ugly solution, i know, but it's quick.)
Or, for example, you can check whether the same functionality is
available from some higher quality libraries...

fortunately, these are libraries from pretty escoteric academic
research, which increases the likelihood i can get the source.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top