Understanding of C ++ file organization and Global Objects

Source: Internet
Author: User

File Types of header files and source files

The header file defines the declaration of functions, classes, and variables, and tells the Compiler which objects are included in this project. The source file is the implementation code. In order to share between modules, declarations and implementations are generally separated, include the H file of the module when you need it.

The compiler has no strict requirements on the file type, and the source file and header file name do not need to be strictly consistent. However, in order to be intuitive and easy to read, it is customary to use CPP as the source file, and h as the header file, the file names must be consistent.

Note that, for the function declared in H, CPP can be compiled without including this H. It seems that the compiler can automatically find the implementation code in all modules according to the function definition, you only need to ensure that the function name in the project is unique. CPP files such as classes and structures must contain H. Therefore, each CPP must contain its own H.

About Module inclusion

Since the declaration and implementation are separated, each CPP must include its own h file. In addition, all CPP files must reference the pre-compiled header.

# Include "stdafx. h"

This will cause some confusion. Generally, we want to include the hfile of the relatively unchanged public module into stdafx. H, which can reduce the Compilation Time and facilitate maintenance. However, assume that the H file of a public class module is included in stdafx. h, if the CPP of this class also contains stdafx. h. Remove the include hfile from CPP, or use the following method.

Prevents cross-Definition

Add the following code to each CPP file:

In vc6:
# If! Defined (xxxx_xxxx) // The XXXX part is defined by a file name and a special letter for distinction and must be capitalized.
# Define xxxx_xxxx
# If _ msc_ver> 1000
# Pragma once
# Endif // _ msc_ver> 1000
... // Variable and function declaration
# Endif

Vs2008 is simple:

# Pragma once

Global Objects, extern, and redefinition during connection

Although the above method can avoid redefinition, it is only for a single module. Because a single module compiles and generates obj files separately during compilation, it is okay to use the above two methods to handle compilation, because every time you process a cpp, you do not consider other cpp items, however, an error occurs when the linker finds multiple objects with the same name during the link. For example, in stdafx. h defines an int a;, so all cpp will contain it, so that all obj in the entire project has an int a, and an error is reported when multiple definitions are found during connection.

Solution:

For global objects (including common variables. h is defined by the extern modifier (you can also write all global objects in an hfile with the extern modifier and then include them in stdafx. h), and then declare in any cpp that the object can be defined only once in one cpp. Other cpp can use this object. In this way, the connector locates the object in all obj when using this object. Generally, the definition of global objects is written in the cpp where the main program is located.

For a global function, if a global function is to be called in the module, it must contain the H file of the function. Therefore, the H file of the function contains stdafx. A global function is formed in the H file. Because the function is not an object instance, it does not cause a connection error.

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.