首页 > 解决方案 > 注意:缺少 vtable 通常意味着第一个非内联虚拟成员函数没有定义

问题描述

我的系统是 macOS Big Sur,我遇到了编译问题。这是我的程序:

#include <iostream>
#include <sys/ioctl.h>
#include <unistd.h>

class view {
    virtual void draw() = 0;
};

class tui : view {
public:
    void draw();
};

void tui::draw() {
    struct winsize size;
    ioctl(STDOUT_FILENO, TIOCGWINSZ, &size);

    printf("x = %hu, y = %hu\n", size.ws_row, size.ws_col);
}

int main() {
    tui a;
    a.draw();
}

这是我尝试时的错误gcc -Wall main.cpp -o my_program

Undefined symbols for architecture x86_64:
  "vtable for __cxxabiv1::__class_type_info", referenced from:
      typeinfo for view in main-c2a1d3.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
  "vtable for __cxxabiv1::__si_class_type_info", referenced from:
      typeinfo for tui in main-c2a1d3.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
  "___cxa_pure_virtual", referenced from:
      vtable for view in main-c2a1d3.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

你知道这个问题吗?

标签: c++macosgcc

解决方案


您正在尝试使用 C 编译器编译 C++ 代码。只需使用:

g++ -Wall main.cpp -o my_program

推荐阅读