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

-x none
コンパイラへ供給するファイルの言語を明示的に指定
none : 言語指定をオフに設定。後続のファイルは拡張子に従って処理

テスト概要

その1
言語指定を最初に「-x c」として、アセンブラの混在する複数ファイルをコンパイル
FAIL コンパイル終了・実行ファイル生成

その2
「-x none」を利用して、言語指定をコマンドライン途中で解除してコンパイル
PASS コンパイル終了・実行ファイル生成

実行環境

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; }

その1

言語指定を最初に「-x 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 -S sample.c <--- アセンブル直前で停止させるためSオプションでコンパイル Using built-in specs. /usr/libexec/gcc/x86_64-pc-linux-gnu/8.2.0/cc1 -quiet -v sample.c ... -o sample.s COLLECT_GCC_OPTIONS='-v' '-S' '-mtune=generic' '-march=x86-64' <--- アセンブル以降の処理は未実施 $ ls -l total 16 -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 ****** ******** 445 *** ** **:** sample.s <--- 作成されたアセンブラファイル $

sample.s(生成されたアセンブラファイル)
.file "sample.c" .text .globl sampFunc .type sampFunc, @function sampFunc: .LFB0: .cfi_startproc pushq %rbp .cfi_def_cfa_offset 16 .cfi_offset 6, -16 movq %rsp, %rbp .cfi_def_cfa_register 6 movq %rdi, -8(%rbp) movl -8(%rbp), %edx movl -4(%rbp), %eax addl %edx, %eax addl $2, %eax popq %rbp .cfi_def_cfa 7, 8 ret .cfi_endproc .LFE0: .size sampFunc, .-sampFunc .ident "GCC: (GNU) 8.2.0" .section .note.GNU-stack,"",@progbits

コンパイル実行
$ ls -l total 16 -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 ****** ******** 445 *** ** **:** sample.s $ gcc -v -x c main.c sample.s Using built-in specs. /usr/libexec/gcc/x86_64-pc-linux-gnu/8.2.0/cc1 -quiet -v main.c ... -o /tmp/ccAVi3ud.s ^^^ main.cのコンパイル as -v --64 -o /tmp/ccctXkAP.o /tmp/ccAVi3ud.s /usr/libexec/gcc/x86_64-pc-linux-gnu/8.2.0/cc1 -quiet -v sample.s ... -o /tmp/ccAVi3ud.s ^^^ sample.sのコンパイル sample.s:1:2: error: expected identifier or ‘(’ before ‘.’ token <--- Cのコードとして認識されるためエラー発生 .file "sample.c" ^ sample.s:4:18: error: stray ‘@’ in program .type sampFunc, @function ^ sample.s:25:30: error: stray ‘@’ in program .section .note.GNU-stack,"",@progbits ^

その2

「-x none」を利用して、言語指定をコマンドライン途中で解除してコンパイル

コンパイル結果
$ ls -l total 16 -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 ****** ******** 445 *** ** **:** sample.s $ gcc -v -x c main.c -x none sample.s <--- 「-x none」以降は拡張子別の処理動作 Using built-in specs. /usr/libexec/gcc/x86_64-pc-linux-gnu/8.2.0/cc1 -quiet -v main.c ... -o /tmp/ccLeiEbg.s ^^^ sample.cのコンパイル as -v --64 -o /tmp/ccPqOvDl.o /tmp/ccLeiEbg.s <--- main.cのアセンブル as -v --64 -o /tmp/ccVD4i7q.o sample.s <--- sample.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 36 -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 ****** ******** 445 *** ** **:** sample.s $ ./a.out n = 4 TEST_FLG = 1 $ エラー無しで終了