LoginSignup
5
0

More than 5 years have passed since last update.

C言語におけるヘッダファイルinclude時のパス指定

Posted at

C/C++で、複数のディレクトリにまたがってヘッダファイルをincludeする場合に自分がとっている方法を記しておきます。

root
|-dir1
   |-main1.c
|-dir2
   |-main2.c
|-dir3
   |- config.h

ここでmain1.cmain2.cの両方でconfig.hをインクルードしたい場合、2つのファイルで

#include "../dir3/config.h"

と書くと思いますが、

../dir3/

を書くのが多少煩わしいです。

そこで以下のようなヘッダファイルを作成します。

config_sub.h
#include "../dir3/config.h"

そして以下のように配置します。

root
|-dir1
   |-main1.c
   |-config_sub.h
|-dir2
   |-main2.c
   |-config_sub.h
|-dir3
   |- config.h

こうすることで、main1.cmain2.cでは

#include "config_sub.h"

とすることでconfig.h
の内容をincludeすることができます。

ファイルを手動で作成するのが煩わしい場合はPythonでスクリプトを書いたりすることで対応できるかもです...

5
0
5

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
5
0