This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
duplicate definitions from linker
- From: Alf Salte <salte at socketip dot no>
- To: gcc-help at gcc dot gnu dot org
- Date: 14 Dec 2002 19:36:39 +0100
- Subject: duplicate definitions from linker
Hello,
I get error from the linker when trying to link my program. The linker
error is that I have duplicate definitions of several functions.
The functions in question is a function defined inline outside of the
class and in a file different from where the class is defined.
Essentially the setup is like this:
------- file A.hxx
class A {
/* Lots of other definitions */
bool func() const;
};
-------- end of file A.hxx
-------- file B.hxx
class B {
/* definition of B */
};
inline bool A::func() const
{ /* body here */ }
------------ end of file B.hxx
Appearantly the 'inline' is not enough hint for GCC to place the
function body in a linkonce section and so I get multiple definitions.
Problem is that I cannot define the function inline in A.hxx since the
function body refer to things defined in B.hxx and I did try something
like:
class A;
extern const C A::var;
in A.hxx which - if it worked - would allow me to define the function
inline in A.hxx but the compiler complained and said that I couldn't do
such a thing, Although a class can be incompletely defined by 'class X;'
you get only the class name available so that this class can be referred
to. You cannot refer to things inside that class, For example I also
tried to do things like:
class A;
enum A::E;
to tell the compiler that there is a enum type E in the class A but that
didn't work.
So, I ended up defining the function inline in B.hxx instead but now the
linker complains about multiple definitions and so my question is, what
can I do to resolve the situation?
I cannot easily combine the two header files A.hxx and B.hxx into one as
they are actually in separate directories and define different types
belonging to different modules of the program however, they are closely
related as indicated above.
What I would like to do is to be able to define the function inline in
the class where it is, but since that didn't work, my next thing would
be to be able to tell the compiler something like:
"This function is inline and may therefore appear in several .o files
after compiling, if so, it should link one and ignore the others and not
report it as an error that there are several definitions of the
function." i.e. "put this function definition in a "link once" section.
It appears that a simple 'inline ...' is not enough to do this with GCC
3.2 and so I ask what WILL work to resolve this problem?
Alf Salte