C言語を学ぶとき、EclipseのようなIDEを使うときは多いと思いますが、テキストを順に進めていくときに、毎回プロジェクトを作成して、ソースフォルダsrcを作り、その中にmain関数があるソースファイルを作り、プロジェクトごとビルドして、実行するといった手順をふみ、次の練習コードでは、またプロジェクトを作りと、プロジェクトがたくさんになってしまいます。今回は、main関数入りのソースファイルは一つで、他は関数のみ記述したファイルを、ヘッダーファイルを経由して取り込んで、実行していく方法をやっていきます。今回利用する環境は「Pleiades All in One Eclipse 2024 C/C++」です。
(1)プロジェクトを作成して、mian関数入りのソースファイルで実行する
まずプロジェクト「HelloC」を作成し、その中にソースフォルダ「src」、その中にソースファイル「HelloC.c」を作成します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
int main(void){ | |
printf("HelloC_main\n"); | |
} |
プロジェクトのビルドをして、実行してみます。
(2) ファイル「subFunction01.c」を作成し、そこに関数subFunction01を作成した後、ヘッダファイルを作成する次に、ソースフォルダの中にsubFunction01.cを作成します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
int main(void){ | |
printf("HelloC_main\n"); | |
subFunction01(); | |
} |
この状態で、main関数から「subFunction01();」として、関数を呼び出しても、subFunction01が見つからずエラーになります。このように別々のファイルに書いた場合、ファイル間で呼び出しを行うにはヘッダーファイルが必要となります。
プロジェクトの下にヘッダーを入れるフォルダ「include」を作成します。さらにこの中に「subFunction.h」ヘッダーファイルを作成します。このヘッダーファイルに、subFunction01関数のプロトタイプ宣言を書き入れておきます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef INCLUDE_SUBFUNCTION_H_ | |
#define INCLUDE_SUBFUNCTION_H_ | |
void subFunction01(); | |
#endif /* INCLUDE_SUBFUNCTION_H_ */ |
次にこのヘッダーファイルの場所をプロジェクトにパスを通しておきます。
正規表現で書いてありますが、workspace中からプロジェクトを選択し、その中のincludeを指定しただけです。これでヘッダーファイルがあるフォルダにパスが通ったので、「HelloC.c」と「subFunction01.c」にこのヘッダーファイルを書いて、関数subFunction01を使えるようにします。
#include "subFunction.h"
[HelloC.c]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include "subFunction.h" | |
int main(void){ | |
printf("HelloC_main\n"); | |
subFunction01(); | |
} |
[subFunction01.c]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include "subFunction.h" | |
void subFunction01(){ | |
printf("HelloC_subFunction01\n"); | |
} |
改めてプロジェクトをビルドして、実行してみます。
プロジェクト全体このように、main関数でない関数として、別ファイルとして加え、ヘッダファイルにプロトタイプ宣言を書き込み、ヘッダフォルダへのパスを通して、そのヘッダファイルをincludeすることで、別ファイルにある関数を呼び出すことができました。gccが使える環境ならば、もう少し簡略化できますが、Eclipse環境ではこのように実施するのが一つの方法です。
0 件のコメント:
コメントを投稿