我现在正在做一些非正统的链接,可以预见的是我遇到了一些问题。
然而我已经完成了所有常规科目,问题仍然存在。
我有一个定义类的 D 源文件
extern (C++){
class motionFactory{
public:
this(double velocity, double gravity, double angle){
this.velocity = velocity;
this.gravity = gravity;
this.angle = angle;
}
}
}
然后我使用这个 .hpp 文件在 C++ 中访问(根据 D 文档这是完全合法的)
class motionFactory {
public:
motionFactory(double velocity, double gravity, double angle);
}
然后像这样继承这个类:
class motionGraph : public wxBitmap, public motionFactory{
public:
motionGraph(double velocity, double gravity, double angle, int width, int height);
}
但是在编译过程中,我遇到了一个链接错误,指出 MotionFactory 的构造函数未定义。
/usr/bin/ld: motionGraph.o: in function `motionGraph::motionGraph(double, double, double, int, int)':
motionGraph.cpp:(.text+0x66): undefined reference to `motionFactory::motionFactory(double, double, double)'
我是这样编译的:
gdc -r D/motion.d ballistics.o -o motion.o $(CFLAGS) -lgphobos -J fortran
g++ -c gui/motionGraph.cpp -o motionGraph.o $(CFLAGS) $(wxFlags)
g++ gui/main.cpp motionGraph.o motion.o mainFrame.o -o guiCalculator -lm $(CFLAGS) $(wxFlags)
我觉得这很奇怪,因为在以前版本的代码中,我在 c++ 中构造了 MotionFactories 就很好,而且 objdump 说构造函数就在那里
编译后的D文件objdump:
000000000000039c g F .text 0000000000000047 motionFactory::motionFactory(double, double, double)
编译后的c++文件objdump:
0000000000000000 g F .text 0000000000000177 motionGraph::motionGraph(double, double, double, int, int)
0000000000000000 *UND* 0000000000000000 motionFactory::motionFactory(double, double, double)
0000000000000000 w O .data.rel.ro._ZTV11motionGraph 0000000000000108 vtable for motionGraph
0000000000000000 *UND* 0000000000000000 operator new(unsigned long)
0000000000000000 *UND* 0000000000000000 motionFactory::motionFactory(double, double, double)
为什么 objdump 可以看到构造函数,而 g++ 却看不到? 我知道我让自己的事情变得不必要的困难,但请让我迷路。
想通了。 C++ 期望看到不同级别的多重继承的不同构造函数。 D 没有提供这些,因为它没有多重继承的概念。 这导致了奇怪的错误。 编写自定义链接脚本来重命名调用解决了问题。