Loose-Info.com
Last Update 2021/12/01
TOP - 各種テスト - gcc - -include オプション

-include ファイル名
1行目への#include "ファイル名" の記述と同等の処理を実施

テスト概要

オプション無し、および-includeオプションを使用したコンパイル

実行環境

GCC-8.2.0
GNU C Library 2.28
GNU Binutils 2.31.1


コード例・出力内容中の表記

・実行例中の太字表記部分は、コマンドなどの入力された文字列を示します。
・「」や「...」の着色省略表記は、 実際のソースコードや出力内容などを省略加工した部分を示します。

使用ファイル

sample.c
#include <stdio.h> int main(void) { printf("SAMPLE = %d\n", SAMPLE); return 0; }
sample.h
#define SAMPLE 2

動作テスト

オプション無しでコンパイル
$ gcc sample.c sample.c: In function ‘main’: sample.c:5:26: error: ‘SAMPLE’ undeclared (first use in this function) printf("SAMPLE = %d\n", SAMPLE); ^~~~~~ sample.c:5:26: note: each undeclared identifier is reported only once for each function it appears in $ ヘッダファイルが読み込まれずマクロ名「SAMPLE」が未定義となりエラーが発生

-includeオプションを使用してコンパイル
$ gcc -include sample.h sample.c エラー・警告無し $ ./a.out SAMPLE = 2 $ 指定したファイルが読み込まれ「SAMPLE」が定義される