Tag Archives: C + + learning notes

[Solved] C++ error: undefined reference to `xxx‘

        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.