Definition:
This error means that in the process of linking the program, the linker could not find the definition of some entity referenced (attempted to use) in the program.
Such entities may include, for example, a function or a variable.
Causes and solutions:
There may be several possible reasons for the occurrence of an error, and this depends on what the project is being built. All the many situations can be divided into two large groups:
Used third-party libraries
The required (static) library for the linker is not specified.
For example, only the *.h file with ads is connected to the project, but the implementation code is missing, usually these are *.lib or *.a files (depending on the system used). You need to explicitly connect the library to the project. For Visual C ++, this can be done by adding the following line directly to the code:
#pragma comment(lib, "libname.lib")
For gcc/clang must specify the file via the -l (el) switch
For Qt in the .pro file you need to use the LIBS variable:
LIBS += -L[путь_к_библиотеке] -l[имя_библиотеки]
For the cmake build system, there is target_link_libraries .
The library is specified, but the bitness of the library and the code being compiled do not match.
In general, the bit width of the project being built (application or library) must match the bit width of the third-party library used. Typically, library vendors provide a choice of 32 or 64 bit version to use. If the library is supplied in source code and is assembled separately from the current project, you must also select the correct bit depth.
The library is listed, but it is compiled for another (incompatible) OS.
For example, when building a project in Windows, an attempt is made to use a binary file compiled for Linux. In this case, you need to use files that are suitable for your OS.
The library is listed, but it is compiled by another compiler that is not compatible with the one used.
Object files obtained by assembling C ++ code by different compilers for the same OS may be binary incompatible with each other. It is required to use compatible (most likely and at all identical) compilers.
The library is listed and compiled by the same compiler as the main project, but different versions of the run-time libraries are used.
For example, for Visual C ++ it is possible that the library is built with the /MDd key, and the main project with /MTd . It is required to set the keys so that the same versions of the Run-Time libraries are used.
Third-party libraries are not used.
There is simply no function definition.
void f(int); // всего лишь объявление. Нет `тела` функции int main(){ f(42); // undefined reference to `f(int)' }
You need to add the definition of the function f :
void f(int) { // тело функции }
There may be another special case with an error of the form:
undefined reference to `vtable for <имя_класса>`
Such an error occurs if the declared virtual function of a class that is not pure ( =0 ) does not contain an implementation.
class C { virtual void f(int); };
It is necessary to add such an implementation. If this is done outside the class, you need to remember to specify the name of the problem class, otherwise it will be just a free function that does not eliminate the indicated problem:
void C::f(int) { // виртуальная функция-член вне определения класса // тело функции } void f(int) { // свободная функция, не устраняющая проблему // тело функции }
A similar situation can occur when using namespaces when a function declaration is in a namespace:
// В заголовочном файле namespace N { void f(int); };
and when implementing, specify this namespace forgotten:
// В файле реализации void f(int) { // функция в глобальном пространстве имён, не устраняющая проблему // тело функции } namespace N { void f(int) { // функция в нужном пространстве имён // тело функции } } // конец пространства имён
There is no definition of a static class variable.
struct S { static int i; }; int main() { S s; si = 42; // undefined reference to `S::i' }
You need to add a definition (allocate memory) variable:
int S::i;
Incorrect implementation of template code.
For example, the implementation of the template code is placed in the *.cpp file, although it must be completely in the include *.h file.
The code file was not compiled.
For example, in the case of using the make-file, the rule for building the file was not written, and in the case of using IDE of the type Visual Studio *.cpp file was not added to the list of project files.
The virtual function in the base class is not declared as =0 (pure-virtual).
struct B { void virtual f(); }; struct D : B { void f() {} }; int main() { D d; }
When using a class hierarchy, a function in the base class that has no implementation should be marked as "clean":
struct B { void virtual f() = 0; };
The name has no external binding.
For example, there is a declaration of the function f in module А and even its implementation in module B , but the implementation is marked as static :
// A.cpp void f(); int main() { f(); // undefined reference to `f()' } // B.cpp static void f() {}
A similar situation may occur when using a nameless namespace:
// B.cpp namespace { void f() {} }