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

-imacros ファイル名
-includeのようにインクルードはせずにマクロを取得

テスト概要

オプション無し、-imacros、および-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); sampFunc(); return 0; }

sample.h
#define SAMPLE 2 void sampFunc(void);

func.c
void sampFunc(void) { }

動作テスト

オプション無しでコンパイル
$ gcc sample.c func.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.c:7:2: warning: implicit declaration of function ‘sampFunc’ [-Wimplicit-function-declaration] sampFunc(); ^~~~~~~~ $ マクロ「SAMPLE」および関数「sampFunc()」ともに未定義、未宣言

-imacrosオプションを使用してコンパイル
$ gcc -imacros sample.h sample.c func.c sample.c: In function ‘main’: sample.c:7:2: warning: implicit declaration of function ‘sampFunc’ [-Wimplicit-function-declaration] sampFunc(); ^~~~~~~~ $ マクロ「SAMPLE」の定義は有効 関数「sampFunc()」の宣言は無効

-includeオプションを使用してコンパイル
$ gcc -include sample.h sample.c func.c エラー・警告無し $ ./a.out SAMPLE = 2 $ マクロ「SAMPLE」および関数「sampFunc()」の定義・宣言ともに有効