VC ++ LNK2005 error

xiaoxiao2021-03-06  29

Programming often encounters a LNK2005 error - repeated definition error, in fact, the LNK2005 error is not a difficult to resolve. To make it clear about it, you can easily solve it.

There is mainly the following cases that cause the LNK2005 error:

1. Define global variables. There may be two cases:

A. For some primary programmers, sometimes you can use the definition of the definition to use the definition. In fact, this is wrong, global variables are for the entire project. Correct should be defined in a CPP file as follows: int g_test; then use: Extern Int g_test, if you still use int G_test, then generate LNK2005 errors, general error error message Similar : AAA.OBJ ERROR LNK2005 INT BOOK C? BOOK @@

3HA

Already defined in bbb.obj. If you remember, you can't assign a variable. Otherwise, there will be a LNK2005 error.

What is needed here is "declaration", not "definition"! According to the rules of C standards, a variable is a statement that must meet two conditions at the same time, otherwise it is defined:

(1) The statement must use the extern keyword; (2) cannot be assigned to the variable

So, the following is a statement:

Extern Int A;

The following is the definition

INT A; int a = 0; extern int a = 0;

B, for the programming that is not such a rigorous programmer, always define a global variable in the file that needs to use the variable, and for the variable name, this is often easily deduplication, causing the LNK2005 error.

2. The header file contains repetition. It is often necessary to include the definitions of variables, functions, classes in the header file, and have to include many other uses, if there is no related macro in the header file to prevent repetitive links, then the LNK2005 error will be generated. . The solution is to do a similar processing in the header file that needs to be included: #ifndef my_h_file // If this macro is not defined

#define my_h_file // Define this macro

....... // Head file main body content

.......

#ENDIF

The above is made using macros, or you can use pre-transmissions, add:

#pragma overce

// Head file main body

3. By using a third party library. This situation is mainly caused by the C operation function library and the MFC's library conflict. The specific method is to put the prompt error to the front of another library. Alternatively, different C function libraries may cause this error. Microsoft and C have two C operation function libraries, one is a normal library: libc.lib, no multi-threaded. The other is to support multi-threaded: msvcrt.lib. If in one project, these two libraries are mixed, which may cause this error. In general it requires the MFC's library to be linked to the C run function library, it is recommended to use MSVCRT.LIB that supports multi-threads. So before you use a third-party library, you must know what library it is linked, otherwise it may cause the LNK2005 error. If you have to use a third-party library, you can try to modify it in the method described below, but you can't guarantee that you can solve the problem, the first two methods are Microsoft provided:

A. Select VC menu Project-> settings-> link-> catagory Select Input, and then fill in the library you need to ignore in the Edit column of Ignore Libraries, such as: nafxcwd.lib; libcmtd.lib. Then fill in the order of the correct library in the Edit column of Object / Library Modules, here you need to determine what is the right order, huh, god bless you! B, select the VC menu project-> settings-> link page, then enter / verbose: lib in the EDIT column of Project Options, so you can see the order in the output window during the compiled link program.

C, select the VC menu Project-> settings-> C / C page, Catagory selects Code Generation, select Multithread DLL and other libraries in the user runtime libraray, try one by one.

About the related processing process of the compiler, reference:

http://www.donews.net/xzwenlan/archive/2004/12/23/21168.aspx

This is how many of the LNK2005 errors I have encountered, and there is certain that there may be such a mistake, so I don't want you to follow this article, I will encounter the lnk2005 error when I don't move. The exclusion error of the brain wants to hide the seat. The process of programming is a thinking process, so there are still more to start your mind, so you will harvest more!

转载请注明原文地址:https://www.9cbs.com/read-61808.html

New Post(0)