Loose-Info.com
Last Update 2020/10/06
TOP - 各種テスト - gcc - -Sオプション

-Sオプション
ソースファイルのコンパイルまでを実行

テスト概要

ソースコードを-Sオプションでコンパイル
コンパイル終了・アセンブラファイル生成

実行環境

GCC-8.2.0
GNU C Library 2.28
GNU Binutils 2.31.1


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

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

使用ファイル


sample.h
/* -S オプション動作確認用ヘッダファイル */ #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
/* -S オプション動作確認用ソースファイル sample.c */ #include "sample.h" int sampFunc(struct Samp s) { return s.nsamp1 + s.nsamp2 + TEST_FLG; }

main.c
/* -S オプション動作確認用ソースファイル 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; }

動作テスト


コンパイル実行
$ 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 main.c sample.c Using built-in specs. COLLECT_GCC=gcc /usr/libexec/gcc/x86_64-pc-linux-gnu/8.2.0/cc1 -quiet -v main.c ... -o main.s ^^^ main.cのコンパイル /usr/libexec/gcc/x86_64-pc-linux-gnu/8.2.0/cc1 -quiet -v sample.c ... -version -o sample.s ^^^ sample.cのコンパイル COLLECT_GCC_OPTIONS='-v' '-S' '-mtune=generic' '-march=x86-64' $ ls -l コンパイルのみ(アセンブル直前)で終了 total 20 -rw-r--r-- 1 ****** ******** 282 *** ** **:** main.c -rw-r--r-- 1 ****** ******** 698 *** ** **:** main.s <--- 生成されたアセンブラファイル -rw-r--r-- 1 ****** ******** 166 *** ** **:** sample.c -rw-r--r-- 1 ****** ******** 215 *** ** **:** sample.h -rw-r--r-- 1 ****** ******** 445 *** ** **:** sample.s <--- 生成されたアセンブラファイル $

main.s(生成されたアセンブラファイル)
.file "main.c" .text .section .rodata .LC0: .string "n = %d\n" .LC1: .string "TEST_FLG = %d\n" .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 subq $16, %rsp movl $1, -12(%rbp) movl $2, -8(%rbp) movq -12(%rbp), %rax movq %rax, %rdi call sampFunc movl %eax, -4(%rbp) movl -4(%rbp), %eax movl %eax, %esi movl $.LC0, %edi movl $0, %eax call printf movl $1, %esi movl $.LC1, %edi movl $0, %eax call printf movl $0, %eax leave .cfi_def_cfa 7, 8 ret .cfi_endproc .LFE0: .size main, .-main .ident "GCC: (GNU) 8.2.0" .section .note.GNU-stack,"",@progbits

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 $1, %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