Correct declaration of C + + global variables

Source: Internet
Author: User
In C + + the global variable declaration error condition causes the LNK2005 error mainly has the following kinds of situations: 1. The global variable is repeatedly defined. There are two possible scenarios:
A, for some beginners programming programmers, sometimes think that need to use the global variables can use the definition of the statement. In fact, this is wrong, the global variable is for the entire project. The correct should be defined in a CPP file as follows: int g_test;   Then you should use the CPP file in the use of: extern int g_test can be, if the use of int g_test, then will produce LNK2005 error, general error message similar to: Aaa.obj error LNK2005 int book C. Book@@3ha already defined in Bbb.obj. Remember that you cannot assign a value to a variable or there will be a LNK2005 error.
What is required here is "declaration", not "definition". According to the C + + standard, a variable is declared and must satisfy two conditions, otherwise it is defined as:
(1) Declaration must use the extern keyword; (2) cannot assign an initial value to a variable
So, here's the statement:
extern int A;
The following is the definition
int A;   int a = 0; extern int a = 0;
B, for so programming is not so rigorous programmer, always need to use variables in the file to define a global variable, and the variable name is also not considered, this is often easy to cause variable name duplication, and cause LNK2005 error.

2. The header file contains duplicates. Often need to include the header file contains variables, functions, definitions of classes, in other places to use and have to include many times, if the header file does not have related macros and other measures to prevent duplication of links, then will produce LNK2005 error. The solution is to do something similar in the header file that you want to include: #ifndef my_h_file//If you do not define this macro
#define MY_H_FILE//define this macro
....//Header File body contents
.......
#endif
The above is done using macros, or you can use precompilation to do it in a header file:
#pragma once
Header file Body
3. caused by using a third party library. This situation is mainly due to the C Run-time function library and MFC library conflicts. The way to do that is to put the library that prompted the error to the front of another library. Choosing a different library of C functions may cause this error. Microsoft and C have two kinds of C run-time function libraries, one is the ordinary function library: LIBC. LIB, multithreading is not supported. The other is to support multithreading: MSVCRT.lib. If a project, the two functions of the library mixed use, may cause this error, in general, it requires the library of MFC before the C run-time function library is linked, so it is recommended to use MSVCRT.lib support multithreading. So before you use a third party library, you need to know what library it is linking to, or it may cause LNK2005 errors. If you have to use a third party library, you can try to modify the method as described below, but there is no guarantee that the problem will be solved, the first two methods are provided by Microsoft:
A, select the VC menu Project->settings->link->catagory Select Input, and then in the Ignore Libraries Edit bar to fill in the library you need to ignore, such as: Nafxcwd.li b Libcmtd.lib. Then fill in the correct library order in the Object/library Modules Edit Bar, here you can determine what is the correct order, hehe, God bless you.
B, select the VC menu Project->settings->link page, and then enter/verbose:lib in the Edit bar of Project Options so that you can see the order of the links in the Output window during the compilation of the linker.
C, select the VC menu project->settings->c/c++ page, catagory Select the Code Generation and then in the User Runtime Libraray Select multithread DLL and other libraries, try each.
Regarding the compiler's related processing process, refer to:
Http://www.donews.net/xzwenlan/archive/2004/12/23/211668.aspx
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.