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

发布于 2024-09-15 16:56:50 字数 6325 浏览 12 评论 0原文

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

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

编译使用,

g++-g -I/u01/kasunt/工作空间/corelibCORBA/包括 -I/u01/kasunt/workspace/corelibCORBA/installed_components/include -I/u01/kasunt/工作空间/corelibCORBA/idl -I/u01/kasunt/workspace/corelibCORBA/src/服务器 -I/u01/kasunt/workspace/corelibCORBA/installed_components/idl -I/u01/kasunt/workspace/corelibCORBA/installed_components/src/Server -I/u01/kasunt/workspace/corelibCORBA/installed_components/imake -DNARROWPROTO -I 。 -I/u01/kasunt/workspace/corelibCORBA/installed_components/include -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/工作空间/corelibCORBA/include -fvisibility=隐藏 -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/local/ACE+TAO+CIAO-5.7.9/ACE_wrappers/TAO -I/usr/local/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/工作空间/corelibCORBA/lib -L/u01/kasunt/workspace/corelibCORBA/installed_components/lib -g -O -Wl,-E -L/usr/local/ACE+TAO+CIAO-5.7.9/ACE_wrappers/lib -L/usr/local/ACE+TAO+CIAO-5.7.9/ACE_wrappers/lib -L/usr/local/ACE+TAO+CIAO-5.7.9/ACE_wrappers/TAO/tao -L/u01/kasunt/workspace/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_( new SequenceType ), 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;
}

并且您可以看到我已在链接选项中指定了共享库(以粗体显示)。我最初认为这可能是由于某种依赖关系所致,并将库标志移至开头,但没有影响。

我很感激。

Im wondering if anyone would be able to help me as I have been scratching my head for two days on this.

I have done the compilation and when trying to link a source code on linux getting undefined reference

Compiling using,

g++ -g
-I/u01/kasunt/workspace/corelibCORBA/include
-I/u01/kasunt/workspace/corelibCORBA/installed_components/include
-I/u01/kasunt/workspace/corelibCORBA/idl
-I/u01/kasunt/workspace/corelibCORBA/src/Server
-I/u01/kasunt/workspace/corelibCORBA/installed_components/idl
-I/u01/kasunt/workspace/corelibCORBA/installed_components/src/Server
-I/u01/kasunt/workspace/corelibCORBA/installed_components/imake
-DNARROWPROTO -I . -I/u01/kasunt/workspace/corelibCORBA/installed_components/include
-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/local/ACE+TAO+CIAO-5.7.9/ACE_wrappers/TAO
-I/usr/local/ACE+TAO+CIAO-5.7.9/ACE_wrappers/TAO/orbsvcs
-DACE_HAS_EXCEPTIONS -D__ACE_INLINE__ -c -o TestSeqWrapper.o TestSeqWrapper.cpp

Linking using,

g++ -o TestSeqWrapper TestSeqWrapper.o
-g -L/u01/kasunt/workspace/corelibCORBA/lib
-L/u01/kasunt/workspace/corelibCORBA/installed_components/lib
-g -O -Wl,-E -L/usr/local/ACE+TAO+CIAO-5.7.9/ACE_wrappers/lib
-L/usr/local/ACE+TAO+CIAO-5.7.9/ACE_wrappers/lib
-L/usr/local/ACE+TAO+CIAO-5.7.9/ACE_wrappers/TAO/tao
-L/u01/kasunt/workspace/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()'

Doing nm of the library I can see the above constructor is included,

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()

Offending line on the source code (line 165), SeqWrapper() : var_( new SequenceType ), size_( 0 ) { }

Part of the code,

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() ) { }

.....

Source code of TestSeqWrapper.cpp (I have removed most of the unwanted stuff to make it less confusing)

#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;
}

And you can see that I have specified the shared library in the link options (shown in bold). I initially thought it might be due a dependency of some sort and moved the library flag to the beginning to no affect.

I appreciate it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

任性一次 2024-09-22 16:56:50

既然你说

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 functionSeqWrapper'`

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

请参阅:GCC C++ 链接器错误:对 'vtable for XXX' 的未定义引用、对 'ClassName::ClassName()' 的未定义引用

如果将所有库构建为共享库,则此问题消失了。

Since you say

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

This means that ComboElementSeq_T() is in the lib -lCorbaIdl
If the reference is not being found, then it is being used by one of the libraries specified after this library:

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

Thus one of the follwoing contains : TestSeqWrapper.o: In functionSeqWrapper'`

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

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

If you build all the libs as shared libraries then this problem goes away.

捂风挽笑 2024-09-22 16:56:50

各位,

在喝了 10 杯咖啡和几个几乎不眠之夜之后,我终于找到了问题的根源。希望这对某人有所帮助,并且有人能够为我提供一些关于避免此类事情的线索。所以就这样吧。

这次我使用 -D 选项再次运行以下命令,它显示了完全不同的输出,并且是我所期待的。它没有结果,并且找不到“ECONZ::Corba::ComboElementSeq_T::ComboElementSeq_T”。

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

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

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

People,

After 10 cups of coffee and few almost sleepless nights I managed to get to the bottom of the problem. Hopefully this helps someone and someone will be able shed some light into me about avoiding such things. So here goes.

I ran the following once again with the -D option this time and it showed a completely different output and one that I was kind of expecting. It resulted in nothing and "ECONZ::Corba::ComboElementSeq_T::ComboElementSeq_T" was not found.

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

Then it sort of led me to believe a compilation problem with the lib. So I looked closely into the compiler flags and found these two "-fvisibility=hidden -fvisibility-inlines-hidden". This was coming from ACE/TAO and it was a new attribute they have added since GCC 4. This was causing the dynamic symbols to be hidden. So my question for anyone who is aware of this is what I should have done to link the library ?

For further reading please read http://gcc.gnu.org/wiki/Visibility

甜是你 2024-09-22 16:56:50

你的链接行中有错字吗?

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

应该是

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

typo in your link line?

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

should be

-L/u01/kasunt/workspace/corelibCORBA/lib
葮薆情 2024-09-22 16:56:50

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

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

它工作得很好。有谁能解释一下吗????

I dont know if this makes sense to anyone but seems that using library as a static instead of shared works perfectly. So I attached

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

and it worked perfectly. Anyone able to explain this ?????

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文