Home > Backend Development > C++ > C++ error: Failed to link function correctly, how to deal with it?

C++ error: Failed to link function correctly, how to deal with it?

PHPz
Release: 2023-08-21 20:45:50
Original
1123 people have browsed 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” 
Copy after login
xxx.exe : fatal error LNK1120: 1 unresolved externals 
Copy after login

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

  1. Lack of library files

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.

  1. The function definition is inconsistent with the declaration

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.

  1. The function name is misspelled

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.

  1. The function is not in the header file

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

  1. Check library files

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.

  1. Check function definitions and declarations

Make sure all functions are declared and defined correctly. Function definitions should be consistent with their declaration.

  1. Check the spelling of the function name

Make sure the function name is spelled correctly and consistent with the definition.

  1. Put the function implementation into the header file

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template