Giter Club home page Giter Club logo

c-separate-compilation's Introduction

c-separate-compilation

C言語の分割コンパイルのチュートリアル

includeの役割

まず分割コンパイルの前に、includeの役割を知ろう。

00 - return 数字 するだけのプログラム

ディレクトリ00main.cを実行すると、ステータスコード120が得られます。

$ cd 00
$ gcc -Wall main.c && ./a.out ; echo $?

ここまでは何の変哲もないC言語のプログラムです。

01 - include 命令のはたらきを確認しよう

次に、ディレクトリ01main.cの中身を見てから、実行してみてください。 00のときと同じ結果になるはずです。

$ cd 01
$ gcc -Wall main.c && ./a.out ; echo $?

includeがファイルを単純に挿入しているだけであることがわかりましたね。 下記のようにすれば、プリプロセスの結果を見ることができます。

$ gcc -E main.c

どうですか? プリプロセッサがinclude命令をファイルの中身に置き換えた結果を見ることができましたね。

分割コンパイル

10 - helloするだけのプログラム

ディレクトリ10main.cを実行すると、"hello"と表示されます。

$ cd 10
$ gcc -Wall main.c && ./a.out

何の変哲もないC言語のプログラムです。

11 - *.cファイルを分割してみる

先ほどのmain.cから、hello関数を別ファイルに分離してみましょう。

そしてgccでコンパイルしてみます。

$ cd 11
$ gcc -Wall main.c hello.c
main.c: In function ‘main’:
main.c:3: 警告: implicit declaration of function ‘hello’
$ ./a.out

警告は出ますが、コンパイルは成功してa.outが出力されます。

(なお、#include <stdio.h>をhello.cに書いてるのは、printfがhello.cにあるからです。)

12 - implicit declaration of functionを解決する

警告が出た原因は、main.cの中でhello()のプロトタイプ宣言を見つけられなかったからです。 なので、main.cの中に単純にhello()のプロトタイプ宣言を書けば警告は消えます。

$ cd 12
$ gcc -Wall main.c hello.c
$ ./a.out

13 - もっと多数の.cファイルがあるケースを考えてみる

多数の.cファイルが、ユーティリティ関数p()を呼び出しているケースを考えてみましょう。 さっきと同じやり方で警告を回避しようとすると、それぞれの.cファイルでプロトタイプ宣言をいちいち記述しないといけません。

$ gcc -Wall main.c morning.c night.c evening.c util.c && ./a.out
good morning
good evening
good night

しかしこの方法には問題があります。

  • 関数の引数や戻り値の型が変わった場合に、それぞれの.cファイルについて同じ修正を適用する必要がある。
  • ユーティリティ関数が増えた場合に、コピペすべきプロトタイプの数が大量になる

つまりはコードが重複しているということです。

14 - includeを使ってDRYに

さあここでincludeの出番です。

プロトタイプ宣言をutil.hというファイルに書いて、それを各.cファイルからincludeしてあげましょう。 これにより、プロトタイプ宣言を何度も書かなくてすみます。

c-separate-compilation's People

Contributors

dqneo avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.