Error: undefined reference to ` xxx ‘in C + + means that an undefined method is referenced. There are many reasons for this problem. Here are two problems I encountered.
1. The corresponding header file is not referenced or the versions of library functions referenced in the header file are inconsistent. In different versions of libraries, the names of the same implementation method may be inconsistent, which causes this problem.
2. The method of using extern keyword is wrong. There are many uses of extern. The function of extern here is to refer to functions of other files.
a.h:
//a.h
#ifndef AH
#define AH
extern int test();//Here the return type and parameters must be the same as the implementation method
#endif
aaa.cpp
//aaa.cpp
#include <stdio.h>
#include <iostream>
#include "a.h" //Both need to refer to the corresponding header file, otherwise an error will be reported
using namespace std;
int test()
{
cout << "abc" <<endl;
}
bbb.cpp
//bbb.cpp
#include <stdio.h>
#include <iostream>
#include "a.h" //Both need to refer to the corresponding header file, otherwise an error will be reported
int main()
{
test();
}
Results of operation:
If any cpp file does not refer to the corresponding header file, an error will be reported.
Read More:
- [Solved] PCL Compile Error: undefined reference to `pcl::PCLBase<pcl::PointXYZRGBA>:: XXX
- C++Qt development – Qtsocket programming error: undefined reference to `_imp__WSAStartup
- Clion new method shows undefined reference to solution
- [Solved] Qt Error: undefined reference to xxxxx
- error: undefined reference to `calculate()` [How to Solve]
- [Solved] cortex_M3 freeRTOS Error: undefined reference to `_sbrk‘
- [Solved] QT error: error: undefined reference to ` VTable`
- [Solved] Compile Error: undefined reference to `google::FlagRegisterer::FlagRegisterer
- GCC Error:(.text+0x24): undefined reference to `main‘collect2: error: ld returned 1 exit status [Solved]
- [Solved] VScode Error: undefined reference to ‘WinMain’ collect2.exe: error: ld returned 1 exit status
- [Solved] QT Error: error: undefined reference to `GameModel::~GameModel()’
- [Solved] Compile the program error: undefined reference to `cv::dnn::dnn4_v20210301::Net::~Net()‘
- [Solved] C++ Error: Undefined symbols for architecture x86_64:
- [Solved] Eslint error: /xxx/components/xxx import should occur after import of /xxx/utils/xxx
- How to Solve Kotlin unresolved reference error
- [Solved] Android Develop Error: xxx does not have a NavController set on xxx
- [Solved] Could not resolve placeholder ‘XXX‘ in value “${XXX}“
- Angular_ Error: Cannot assign to a reference or variable!
- [Solved] “Field pet in XXX.HelloController required a bean of type ‘XXX.Pet‘ that could not be found.“
- How to Solve Fatal error stdatomic in C/C++ Compilation