C Language Basics: Multi-File Compilation

        Multi-file compilation, when we first wrote the Hello World program, we wrote the program in a text file with a .c suffix, and then compiled and run it through the gcc compiler. In this section we will learn how to write programs with multiple source files

First, the header file header and the source file source

        Usually we will include some type definitions, structure definitions, macro definitions, function declarations, include and other content in header files. Instead, write the actual function implementation in the source file.

        For example, we can write the following in the header file hello.h

/* hello.h */
#include <stdio.h>
void print_hello(void);

        It contains standard input and output header files, type definitions, function declarations, etc., and we write a source file of hello.c:

/* hello.c */
#include "hello.h"
void print_hello(void)
{
	printf("Hello World!\n");
}

        The source file contains the header file hello.h, so in this hello.c file, you can use the content defined in the header file, you can use custom types, custom functions, standard input and output functions, etc. When compiling code with gcc, you only need to specify hello.c and the compiler will find this header file according to #include "hello.h". Note that hello.h and hello.c should be stored in the same directory.

        It is worth mentioning in detail the path of include, when using <> to specify the included header file, the compiler will search from the system header file library, and using "" to include the header file, the compiler will start from The current program directory is searched. The included file can be an absolute path or a relative path during include, in short, as long as the relationship between the storage path of the header file and the current source file is correct.

        In addition, include can not only include .h type header files, in theory, it can include any type of files, such as including a .c file, etc., but we usually use it to include .h type header files.

 

2. Multi-file compilation

        Earlier we have learned how to write header and source files, but this is just a single-file compilation method. Most large-scale projects have a lot of header files and source files, and it is impossible for us to write all the codes in the same file, which is also inconvenient to read and maintain the code. Usually, the code is written separately according to different functions. in multiple source and header files. For example, we can write a simple calendar program:

/* ioput.h */
#include <stdio.h>
//Read the year entered by the user
int input_year(void);
//Read the month entered by the user
int input_month(void);
// show the calendar
void output_days(int year, int month, int week, int is_leap_year);


/* ioput.c */
#include "ioput.h"
//Read the year entered by the user
int input_year(void)
{
	int year;
	printf("Enter the year:");
	scanf("%d", &year);
	return year;
}
//Read the month entered by the user
int input_month(void)
{
	int month;
	printf("Enter the month:");
	scanf("%d", &month);
	return month;
}
// show the calendar
void output_days(int year, int month, int week, int is_leap_year)
{
	//The name of the month and week and the number of days in each month
	char* month_name[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
	char* week_name[7] = { "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa" };
	int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	//February 29th day in leap year
	if (is_leap_year)
	{
		days[1] = 29;
	}
	printf("\n");
	//display year, month and week
	printf("     %s %d\n", month_name[month], year);
	for (int j = 0; j < 7; j++)
	{
		printf("%2s ", week_name[j]);
	}
	printf("\n");
	//Display blanks before the 1st of each month
	for (int i = 0; i < week % 7; i++)
	{
		printf("   ");
	}
	//loop through the dates
	for (int i = 1; i <= days[month]; i++)
	{
		printf("%2d ", i);
		/ / Display 7 numbers and then wrap
		if ((i + week) % 7 == 0)
		{
			printf("\n");
		}
	}
	printf("\n\n");
}


/ * calc.h * /
#include "ioput.h"
//Zeller's formula calculates the week, only for dates after October 15, 1582
int calc_week(int year, int month, int day);
//calculate leap year
int calc_leap_year(int year);
//Calendar core function
void calc_core(void);


/* calc.c */
#include "calc.h"
//Zeller's formula calculates the week, only for dates after October 15, 1582
int calc_week(int year, int month, int day)
{
	if (month <= 2)
	{
		month += 12;
		year--;
	}
	int century = year / 100;
	year %= 100;
	int days = (year + year / 4 + century / 4 - 2 * century + 26 * (month + 1) / 10 + day - 1) % 7;
	while (days < 0)
	{
		days += 7;
	}
	return days;
}
//calculate leap year
int calc_leap_year(int year)
{
	if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
	{
		return 1;
	}
	return 0;
}
//Calendar core function
void calc_core(void)
{
	do
	{
		int year = input_year();
		if (year <= 1582)
		{
			break;
		}
		int month = input_month();
		if (month <= 0 || month >= 13)
		{
			break;
		}
		int is_leap_year = calc_leap_year(year);
		int week = calc_week(year, month, 1);
		month--;
		output_days(year, month, week, is_leap_year);
	}
	while (1);
}

/* main.c */
#include "calc.h"
int main(int argc, char *argv[])
{
	calc_core();
	return 0;
}

        Accept the user's input and output as a pair of header files and source files ioput.h and ioput.c, and calculate the leap year and calculate the day of the week in another pair of header files calc.h and calc.c, and finally the main function main The source file main.c where it is located contains these related header files, and compiles these source files together:

                gcc -o calc main.c ioput.c calc.c

        gcc compiles the three source files together into one executable. Let's take a look at the results of running this file.

Enter the year: 2017
Enter the month: 11

     November 2017
Su Mo Tu We Th Fr Sa
          1  2  3  4
 5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30

        About this example, it is only to let the reader know the process and method of multi-file compilation. If the reader is not clear about the program in the example, please refer to "Displaying a Beautiful Calendar" .


Welcome to the public account: programming aliens

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325691207&siteId=291194637