Loose-Info.com
Last Update 2020/09/19
TOP - 各種テスト - gcc - -xオプションの各種言語指定 - -x c-header

-x c-header
コンパイラへ供給するファイルの言語を明示的に指定
c-header : Cヘッダファイル

テスト概要

・言語指定オプション「-x c-header」によるCヘッダファイルのコンパイル
・プリコンパイル済みヘッダを利用したソースファイルのコンパイル

実行環境

GCC-8.2.0
GNU C Library 2.28
GNU Binutils 2.31.1


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

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

使用ファイル


sample.h
/* -x オプション動作確認用ヘッダファイル */ #ifndef SAMPLE_H #define SAMPLE_H #define TEST_FLG 1 struct Samp { int nsamp1; int nsamp2; }; int sampFunc(struct Samp s); #endif /* SAMPLE_H */

sample.c
/* -x オプション動作確認用ソースファイル sample.c */ #include "sample.h" int sampFunc(struct Samp s) { return s.nsamp1 + s.nsamp2 + TEST_FLG; }

main.c
/* -x オプション動作確認用ソースファイル main.c */ #include "sample.h" #include <stdio.h> int main(void) { struct Samp smp; smp.nsamp1 = 1; smp.nsamp2 = 2; int n = sampFunc(smp); printf("n = %d\n", n); printf("TEST_FLG = %d\n", TEST_FLG); return 0; }

動作テスト

言語指定オプション「-x c-header」によるCヘッダファイルのコンパイル

コンパイル実行
$ ls -l total 12 -rw-r--r-- 1 ****** ******** 282 *** ** **:** main.c -rw-r--r-- 1 ****** ******** 166 *** ** **:** sample.c -rw-r--r-- 1 ****** ******** 215 *** ** **:** sample.h $ gcc -v -x c-header sample.h <--- 言語指定をCヘッダファイルとしてコンパイル Using built-in specs. /usr/libexec/gcc/x86_64-pc-linux-gnu/8.2.0/cc1 -quiet -v sample.h ... -o /tmp/ccqh6fEP.s --output-pch=sample.h.gch ^^^ sample.hのコンパイル(sample.h.gchを出力) Compiler executable checksum: 84637943c330ccfe91c8483610cbfda9 $ ls -l total 1748 -rw-r--r-- 1 ****** ******** 282 *** ** **:** main.c -rw-r--r-- 1 ****** ******** 166 *** ** **:** sample.c -rw-r--r-- 1 ****** ******** 215 *** ** **:** sample.h -rw-r--r-- 1 ****** ******** 1774000 *** ** **:** sample.h.gch <--- プリコンパイル済みヘッダー $

プリコンパイル済みヘッダとの判別のため、sample.hの内容の書き換え
#define TEST_FLG 2 <--- 「1」を「2」に書き換え

ソースファイルのコンパイルを実行
$ gcc -v main.c sample.c Using built-in specs. /usr/libexec/gcc/x86_64-pc-linux-gnu/8.2.0/cc1 -quiet -v main.c ... -o /tmp/ccxIUCfa.s ^^^ main.cのコンパイル as -v --64 -o /tmp/ccvcmD7c.o /tmp/ccxIUCfa.s /usr/libexec/gcc/x86_64-pc-linux-gnu/8.2.0/cc1 -quiet -v sample.c ... -o /tmp/ccxIUCfa.s ^^^ sample.cのコンパイル as -v --64 -o /tmp/cc4YYLah.o /tmp/ccxIUCfa.s /usr/libexec/gcc/x86_64-pc-linux-gnu/8.2.0/collect2 ... COLLECT_GCC_OPTIONS='-v' '-mtune=generic' '-march=x86-64' $ ls -l total 1768 -rwxr-xr-x 1 ****** ******** 18392 *** ** **:** a.out -rw-r--r-- 1 ****** ******** 282 *** ** **:** main.c -rw-r--r-- 1 ****** ******** 166 *** ** **:** sample.c -rw-r--r-- 1 ****** ******** 215 *** ** **:** sample.h -rw-r--r-- 1 ****** ******** 1774000 *** ** **:** sample.h.gch $ ./a.out n = 4 <--- 「sample.h.gch」を利用してコンパイルされている事を示す値 TEST_FLG = 1 <--- 上記の値と同じく、「sample.h.gch」の値で置換されている $

その他ヘッダファイルのコンパイルに関する動作テストは以下のページを参照
ファイル名の拡張子が「.h」であるときのコンパイラの動作・挙動