开发者

Impossible linker error

开发者 https://www.devze.com 2023-04-12 19:00 出处:网络
I have a library, libfoo which is made of the following files: base.hpp #ifndef BASE_HPP #define BASE_HPP

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 ?


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


You can't link just main.o without derived.o as well. Unless the first virtual function (or first function for a class with no direction virtual functions) is linked in, the vtable won't be linked in.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号