GCC C ++链接器错误:未定义引用'vtable for XXX',未定义引用'ClassName :: ClassName()' [英] GCC C++ Linker errors: Undefined reference to 'vtable for XXX', Undefined reference to 'ClassName::ClassName()'

查看:637
本文介绍了GCC C ++链接器错误:未定义引用'vtable for XXX',未定义引用'ClassName :: ClassName()'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Ubuntu x64上使用Eclipse-CDT设置了一个C ++项目。我基本上在做一个hello世界,并链接到一个商业的第三方库。



我已经包括头文件,链接到他们的库,但我还是得到链接器错误。这里有一些可能的问题,除了明显的(例如,我99%确定我链接到正确的库)。


  1. 有一种方法来确认我链接的静态库是64位?

  2. 有没有办法确认库有我期望的类(和方法)?? / li>



Eclipse说:

 
构建目标: LinkProblem
调用:GCC C ++链接器
g ++ -L / home / notroot / workspace / somelib-3 / somelib / target / bin -oLinkProblem./src/LinkProblem.o -lsomelib1 -lpthread - lsomelib2 -lsomelib3
./src/LinkProblem.o:在函数main中:
/home/notroot/workspace/LinkProblem/Debug/../src/LinkProblem.cpp:17:undefined引用`SomeClass :: close()'
./src/LinkProblem.o:在函数`SomeOtherClass'中:
/home/notroot/workspace/somelib-3/somelib/include/sql/somefile.h :148:未定义的引用`SomeClass :: SomeClass()'
/home/notroot/workspace/somelib-3/somelib/include/sql/somefile.h:148:undefined引用`vtable for SomeOtherClass'
/home/notroot/workspace/somelib-3/somelib/include/sql/somefile.h:151:未定义引用`SomeClass ::〜SomeClass()'
./src/LinkProblem.o:在函数`〜SomeOtherClass'中:
/home/notroot/workspace/somelib-3/somelib/include/sql/somefile.h:140:未定义引用`vtable for SomeOtherClass'
/ home / notroot /workspace/somelib-3/somelib/include/sql/somefile.h:140:未定义引用`SomeClass ::〜SomeClass()'
/ home / notroot / workspace / somelib-3 / somelib / include / sql / somefile.h:140:undefined引用`SomeClass ::〜SomeClass()'
collect2:ld返回1退出状态
make:*** [LinkProblem]错误1


解决方案

假设这些方法在一个lib中,它看起来像一个排序问题。



将库链接到可执行文件时,它们按照声明的顺序完成。

链接器只会使用所需的方法/函数来解决当前未决的依赖关系。



它是如何工作的:


如果一个后续库使用的方法/函数最初不是对象所需要的,

  • 获取所有对象文件并将它们合并到一个可执行文件中

  • 解决对象文件之间的任何依赖关系。

  • 对于每个库按顺序:


    • 检查未解决的依赖关系,并查看lib是否解析它们。

    • >

      对象需要:




      • 打开

      • <
      • BatchRead

      • BatchWrite






      • 打开

      • 关闭

      • li>



      Lib 2提供




      • BatchRead(但使用lib1:读取)

      • BatchWrite(但使用lib1:write)



      如果链接为这样:


      gcc -o plop plop.o -l1 -l2


      然后链接器将无法解析读写符号。



      链接应用程序,如下所示:


      gcc -o plop plop.o -l2 -l1




      然后它会正确链接。由于l2解析了BatchRead和BatchWrite依赖项,而且还添加了两个新的(读和写)。当我们与l1链接时,所有四个依赖关系都解决了。


      I'm setting up a C++ project, on Ubuntu x64, using Eclipse-CDT. I'm basically doing a hello world and linking to a commerical 3rd party library.

      I've included the header files, linked to their libraries, but I still get linker errors. Are there some possible problems here other than the obvious (e.g. I am 99% sure I'm linking to the correct library).

      1. Is there a way to confirm the static libraries I am linking to are 64bit?
      2. Is there a way to confirm that the library has the class (and methods) I am expecting it to have?

      Eclipse says:

      Building target: LinkProblem
      Invoking: GCC C++ Linker
      g++ -L/home/notroot/workspace/somelib-3/somelib/target/bin -o"LinkProblem"  ./src/LinkProblem.o   -lsomelib1 -lpthread -lsomelib2 -lsomelib3
      ./src/LinkProblem.o: In function `main':
      /home/notroot/workspace/LinkProblem/Debug/../src/LinkProblem.cpp:17: undefined reference to `SomeClass::close()'
      ./src/LinkProblem.o: In function `SomeOtherClass':
      /home/notroot/workspace/somelib-3/somelib/include/sql/somefile.h:148: undefined reference to `SomeClass::SomeClass()'
      /home/notroot/workspace/somelib-3/somelib/include/sql/somefile.h:148: undefined reference to `vtable for SomeOtherClass'
      /home/notroot/workspace/somelib-3/somelib/include/sql/somefile.h:151: undefined reference to `SomeClass::~SomeClass()'
      ./src/LinkProblem.o: In function `~SomeOtherClass':
      /home/notroot/workspace/somelib-3/somelib/include/sql/somefile.h:140: undefined reference to `vtable for SomeOtherClass'
      /home/notroot/workspace/somelib-3/somelib/include/sql/somefile.h:140: undefined reference to `SomeClass::~SomeClass()'
      /home/notroot/workspace/somelib-3/somelib/include/sql/somefile.h:140: undefined reference to `SomeClass::~SomeClass()'
      collect2: ld returned 1 exit status
      make: *** [LinkProblem] Error 1
      

      解决方案

      Assuming those methods are in one of the libs it looks like an ordering problem.

      When linking libraries into an executable they are done in the order they are declared.
      Also the linker will only take the methods/functions required to resolve currently outstanding dependencies. If a subsequent library then uses methods/functions that were not originally required by the objects you will have missing dependencies.

      How it works:

      • Take all the object files and combine them into an executable
      • Resolve any dependencies among object files.
      • For-each library in order:
        • Check unresolved dependencies and see if the lib resolves them.
        • If so load required part into the executable.

      Example:

      Objects requires:

      • Open
      • Close
      • BatchRead
      • BatchWrite

      Lib 1 provides:

      • Open
      • Close
      • read
      • write

      Lib 2 provides

      • BatchRead (but uses lib1:read)
      • BatchWrite (but uses lib1:write)

      If linked like this:

      gcc -o plop plop.o -l1 -l2

      Then the linker will fail to resolve the read and write symbols.

      But if I link the application like this:

      gcc -o plop plop.o -l2 -l1

      Then it will link correctly. As l2 resolves the BatchRead and BatchWrite dependencies but also adds two new ones (read and write). When we link with l1 next all four dependencies are resolved.

      这篇关于GCC C ++链接器错误:未定义引用'vtable for XXX',未定义引用'ClassName :: ClassName()'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆