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

-x assembler
コンパイラへ供給するファイルの言語を明示的に指定
assembler : アセンブラファイル

テスト概要

-Sオプションで生成したアセンブラファイルを、言語指定を「-x assembler」としてコンパイル
PASS コンパイル終了・実行ファイル生成

実行環境

GCC-8.2.0
GNU C Library 2.28
GNU Binutils 2.31.1


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

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

使用ファイル

sample.c
/* -x オプション動作確認用ソースファイル */ #include <stdio.h> int main(void) { printf("-x オプション動作確認用ソースファイル\n"); return 0; }

動作テスト


テスト用アセンブラファイルの生成
$ ls -l total 4 -rw-r--r-- 1 ****** ******** 190 *** ** **:** sample.c $ 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 ^^^ sample.cのコンパイル COLLECT_GCC_OPTIONS='-v' '-S' '-mtune=generic' '-march=x86-64' $ ls -l total 8 -rw-r--r-- 1 ****** ******** 190 *** ** **:** sample.c -rw-r--r-- 1 ****** ******** 634 *** ** **:** sample.s <--- 作成されたアセンブラファイル $

sample.s(生成されたアセンブラファイル)
.file "sample.c" .text .section .rodata .align 8 .LC0: .string "-x \343\202\252\343\203\227\343\202\267\343\203\247\343\..." .text .globl main .type main, @function main: .LFB0: .cfi_startproc pushq %rbp .cfi_def_cfa_offset 16 .cfi_offset 6, -16 movq %rsp, %rbp .cfi_def_cfa_register 6 movl $.LC0, %edi call puts movl $0, %eax popq %rbp .cfi_def_cfa 7, 8 ret .cfi_endproc .LFE0: .size main, .-main .ident "GCC: (GNU) 8.2.0" .section .note.GNU-stack,"",@progbits

言語指定を「-x assembler」としてアセンブラファイルをコンパイル
$ gcc -v -x assembler sample.s Using built-in specs. as -v --64 -o /tmp/ccFCvIzI.o 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 28 -rwxr-xr-x 1 ****** ******** 18328 *** ** **:** a.out <--- アセンブラファイルから実行ファイルを生成 -rw-r--r-- 1 ****** ******** 190 *** ** **:** sample.c -rw-r--r-- 1 ****** ******** 634 *** ** **:** sample.s $ ./a.out -x オプション動作確認用ソースファイル <--- 動作確認 $