不可能的链接器错误
我有一个库 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在链接时忽略了在链接器输入中包含共享库吗?
You neglected to include the shared lib in the linker inputs when linking?
您不能仅链接
main.o
而不链接衍生.o
。除非第一个虚函数(或者没有方向虚函数的类的第一个函数)被链接进来,否则vtable
不会被链接进来。You can't link just
main.o
withoutderived.o
as well. Unless the first virtual function (or first function for a class with no direction virtual functions) is linked in, thevtable
won't be linked in.