6

This question differs from e.g.

What is the difference between g++ and gcc?

since if it is known that the cause of the described problem is "the difference between g++ and gcc", there's no need to look for an answer anymore. In other words, though the answer is the same, the question is different.

I have the following program test.cpp:

struct CircuitElement {
    bool value;
    const char *name;
    CircuitElement *next;

    CircuitElement (const char *name);    
    virtual void evaluate () = 0;       
};

struct Button: CircuitElement {
    Button (const char *name);
    virtual void evaluate ();
};

CircuitElement::CircuitElement (const char *name): name (name), next (0) {
}

Button::Button (const char *name): CircuitElement (name) {
}

void Button::evaluate () {
    // Some statements
}

Button button ("button");

int main () {
    return 0;
}

It compiles alright on www.cpp.sh, but if I compile it with locally with:

gcc test.cpp

I get the following errors:

C:\Users\info_000\AppData\Local\Temp\cceVLVFd.o:test.cpp:(.rdata$_ZTV14CircuitElement[_ZTV14CircuitElement]+0x10): undefined reference to `__cxa_pure_virtual'
C:\Users\info_000\AppData\Local\Temp\cceVLVFd.o:test.cpp:(.rdata$_ZTI6Button[_ZTI6Button]+0x0): undefined reference to `vtable for __cxxabiv1::__si_class_type_info'
C:\Users\info_000\AppData\Local\Temp\cceVLVFd.o:test.cpp:(.rdata$_ZTI14CircuitElement[_ZTI14CircuitElement]+0x0): undefined reference to `vtable for __cxxabiv1::__class_type_info'
collect2.exe: error: ld returned 1 exit status

I must be doing wrong something trivial, and I've been Googling for an answer quite some time, but to no avail.

What am I missing?

Jacques de Hooge
  • 6,750
  • 2
  • 28
  • 45
  • I tried your code in VS17 and with GCC 6.3.0 and it compiled for me, so unfortunately I'm unable to reprocude your problem. Only thing I notice is I've never seen inheritance without specifing public or private `struct Button : CircuitElement`but as I said it compiles for me – smoothware Jun 03 '17 at 18:15
  • 10
    Use `g++` command instead of `gcc`. – cpplearner Jun 03 '17 at 18:17
  • Thanks cpplearner! – Jacques de Hooge Jun 03 '17 at 18:25
  • [Difference between CC, gcc and g++?](https://stackoverflow.com/questions/1516609/difference-between-cc-gcc-and-g) [What is the difference between g++ and gcc?](https://stackoverflow.com/questions/172587/what-is-the-difference-between-g-and-gcc) – cpplearner Jun 03 '17 at 18:28
  • 1
    It's quite hard to search for "difference between g++ and gcc" if you don't know that this difference is the cause of your trouble... – Jacques de Hooge Jun 03 '17 at 18:43
  • Even if this is a bit old, i have a different solution as i just had the same problem. The problem is that some standard C++ functions are defined, but not implemented. The implementation is in libstdc++, so by adding the argument "-llibstdc++" will solve the problem. g++ just links this library by standard. See [What is the purpose of __cxa_pure_virtual?](https://stackoverflow.com/questions/920500/what-is-the-purpose-of-cxa-pure-virtual), there is a good explenation – timoxd7 Nov 11 '21 at 15:39

0 Answers0