链接时的 WAF:"undefined reference"错误
waf at linking time: "undefined reference" error
所以我一直在敲我的头在这个编译器错误在过去的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
。
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 为什么创建友元类的实例会导致"undefined reference to"错误?
- 为什么在使用 SDL2 时仍然收到'undefined reference'链接器错误?
- 从 C 可执行文件加载动态库时收到错误C++"undefined symbol"
- 尝试使用 extern "C" 调用 C 中的C++方法,得到"undefined reference to"对象的链接器错误
- 错误说"camera is undefined",但我确实定义了它
- 编译 OpenBLAS C++项目会产生错误"Undefined symbols for architecture x86_64"
- 使用多个文件时出现"Undefined symbols for architecture x86_64"错误
- 在 Python 3.6 中调用C++扩展时导入错误"undefined symbol: _ZNK9FastNoise8GetNoiseEff"
- 包含<iostream>时"Undefined reference"错误
- CMake 'undefined reference' CERN-ROOT 的错误
- 生成错误:"cannot find target for file"和"undefined reference"
- YAML-cpp 编译错误:"undefined reference to YAML::LoadFile"
- 添加 cpp11 插件时出现错误消息"Undefined reference to boost (...)"
- 使用程序集嵌入数据时"Undefined reference"错误,使用适用于窗口的 mingw-w64 编译(COFF 而不是 ELF)
- 为什么这"undefined extern variable"不会导致 C++17 中出现链接器错误?
- C++ SQL 连接"get_driver_instance(); is undefined "错误
- 访问子类中的"protected"数据时"Identifier is undefined"错误
- 如何修复简单C++应用程序中的"identifier is undefined"错误?