So far, I've been writing code directly into the editor window.
"We've always written all our programs within a single screen at once."
This means we've been writing all the programs in a single file.
This method is simple, so it's effective for small-scale programs.
However, in large programs, writing everything to a single file...
It becomes difficult to know where each program is located.
Furthermore, when attempting to create a program with multiple people,
It's fundamentally impossible for more than two people to write to the same file simultaneously.
Programming with more than two people becomes practically impossible.
To solve this problem, you need to write a program that is split across multiple files.
Splitting the files makes it easier to see where each program is located.
It will also be possible to create a program with multiple people.
In the preceding section, we explained the meaning of header files.
Here, we will actually create a header file and a source file with the
most basic structure.
第11章で作成したsumfunctionをminute割してみたいと思います。
Let's start by creating a source file, sum.c, that includes the sum function.
This is simple; just copy the sum function created in [Chapter 11](11-03.html).
/* sum.c */
int sum(int min, int max)
{
int num;
num = (min + max) * (max - min + 1) / 2;
return num;
}
Next, to make the functions contained within sum.c accessible from other source files,
Extract the declarations from sum.c and create a header file named sum.h.
The declarations contained within sum.c are only the declaration of the sum function, so I will write that here.
/* sum.h */
int sum(int min, int max);
We have now completed the partitioning of the sum function.
However, this doesn't have a main function, so it cannot be executed as a program.
Therefore, we will create a source file containing the main function, main.c.
The code in main.c is largely the same as Chapter 11, but there is one difference.
This time, the prototype declaration for the sum function is written in sum.h.
You cannot use the sum function unless you include sum.h.
Of course, we use the `#include` pseudo-instruction to include sum.h.
However, previously, header file names were enclosed in <>.
To include a header file you created, it needs to be enclosed in double quotes.
Including header files with the #include pseudo-instruction is referred to as
including.
【included】
Including header files with the #include preprocessor directive.
/* main.c */
#include "sum.h"
#include <stdio.h>
int main(void)
{
int value;
value = sum(50, 100);
printf("%d\n", value);
return 0;
}
That completes main.c as well.
Please note that you do not need to create a header file, main.h, for main.c.
Because the functions contained in main.c don't need to be used from other source files.
Well, I'd like to compile and run it right away, but as it is, it won't compile.
Previously, the file was automatically specified based on the editor's functionality.
When using two or more files, you must specify which files to use.
The configuration may vary slightly depending on the compiler being used.
No special setup is required.
Adding files through the menu will automatically register them.
No special setup is required.
Adding files to the project will automatically make them recognized.
メニュー -> run -> コンパイルHourパラメータ を選択し、
コンパイルHourパラメータの欄に、
-emain main.c sum.c
and type it in.
`-emain` is an instruction that creates an executable file named `main.exe`.
The last two specify source file names to be compiled together.
The header file is unnecessary because it's included in the source file.
今までにも、stdio.h などにより無意識の内に複数Fileにminute割していたのですが、
そのHourは、Compilerがautomatic的に設定していたので、
今回のように他のFileを指定する必要などはありませんでした。
"By separating it into another file like this, you won't need to write the sum function directly."
Just copy the sum.h and sum.c files, and simply include "#include "sum.h"" at the top.
All functions contained within sum.c are available.
It's cleaner than copying functions from within sum.c to the same file,
If you just decide on the function's name and arguments, you can have someone else create it for you.
"You can send me the file, and we can compile it together, enabling
multi-person development."
まあ、現代ではGit一択ですので、そんなことはしないのですが・・・