链接时的 WAF:"undefined reference"错误

waf at linking time: "undefined reference" error

本文关键字:undefined 错误 reference WAF 链接      更新时间:2023-10-16

所以我一直在敲我的头在这个编译器错误在过去的2个小时,我想我将代码张贴在这里,看看是否有人可以说明我的错误。

我去掉了所有不相关的部分,只留下一个最小的程序(如下所示),在我看来,它应该可以编译和运行。如果我来了,但我看不出什么是错误的,在main中调用testFunc,然后一切都编译并运行良好。然而,通过调用testFunc,我得到以下内容:

$ ./waf -v --run abr-tool
Waf: Entering directory `/home/conor/workspace/msc/AdaptiveIPTV/Software/conor/ns3/ns-3.15/build'
[1665/1822] cxxprogram: build/src/abr-tools/examples/abr-tool.cc.4.o -> build/src/abr-tools/ns3.15-abr-tool-debug
19:04:19 runner ['/usr/bin/g++', '-L/usr/lib', '-lboost_iostreams', '-L/usr/lib', '-lboost_iostreams', '-pthread', 'src/abr-tools/examples/abr-tool.cc.4.o', '-o', '/home/conor/workspace/msc/AdaptiveIPTV/Software/conor/ns3/ns-3.15/build/src/abr-tools/ns3.15-abr-tool-debug', '-Wl,-Bstatic', '-Wl,-Bdynamic', '-Wl,--no-as-needed', '-L.', '-L.', '-L.', '-L.', '-L.', '-L.', '-lns3.15-point-to-point-debug', '-lns3.15-internet-debug', '-lns3.15-mpi-debug', '-lns3.15-bridge-debug', '-lns3.15-network-debug', '-lns3.15-core-debug', '-lrt']
src/abr-tools/examples/abr-tool.cc.4.o: In function `main':
/home/conor/workspace/msc/AdaptiveIPTV/Software/conor/ns3/ns-3.15/build/../src/abr-tools/examples/abr-tool.cc:7: undefined reference to `testFunc()'
collect2: ld returned 1 exit status

正如你所看到的,下面的代码是作为一个更大的项目的一部分构建的,我知道错误可能来自于构建过程,而不是我的代码的问题,但无论如何,我在理解这里发生的事情时遇到了一些障碍。我正在学习c++,说实话,我觉得自己没有足够的经验来单独编译这段代码,并且能够说"这肯定可以工作,但它没有",这就是为什么我像这样展示它。

还有几点可能是相关的:

  • 我可以使用在abr-tools中的abr-helper.h中定义的宏。当我放入abr-tools时,问题仍然存在。

  • 将abr-helper.h . cc放到与abr-helper.h相同的文件夹中,然后使用#include "abr-helper.h"。
  • 原来的错误是一样的,但对于一堆其他的东西定义在abr-helper.h和使用在abr-tools.cc

我很感激你们能提供的任何帮助,提前感谢。

abr-helper.h:

#ifndef ABR_HELPER_H
#define ABR_HELPER_H
#include <iostream>
void testFunc();
#endif /* ABR_HELPER_H */

abr-helper.cc:

#include <iostream>
#include "abr-helper.h"
void testFunc(){
        std::cout << "this is all testFunc() does ..." << std::endl;
}

abr-tool.cc:

#include <iostream>
#include "ns3/abr-helper.h"
int main (int argc, char *argv[]){
    std::cout << "in main()" << std::endl;
    testFunc();
    return 0;
}

看起来文件abr-helper.cc没有被编译。您可以通过添加

来轻松地进行测试。
#error "Test"

行到该文件。如果构建成功,则文件没有被编译,您需要添加它。如何做到这一点取决于你的编译器或IDE。

您需要包含abr-helper。

我不知道waf,所以我不能给出一个解决方案。下面是结果:

在编译期间,一切正常。链接过程中出现问题(ld错误)。

在以:19:04:19 runner开头的行中,没有对abr-helper的引用。0或者类似的。您一定是忘记添加abr-helper了。

除了其他答案(我也没有看到要编译的文件abr-tool.cc),函数void testFunc需要声明为extern

相关文章: