error LNK2005:already defined

R

Rohini

Hi ,

I am getting the following LINK 2005 error when I tried to build my
project in vc++7.1.
I am doing the build process in win32release mode.

I have searched google on this but whatever I found was related to
standard dll's and lib's.

Here in my case it is not a standard lib.It is generated by a different
project in another workspace.

-----------------------------------------------------------------------------------------------------------------------------------
ghjkl.lib(ghjkl.dll) : error LNK2005: "public: int __thiscall
OSF_Task<class OSF_Thread_Mutex,class
OSF_Condition_Thread_Mutex>::getq(class OSF_Message_Block * &,class
OSF_Time_Value *)"
(?getq@?$OSF_Task@VOSF_Thread_Mutex@@VOSF_Condition_Thread_Mutex@@@@QAEHAAPAVOSF_Message_Block@@PAVOSF_Time_Value@@@Z)
already defined in abc.obj
-------------------------------------------------------------------------------------------------------------------------------------

The function getq(class OSF_Message_Block * &,class OSF_Time_Value *)
is not defined in abc.cpp but it is declared and defined in another
header file asd.h.

I am putting the relevant code below.

***asd.h
----------------------------------------------------------------------------------------------------------------------------------
class ghj{
typedef STL_Message_Queue<OSF_Recursive_Thread_Mutex, OSF_Manual_Event>
MessageQueue;

int getq(OSF_Message_Block*& mb, OSF_Time_Value* tv=0)
{
return mq_->dequeue_head(mb,tv);
}


private:
MessageQueue* mq_;

}
-------------------------------------------------------------------------------------------------------------------------------------
Any help is appreciated.

Thanks in advance

Best Regards,
Rohini Chandra
 
C

Carl Daniel [VC++ MVP]

Rohini said:
Hi ,

I am getting the following LINK 2005 error when I tried to build my
project in vc++7.1.
I am doing the build process in win32release mode.

I have searched google on this but whatever I found was related to
standard dll's and lib's.

Here in my case it is not a standard lib.It is generated by a different
project in another workspace.

-----------------------------------------------------------------------------------------------------------------------------------
ghjkl.lib(ghjkl.dll) : error LNK2005: "public: int __thiscall
OSF_Task<class OSF_Thread_Mutex,class
OSF_Condition_Thread_Mutex>::getq(class OSF_Message_Block * &,class
OSF_Time_Value *)"
(?getq@?$OSF_Task@VOSF_Thread_Mutex@@VOSF_Condition_Thread_Mutex@@@@QAEHAAPAVOSF_Message_Block@@PAVOSF_Time_Value@@@Z)
already defined in abc.obj
-------------------------------------------------------------------------------------------------------------------------------------

The function getq(class OSF_Message_Block * &,class OSF_Time_Value *)
is not defined in abc.cpp but it is declared and defined in another
header file asd.h.

I am putting the relevant code below.

***asd.h
----------------------------------------------------------------------------------------------------------------------------------
class ghj{
typedef STL_Message_Queue<OSF_Recursive_Thread_Mutex, OSF_Manual_Event>
MessageQueue;

int getq(OSF_Message_Block*& mb, OSF_Time_Value* tv=0)
{
return mq_->dequeue_head(mb,tv);
}


private:
MessageQueue* mq_;

}

This isn't really the code that causes that error, is it? The error refers
to a template class named OSF_Task<T1,T2> where here you're showing a class
ghj that's not a template.

In the real code, is the definition of getq outside the body of the class?

e.g.

int ghj::getq(...)

If so, you need to add the inline keyword to that out of line definition, or
move it to a .cpp file instead of the .h file.

-cd
 
R

Rohini

Hi Carl,

Thanks for the information.
The definition of getq is inside the body of the class.
I have tried to place the definition in a.cpp file and compile but it
did not work.

Here is some more information which can help you analyse my
problem.There are two .h files where the function is declared and
defined one is abc.h and the other efg.h
=========================================================================================================================

abc.h
==================
typedef OSF_Task<OSF_MT_SYNCH> Task;

class Task: public OSF_Event_Handler
{
public:
int getq(OSF_Message_Block*& mb, OSF_Time_Value* tv=0)
{
return mq_->dequeue_head(mb,tv);
}
}
=========abc.h

efg.h
========
template <OSF_SYNCH_DECL>
class OSF_Task : public OSF_Task_Base
{
Public:
int getq (OSF_Message_Block *&mb, OSF_Time_Value *timeout = 0);
}


template <OSF_SYNCH_DECL>
OSF_INLINE int
OSF_Task<OSF_SYNCH_USE>::getq (OSF_Message_Block *&mb, OSF_Time_Value
*tv)
{
return this->msg_queue_->dequeue_head (mb, tv);
}
=========efg.h

efg.h is included in abc.h

class OSF_Task_Base,class Task inherit from OSF_Event_Handler

# define OSF_MT_SYNCH OSF_Thread_Mutex, OSF_Condition_Thread_Mutex

# define OSF_SYNCH_DECL class _OSF_SYNCH

# define OSF_SYNCH_USE _OSF_SYNCH


=========================================================================================================================

Please help me resolve this problem.
Thanks once again.
 
R

Rohini

Hi Carl,
I am very sorry for misguiding you.
There is if else condition which I did not provide in my previous
reply.

abc.h
==================
#ifdef USE_OWN_TASK

class Task: public OSF_Event_Handler
{
public:
int getq(OSF_Message_Block*& mb, OSF_Time_Value* tv=0)
{
return mq_->dequeue_head(mb,tv);
}
}
#else
typedef OSF_Task<OSF_MT_SYNCH> Task;
=========abc.h
 
Top