: Header fileCC++使使: Include file

1

CC++[1]CC++

背景

編集

使
int add(int a, int b)
{
    return a + b;
}

この関数を別のソースファイルで参照するには、関数プロトタイプで宣言する必要がある。従って、次のようになる。

extern int add(int, int);

int triple(int x)
{
    return add(x, add(x, x));
}

 add 使

 add add 使 #include 使
#ifndef H_ADD
#define H_ADD

extern int add(int, int);

#endif
#include "add.h"

int triple(int x)
{
    return add(x, add(x, x));
}


#include "add.h"

int add(int a, int b)
{
    return a + b;
}

一般にヘッダファイルはインタフェースだけを提示するのに使われ、そこで宣言されたコンポーネントの使い方をある程度解説する文書を(コメントなどで)含んでいることが多い。上記の例では、サブルーチンの実装は別のソースファイルにあり、個別にコンパイルされる。C言語やC++における例外としてインライン関数がある。多くの実装ではインライン関数の展開はコンパイル時にその定義がないとできない実装になっていることが多いためである。

代替手法

編集

2

JavaC++[2]

脚注

編集
  1. ^ C11 standard, 7.1.2 Standard headers, p. 181, footnote 182: "A header is not necessarily a source file, nor are the < and > delimited sequences in header names necessarily valid source file names.
  2. ^ A Module System for C++ (Revision 4)” (PDF) (英語). JTC1/SC22/WG21 - The C++ Standards Committee (2016年2月15日). 2016年6月5日閲覧。

関連項目

編集

外部リンク

編集