Introduction to C-language modular compilation

Source: Internet
Author: User

Introduction to C-language modular compilationThe concept of modular programming

  The so-called modular into (multi-file development), is a multi-file (. c file) programming, a. c file and a. h file can be called a module.

Considerations for header file development:

1) header file can be similar to the C program reference other header files, you can write preprocessing blocks, but do not write specific statements.

2 You can declare a function, but you cannot define a function

3 You can declare a constant, but you cannot define a variable

4) You can "define" a macro function. Note: Macro functions are much like functions, but they are not functions. In fact, it is a statement.

5) The definition of the structure, the custom data type is usually also placed in the header file.

6) When programming multiple files, only one file can contain the main () function, because a project can have only one entry function. We call the file that contains the main () function as the main file.

7) in order to better organize the various documents, a. c file typically corresponds to a. h file, with the same file names, such as FUN.C and Fun.h. A file, declared in the. h file, that implements the. c file, the. h and. c files must have the same name.

8) The header file adheres to the principle of idempotent, that is, it can contain the same header file multiple times, but the effect is the same as it is included only once.

9) Measures to prevent duplication of containment

Usage scenarios for header files:

1. Call the library function via the header file

Some source code is inconvenient or not allowed to advertise to the user, only need to provide the user with the header file and binary library. The user only needs to declare and invoke the library function according to the interface in the header file, and the second does not care how the interface is implemented.

2. Multi-file compilation

a slightly larger project is divided into several file implementations that introduce function declarations from other files to the current file through a header file.

3. header file to enhance type safety Check

if an interface is implemented or used in a way that is inconsistent with the declaration in the header file, the compiler will point out the error, a simple rule that can greatly reduce the burden of debugging and error-changing by the programmer.

#include < > and #include ""

#include ""

Contains a user-defined file, which can be a header file or an ordinary file

1.look under the path where the current file is located

2, if not found above , to the compiler include path lookup

3, if the compiler include path is not found under the include path of the system to find

4, if the above three are not found on the error.

Current folder -----> compiler include----> system include-----> ( if not found ) error

#include <>

Header file containing a system ( compiler comes with )

1. Search under the compiler's include path

2, if not found on the system include path to find

3, if not found on the error

Compiler include---> system include--> ( if not found ) error

Xcode comes with compiler include:/applications/xcode.app/contents/developer/platforms/macosx.platform/develope r/SDKs/ macosx10.10.sdk/usr/include/

The Include paths for Mac systems are:/usr/include/usr/local/include

Include is not necessarily written on the first line

Use conditional compilation directives to prevent duplicate inclusion of header files

#ifndef C7________xxx_h

#define C7________xxx_h

#endif

Introduction to C-language modular compilation

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.