Compilation and makefile files for multiple. c Files under Linux

Source: Internet
Author: User

When programming, we can separate each function of a complete program, write a. c file, and compile and link it together at the end. This facilitates the modularity of the program functions and also facilitates the inspection of code errors.

. h file: Inside Edit the header file that the program needs to refer to.

#ifndef/#define/#endif: Prevents the header file from being repeatedly referenced.

Overall usage:

#ifndef a_h//If there are no a.h files, #define A_H. If there is, end definition

#define A_H//define A.H file

Define the header files required by a.h

#endif//End Definition Section

Example:

The writing format of an. h file.

#ifndef a_h

#define A_h

Define some of the header files required by the program

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

...

#endif

Save exit.

In fact, when writing a small program can also not #ifndef/#define/#endif, but it is more secure, not easy to make mistakes.

Writing format for. c Files

Before writing each function to load the header file, if it is a. h file, you need to write the # include "A.h".

For example, refer to the previous a.h file.

Add.c

#include "A.h"

int add (int a,intb)

{

return a+b;

}

Save exit.

After editing the program, you need to compile the link.

We can compile each. c file with GCC. If there are three. c Files A.C, B.C, C.C, compile the method as follows:

GCC a.c–o A.O//compiles three. c files into an. o file

GCC B.c-o b.o

GCC C.c-o C.O

GCC a.o b.o c.o–o All//compile three. o files into an executable file

./ALL//Execution Program

For example:

Test.h

Add.c

Main.c

Compile:

Perform:

If we have a lot of. c files, this method is not very good, then we present makefile file.

Makefile: Automatic compilation. The compilation process of each. c file is written ahead of time in the makefile file, and when you run the program, the system uses the make command to automatically compile the file to improve efficiency.

Makefile file Format:

Vim Makefile

File type: Which file gets

Get the process

For example:

MAIN:MAIN.O//executable file main is obtained by the target file MAIN.O.

GCC main.o–o main//Gets the process of compiling MAIN.O into a main file.

Main.o:main.c

Gcc-c Main.c-o MAIN.O

In the makefile file, be sure to compile each. c file into an. o file in the order in which they were executed, and then compile the. o file into an executable file sequentially.

A lot of. o files are generated after each compilation, which makes little sense for the program to run, but it takes up memory, so we can also add clear commands (clean) to the makefile file, such as:

. Phony:clean

Clean: Delete all files with a. o File type

RM–RF *.O

Some files can also be represented by the following symbols:

[email protected]: represents the target file within the rule.

$<: Represents the first dependent file in a rule.

%.C:%.O all. c files are compiled into. o Files

. Phony:clean

If there is. Phony:clean, there are clean files outside, and when you do clean, the. o file is deleted and the clean file is still outside. Ensure the security of clean files outside. If not. Phony:clean statement, when there is no clean file on the outside, the. o file is also deleted when you execute make, and an error is found if there is clean outside.

Phony: The target is not the actual file name, just the name of the command executed at the time of the request. There are two reasons to use the phony target: Avoid conflicts with file names and improve performance.

For example:

Vim Makefile

Compile and execute (make: Compile,./ALL: Execute):

Compilation and makefile files for multiple. c Files under Linux

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.