对ctor和dctor简单代码的未定义引用

时间:2012-10-16 10:12:28

标签: c++ gcc linker ld

这段简单的代码确实给了我很多时间,所以任何人都可以成为某种形容并向我解释可能出现的问题?我有简单的cpp文件,它使用头文件中包含的类。

lib.h

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&#39;:

start.cpp :(。text + 0x7a):对`tnamespace :: test :: test()&#39;

的未定义引用

start.cpp :(。text + 0x88):对`tnamespace :: test :: ~test()&#39;

的未定义引用

collect2:ld返回1退出状态

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

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

1 个答案:

答案 0 :(得分:2)

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

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