链接时C ++未定义的引用

时间:2010-08-24 07:03:29

标签: c++ gcc build-process gnu ld

我想知道是否有人能够帮助我,因为我已经在这两天头疼了。

我完成了编译,当尝试在linux上链接源代码时获取未定义的引用

使用编译,

  

g ++ -g   -I / U01 / kasunt /工作区/ corelibCORBA /包括   -I / U01 / kasunt /工作区/ corelibCORBA / installed_components /包括   -I / U01 / kasunt /工作区/ corelibCORBA / IDL   -I / U01 / kasunt /工作区/ corelibCORBA / src目录/服务器   -I / U01 / kasunt /工作区/ corelibCORBA / installed_components / IDL   -I / U01 / kasunt /工作区/ corelibCORBA / installed_components / src目录/服务器   -I / U01 / kasunt /工作区/ corelibCORBA / installed_components / imake的   -DNARROWPROTO -I。 -I / U01 / kasunt /工作区/ corelibCORBA / installed_components /包括   -W -Wall -Wpointer-arith -pipe -D_POSIX_THREADS -D_POSIX_THREAD_SAFE_FUNCTIONS -g -O -I / u01 / kasunt / workspace / corelibCORBA / include   -g -O -I / u01 / kasunt / workspace / corelibCORBA / include   -fvisibility = hidden -fvisibility-inlines-hidden -DACE_GCC_HAS_TEMPLATE_INSTANTIATION_VISIBILITY_ATTRS = 1   -W -Wall -Wpointer-arith -ggdb -pipe -Wall -g -D__linux__ -D__x86__ -rdynamic -D_REENTRANT -DTAO_HAS_INTERCEPTORS = 0 -DTAO_HAS_VALUETYPE = 1 -D_REENTRANT -DACE_HAS_AIO_CALLS -D_GNU_SOURCE -I / usr / local / ACE + TAO + CIAO -5.7.9 / ACE_wrappers   -I / USR /本地/ ACE + TAO + CIAO-5.7.9 / ACE_wrappers / TAO   -I / USR /本地/ ACE + TAO + CIAO-5.7.9 / ACE_wrappers / TAO / orbsvcs   -DACE_HAS_EXCEPTIONS -D__ACE_INLINE__ -c -o TestSeqWrapper.o TestSeqWrapper.cpp

使用链接,

  

g ++ -o TestSeqWrapper TestSeqWrapper.o   -g -L / u01 / kasunt / workspace / corelibCORBA / lib   -L / U01 / kasunt /工作区/ corelibCORBA / installed_components / lib中   -g -O -Wl,-E -L / usr / local / ACE + TAO + CIAO-5.7.9 / ACE_wrappers / lib   -L / USR /本地/ ACE + TAO + CIAO-5.7.9 / ACE_wrappers / lib中   -L / USR /本地/ ACE + TAO + CIAO-5.7.9 / ACE_wrappers / TAO /陶   -L / U01 / kasunt /工作区/ corelibCORBA / installed_components / IDL   -lcorelibCORBA -lcorelibCORBA /u01/kasunt/workspace/corelibCORBA/installed_components/lib/corelibCORBA3r.lib   /u01/kasunt/workspace/corelibCORBA/installed_components/lib/CorbaController3r.lib   /u01/kasunt/workspace/corelibCORBA/installed_components/lib/EConcurrency3r.lib   /u01/kasunt/workspace/corelibCORBA/installed_components/lib/corelibLogger3r.lib   /u01/kasunt/workspace/corelibCORBA/installed_components/lib/LeakTracker3r.lib   /u01/kasunt/workspace/corelibCORBA/installed_components/lib/trace3r.lib   -lDefineTimeZone /u01/kasunt/workspace/corelibCORBA/installed_components/lib/timestamp3r.lib   /u01/kasunt/workspace/corelibCORBA/installed_components/lib/ApplConfig3r.lib   /u01/kasunt/workspace/corelibCORBA/installed_components/lib/appl3r.lib   /u01/kasunt/workspace/corelibCORBA/installed_components/lib/logstream3r.lib   /u01/kasunt/workspace/corelibCORBA/installed_components/lib/disklog3r.lib   /u01/kasunt/workspace/corelibCORBA/installed_components/lib/timeout3r.lib   /u01/kasunt/workspace/corelibCORBA/installed_components/lib/getpnam3r.lib   /u01/kasunt/workspace/corelibCORBA/installed_components/lib/config3r.lib   /u01/kasunt/workspace/corelibCORBA/installed_components/lib/strl3r.lib   /u01/kasunt/workspace/corelibCORBA/installed_components/lib/string3r.lib   /u01/kasunt/workspace/corelibCORBA/installed_components/lib/command3r.lib   /u01/kasunt/workspace/corelibCORBA/installed_components/lib/cmdargs3r.lib   /u01/kasunt/workspace/corelibCORBA/installed_components/lib/CppUtils3r.lib   /u01/kasunt/workspace/corelibCORBA/installed_components/lib/BuildTag3r.lib   /u01/kasunt/workspace/corelibCORBA/installed_components/lib/exception3r.lib    -lCorbaIdl -lTAO_Valuetype -lTAO_PortableServer -lTAO_IORTable -lTAO_AnyTypeCode -lTAO_CosEvent -lTAO_CosNaming -lTAO -lACE

TestSeqWrapper.o: In function `SeqWrapper':
/u01/kasunt/workspace/corelibCORBA/include/corelibCORBA/SeqWrapper.hpp:165: undefined reference to `ECONZ::Corba::ComboElementSeq_T::ComboElementSeq_T()'

执行库的nm我可以看到包含上面的构造函数,

nm --demangle ../installed_components/lib/libCorbaIdl.so | grep "ECONZ::Corba::ComboElementSeq_T::ComboElementSeq_T"
000327ba t ECONZ::Corba::ComboElementSeq_T::ComboElementSeq_T(ECONZ::Corba::ComboElementSeq_T const&)
000331c2 t ECONZ::Corba::ComboElementSeq_T::ComboElementSeq_T(unsigned int)
0002edb0 t ECONZ::Corba::ComboElementSeq_T::ComboElementSeq_T(unsigned int, unsigned int, ECONZ::Corba::ComboElement_T*, bool)
0002d768 t ECONZ::Corba::ComboElementSeq_T::ComboElementSeq_T()
00032e52 t ECONZ::Corba::ComboElementSeq_T::ComboElementSeq_T(ECONZ::Corba::ComboElementSeq_T const&)
0003172a t ECONZ::Corba::ComboElementSeq_T::ComboElementSeq_T(unsigned int)
0002d8e0 t ECONZ::Corba::ComboElementSeq_T::ComboElementSeq_T(unsigned int, unsigned int, ECONZ::Corba::ComboElement_T*, bool)
0002d8a6 t ECONZ::Corba::ComboElementSeq_T::ComboElementSeq_T()

源代码上的违规行(第165行), SeqWrapper():var_(新序列类型),size_(0){}

部分代码,

template< typename SEQUENCE >
class SeqWrapper : CppUtils::NonCopy
{
public:
   typedef SEQUENCE SequenceType;
   typedef typename SequenceType::_var_type SequenceType_var;

   typedef typename ElementTypeOf<SequenceType_var>::ElementType ElementType;
   typedef typename ElementTypeOf<SequenceType_var>::ReturnType ReturnType;
   typedef typename ElementTypeOf<SequenceType_var>::ConstReturnType ConstReturnType;

private:
   SequenceType_var var_;
   CORBA::ULong size_;

  void expand_( CORBA::ULong size )
  {
     // NOTE: current implementation assumes size only increases by small increments
     if (size > var_->length()) {
        var_->length( (size < 16)? 16 : (size > 256? size+256 : 2*size-2) );
     }
  }

public:
   SeqWrapper() : var_( new SequenceType ), size_( 0 ) { }
   SeqWrapper( CORBA::ULong reserve ) : var_( new SequenceType( reserve ) ), size_( 0 ) { var_->length( reserve ); }
   SeqWrapper( SequenceType *ownSeq ) : var_( ownSeq ), size_( ownSeq->length() ) { }

.....

TestSeqWrapper.cpp的源代码(我删除了大部分不需要的东西,以减少它的混乱)

#include <iostream>
#include <CppUtils/StringHelpers.hpp>
#include <corelibCORBA/SeqWrapper.hpp>
#include <CorbaC.h>

using namespace std;

int main()
{
    ECONZ::Corba::SeqWrapper< ECONZ::Corba::StringSeq_T > seq;
}

您可以看到我在链接选项中指定了共享库(以粗体显示)。我最初认为它可能是由于某种依赖性而将库标志移动到开头而没有影响。

我很感激。

4 个答案:

答案 0 :(得分:1)

既然你说

nm --demangle ../installed_components/lib/libCorbaIdl.so
> 0002d768 t ECONZ::Corba::ComboElementSeq_T::ComboElementSeq_T()

这意味着ComboElementSeq_T()位于lib -lCorbaIdl中 如果未找到引用,则此库之后指定的库之一正在使用它:

> -lCorbaIdl -lTAO_Valuetype -lTAO_PortableServer -lTAO_IORTable
> -lTAO_AnyTypeCode -lTAO_CosEvent -lTAO_CosNaming -lTAO -lACE

因此,其中一个包含:TestSeqWrapper.o: In function SeqWrapper'`

  • -lTAO_Valuetype
  • -lTAO_PortableServer
  • -lTAO_IORTable
  • -lTAO_AnyTypeCode
  • -lTAO_CosEvent
  • -lTAO_CosNaming
  • -lTAO
  • -lACE

请参阅:GCC C++ Linker errors: Undefined reference to 'vtable for XXX', Undefined reference to 'ClassName::ClassName()'

如果将所有库构建为共享库,那么这个问题就会消失。

答案 1 :(得分:1)

人,

经过10杯咖啡和几乎几乎不眠之夜,我成功地解决了问题的根源。希望这有助于某人和某人能够避开这些事情。所以这里。

这次再次使用-D选项运行以下内容,它显示了一个完全不同的输出和一个我有点期待的输出。它没有结果,找不到“ECONZ :: Corba :: ComboElementSeq_T :: ComboElementSeq_T”。

nm -DC ../installed_components/lib/libCorbaIdl.so | grep "ECONZ::Corba::ComboElementSeq_T::ComboElementSeq_T"

然后它让我相信lib的编译问题。所以我仔细研究了编译器标志,发现这两个“-fvisibility = hidden -fvisibility-inlines-hidden”。这是来自ACE / TAO,它是他们自GCC 4以来添加的新属性。这导致动态符号被隐藏。所以我的问题是任何知道这一点的人都是我应该做的链接库吗?

如需进一步阅读,请阅读http://gcc.gnu.org/wiki/Visibility

答案 2 :(得分:0)

你的链接线错误?

-L/u01/kasunt/workspace/corelibCORBA/idl

应该是

-L/u01/kasunt/workspace/corelibCORBA/lib

答案 3 :(得分:0)

我不知道这对任何人是否有意义,但似乎使用库作为静态而不是共享工作完美。所以我附上了

  

/u01/kasunt/workspace/corelibCORBA/installed_components/lib/libCorbaIdl.a

它完美无缺。任何人都能解释这个?????