【问题标题】:undefined reference to ctor and dctor simple code对 ctor 和 dctor 简单代码的未定义引用
【发布时间】:2012-10-06 09:53:14
【问题描述】:

这段简单的代码真的让我很难受,所以谁能帮我解释一下可能出了什么问题?我有一个简单的 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() {}
}

开始.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()' 的未定义引用

start.cpp:(.text+0x88): undefined reference to `tnamespace::test::~test()'

collect2: ld 返回 1 个退出状态

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

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

【问题讨论】:

标签: c++ gcc linker ld


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-06
    • 2021-06-09
    • 1970-01-01
    • 2015-06-06
    • 2015-02-25
    • 1970-01-01
    • 1970-01-01
    • 2019-11-17
    相关资源
    最近更新 更多