Home > Article > Backend Development > C++ error: Failed to link function correctly, how to deal with it?
When writing programs in C, sometimes you will encounter the problem of failing to link functions correctly. This usually happens when the compiler cannot find the function definition. When this problem occurs, we need to deal with it. Next, we will introduce the definition of the function that cannot be linked correctly and how to deal with it.
1. Failed to link the function definition correctly
Failed to link the function correctly, which means that the compiler cannot associate the symbol reference with the symbol definition and cannot find the required function implementation. When compiling, the compiler will check whether the function used in the code has an implementation. If no implementation is found, it will issue an "unresolved symbol" error. This error message usually indicates which file and which line the error occurred. Common error messages are as follows:
xxx.obj : error LNK2019: unresolved external symbol “symbol” referenced in function “function”
xxx.exe : fatal error LNK1120: 1 unresolved externals
Among them, xxx
represents the program name, symbol
is the symbol name that was not found, and function
is the name of the symbol that is called The function name of the symbol.
2. Reasons for failure to link functions correctly
The common reason is the lack of library files, and the library files include function definition. The solution is to add this library file path to the additional library directory in the project properties.
Sometimes the function definition is inconsistent with the declaration. For example, the function definition is int func(int a)
, but When calling, it is found that the function declaration is int func(char a)
. In this case, the definition and declaration need to be changed to be consistent.
It may be because the function name is misspelled and inconsistent with the definition when called, and the compiler cannot find the function implementation. Check whether the function name is correct.
If the function implementation is not in the header file, you need to put the function implementation into the header file.
3. Solution to failure to link functions correctly
First check whether the program lacks relevant library files. You can first go to the official website to download the corresponding library file, and then add it to the additional library directory of the project properties. If the problem still cannot be solved, you can try to reinstall the corresponding library files.
Make sure all functions are declared and defined correctly. Function definitions should be consistent with their declaration.
Make sure the function name is spelled correctly and consistent with the definition.
Put the function implementation into the header file and make sure to include the header file when calling the function.
Summary
Failure to link functions correctly will cause program compilation to fail and bring a lot of inconvenience to programmers. In order to avoid this situation, it is necessary to do a good job of function declaration, definition and reference of library files. If there is a problem that the function cannot be linked correctly, it is best to troubleshoot and solve it step by step, find out the specific reasons and deal with it accordingly.
The above is the detailed content of C++ error: Failed to link function correctly, how to deal with it?. For more information, please follow other related articles on the PHP Chinese website!