不可能的链接器错误

发布于 2024-12-09 19:04:40 字数 1788 浏览 0 评论 0原文

我有一个库 libfoo 它由以下文件组成:

base.hpp

#ifndef BASE_HPP
#define BASE_HPP

class base
{
        public:

        virtual ~base();

        virtual void foo() = 0;
};

inline base::~base() {}

#endif /* BASE_HPP */

衍生.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"

returned.cpp

#include "derived.hpp"

void derived::foo()
{
}

当我尝试时在一个简单的程序中使用它:

main.cpp

#include <derived.hpp>

int main()
{
        derived d;

        return 0;
}

我收到以下链接器错误:

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

现在有趣的是,我只在我的 Debian Wheezy x86_64 机器上收到此错误(我在两台不同的计算机上尝试过) 。

我还尝试了 Debian Wheezy amd64完全相同的编译器版本gcc (Debian 4.6.1-4) 4.6.1 并且有它链接得很好。

可能出什么问题了?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怎会甘心 2024-12-16 19:04:40

您在链接时忽略了在链接器输入中包含共享库吗?

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

似梦非梦 2024-12-16 19:04:40

您不能仅链接 main.o 而不链接 衍生.o。除非第一个虚函数(或者没有方向虚函数的类的第一个函数)被链接进来,否则vtable不会被链接进来。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文