LoginSignup
0
0

@qnighy 「Cの可変長引数とABIの奇妙な関係」のコメントにあげていただいたプログラムをdockerで, docker(138)

Last updated at Posted at 2019-09-01

@qnighy Cの可変長引数とABIの奇妙な関係

のコメント欄に @fujitanozomu さんが興味深いプログラムが書かれていた。

早速、gccとclang(llvm)でコンパイルしてみた。

<この項は書きかけです。順次追記します。>

gccとclang(llvm)で違う警告などを出す資料としてdockerに記録する。

docker/gcc
# gcc main.c f.c -o maing

# ./maing

# clang main.c f.c -o mainl
f.c:2:51: warning: control reaches end of non-void function [-Wreturn-type]
int f(const char *s, int n, double f) { /* ... */ }
                                                  ^
1 warning generated.

 ./mainl

# gcc hoge.c -o hogeg

# ./hogeg
1.200000 3

# clang hoge.c -o hogel
# ./hogel
1.200000 3

# gcc hogera.c -o hogerag
hogera.c:1:5: warning: conflicting types for built-in function 'printf'; expected 'int(const char *, ...)' [-Wbuiltin-declaration-mismatch]
    1 | int printf();
      |     ^~~~~~
hogera.c:1:1: note: 'printf' is declared in header '<stdio.h>'
  +++ |+#include <stdio.h>
    1 | int printf();
# clang hogera.c -o hogeral
hogera.c:1:5: warning: incompatible redeclaration of library function 'printf' [-Wincompatible-library-redeclaration]
int printf();
    ^
hogera.c:1:5: note: 'printf' is a builtin with type 'int (const char *, ...)'
1 warning generated.
# ./hogeral
1.200000 3

# gcc --version
gcc (GCC) 9.1.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

# clang --version
clang version 7.0.1-8 (tags/RELEASE_701/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

main.c, f.c が本文中のプログラム @qnighy さんのプログラム
hoge.c, hoera.c がコメント中の @fujitanozomu さんのプログラム

main.cはclangで警告がでたが結果に違いはない。

hoge.cは一緒。

hogera.cはgccとclangで違う方向性の警告。

docker hubへの登録がエラーでうまくできていません。
明日、朝、やりなおします。

今日のdocker error:error storing credentials
https://qiita.com/kaizen_nagoya/items/e40b761d4d57296bdb25

#翌日(20190902)

別の機材でdocker loginできることを先に確認してから作業。

macOS
$ docker login

$ docker run -v /Users/Administrator/work:/home/work -it gcc /bin/bash 

-vでホストOSとファイル共有する。文字コードなどの処理とエディタの利用。

docker/gcc
# apt update; apt -y upgrade; apt install -yclang vim wget apt-utils

# ./gccl.sh main f -Wall
https://qiita.com/qnighy/items/be04cfe57f8874121e76
https://qiita.com/kaizen_nagoya/items/79e771ff681fb27f6070
$ clang main.c f.c -Wall -o mainl
f.c:4:51: warning: control reaches end of non-void function [-Wreturn-type]
int f(const char *s, int n, double f) { /* ... */ }
                                                  ^
1 warning generated.

$ gcc main.c f.c -Wall -o maing
f.c: In function 'f':
f.c:4:1: warning: control reaches end of non-void function [-Wreturn-type]
    4 | int f(const char *s, int n, double f) { /* ... */ }
      | ^~~

# ./gccl.sh hoge  
https://qiita.com/qnighy/items/be04cfe57f8874121e76
https://qiita.com/kaizen_nagoya/items/79e771ff681fb27f6070
$ clang hoge.c    -o hogel
1.200000 3

$ gcc hoge.c   -o hogeg
1.200000 3

# ./gccl.sh hogera  
https://qiita.com/qnighy/items/be04cfe57f8874121e76
https://qiita.com/kaizen_nagoya/items/79e771ff681fb27f6070
$ clang hogera.c    -o hogeral
hogera.c:3:5: warning: incompatible redeclaration of library function 'printf'
      [-Wincompatible-library-redeclaration]
int printf();
    ^
hogera.c:3:5: note: 'printf' is a builtin with type 'int (const char *, ...)'
1 warning generated.
1.200000 3

$ gcc hogera.c   -o hogerag
hogera.c:3:5: warning: conflicting types for built-in function 'printf'; expected 'int(const char *, ...)' [-Wbuiltin-declaration-mismatch]
    3 | int printf();
      |     ^~~~~~
hogera.c:1:1: note: 'printf' is declared in header '<stdio.h>'
  +++ |+#include <stdio.h>
    1 | //https://qiita.com/qnighy/items/be04cfe57f8874121e76
1.200000 3

# ./gccl.sh hogehoge
https://qiita.com/qnighy/items/be04cfe57f8874121e76
https://qiita.com/kaizen_nagoya/items/79e771ff681fb27f6070
$ clang hogehoge.c    -o hogehogel
hogehoge.c:4:5: warning: incompatible redeclaration of library function 'printf'
      [-Wincompatible-library-redeclaration]
int printf();
    ^
hogehoge.c:4:5: note: 'printf' is a builtin with type 'int (const char *, ...)'
1 warning generated.
Segmentation fault

$ gcc hogehoge.c   -o hogehogeg
hogehoge.c:4:5: warning: conflicting types for built-in function 'printf'; expected 'int(const char *, ...)' [-Wbuiltin-declaration-mismatch]
    4 | int printf();
      |     ^~~~~~
hogehoge.c:1:1: note: 'printf' is declared in header '<stdio.h>'
  +++ |+#include <stdio.h>
    1 | //https://qiita.com/qnighy/items/be04cfe57f8874121e76
Segmentation fault

# clang --version
clang version 7.0.1-8 (tags/RELEASE_701/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

# gcc --version
gcc (GCC) 9.1.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

確認作業は、

$ docker run -it kaizenjapan/gccclang_va /bin/bash

でできるはず。

それぞれのソースコードはそのままの名前で掲載しているはず。

#shell script

gccl.sh
#!/bin/sh
# (c) @kaizen_nagoya
echo "https://qiita.com/kaizen_nagoya/items/79e771ff681fb27f6070"
echo "$ clang $1.c $2  $3 -o $1l"
clang $1.c $2 $3 -o $1l 
if [  -e $1l ]; then
./$1l 
fi
echo "\r"
echo "$ gcc $1.c $2 $3 -o $1g"
gcc $1.c $2 $3 -o $1g  
if [  -e $1g ]; then
./$1g 
fi

<この記事は個人の過去の経験に基づく個人の感想です。現在所属する組織、業務とは関係がありません。>

文書履歴(document history)

ver. 0.01 初稿 20190901
ver. 0.02 docker hub掲載 20190902

最後までおよみいただきありがとうございました。

いいね 💚、フォローをお願いします。

Thank you very much for reading to the last sentence.

Please press the like icon 💚 and follow me for your happy life.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0