Search code examples
gcclinkervirtuallinker-errorsvtable

Impossible linker error


I have a library, libfoo which is made of the following files:

base.hpp

#ifndef BASE_HPP
#define BASE_HPP

class base
{
        public:

        virtual ~base();

        virtual void foo() = 0;
};

inline base::~base() {}

#endif /* BASE_HPP */

derived.hpp

#ifndef DERIVED_HPP
#define DERIVED_HPP

#include "base.hpp"

class derived : public base
{
        public:

        void foo();
};

#endif /* DERIVED_HPP */

base.cpp

#include "base.hpp"

derived.cpp

#include "derived.hpp"

void derived::foo()
{
}

When I try to use it in a simple program:

main.cpp

#include <derived.hpp>

int main()
{
        derived d;

        return 0;
}

I get the following linker error:

scons -Q -C libfoo
scons: Entering directory `/home/ereon/git_work/box/libfoo'
g++ -o base.os -c -fPIC base.cpp
g++ -o derived.os -c -fPIC derived.cpp
g++ -o libfoo.so -shared base.os derived.os
scons -Q -C bar
scons: Entering directory `/home/ereon/git_work/box/bar'
g++ -o main.o -c -I/home/ereon/git_work/box/libfoo main.cpp
g++ -o bar main.o
main.o: In function `derived::derived()':
main.cpp:(.text._ZN7derivedC2Ev[_ZN7derivedC5Ev]+0x1f): undefined reference to `vtable for derived'
main.o: In function `derived::~derived()':
main.cpp:(.text._ZN7derivedD2Ev[_ZN7derivedD5Ev]+0x13): undefined reference to `vtable for derived'
collect2: ld returned 1
scons: *** [bar] Error 1
make: *** [all] Erreur 2

Now the funny thing is that I only get this error on my Debian Wheezy x86_64 machines (I tried on two different computers).

I also tried on a Debian Wheezy amd64 with the exact same compiler version: gcc (Debian 4.6.1-4) 4.6.1 and there it links fine.

What could be wrong ?


Solution

  • You neglected to include the shared lib in the linker inputs when linking?