这段简单的代码确实给了我很多时间,所以有人可以帮我解释一下什么可能是错误的吗?我有简单的cpp文件,该文件使用头文件中包含的类。

库文件

namespace tnamespace {
class base{
    virtual ~base() {};
};
class test/*: public base*/ {
    public:

    test();
    test();
};
}

lib.cxx
#include "lib.h"

namespace tnamespace{
    test::test() {};
    test::~test() {}
}

start.cpp
#include <iostream>
#include <lib.h>

int main() {
    tnamespace::test d;
    return 0;
}

我使用gcc版本4.1.2 20080704并使用
g++ start.cpp -I./ext_lib -Wall

出现以下链接器错误

/tmp/ccK2v6GD.o:在函数main中:

start.cpp :(。text + 0x7a):对`tnamespace::test::test()'的 undefined reference

start.cpp :(。text + 0x88):对`tnamespace::test::〜test()'的 undefined reference

collect2:ld返回1退出状态

我设法找到解决方案。我忘了编译我的库。正确的g++命令

g++ start.cpp ext_lib / lib.cxx -I./ext_lib -Wall

最佳答案

您没有编译lib.cxx,因此没有导出符号。

g++ start.cpp lib.cxx -I./ext_lib -Wall

关于c++ - 未定义对ctor和dctor简单代码的引用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12912311/

10-11 23:12
查看更多