Define variables in. cpp files, extern in. h files

Source: Internet
Author: User

In Cocos2d-x's D:\phoneclient\cocos2dxlib\cocos\base\CCValue.h file, the following code is found:

After the global lookup, in the D:\phoneclient\cocos2dxlib\cocos\base\CCValue.cpp file, locate the definition of the variable:

extern int a;//This is statement a
int a;//This is the definition of a
Under normal circumstances, the H declares the CPP definition.

declarations and definitions are different, declarations can be made more than once, and defined only once.
h files may be contained in a lot of CPP files, so only write declarations cannot write definitions, and if write definitions are included several times, they are defined several times.

The primary purpose of the conditional indicator #ifndef is to prevent duplicate inclusion and compilation of header files. A source file contains a number of the same header file, and will only be included once when multiple source files contain the same head file, each source file will be included once, the variables will be defined multiple times, link error.
Also, the concept of a C language, such as header files and precompiled macro #ifndef, is not related to the principle of compiling

Define variables in. cpp files, extern in. h files

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.