0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C++ Builder > IDE > [ファイル]-[使用するユニット]でincludeする位置をコントロールする > #ifdef使用

Last updated at Posted at 2016-11-30
動作確認
C++ Builder XE4

IDEの標準機能として、メニューの[ファイル]-[使用するユニット]を使うことで、ソースもしくはヘッダファイルに#include "Unit2.h"などを追加できる。

通常の追加

Unit1.cpp
# include "Unit1.h"
# include "Unit2.h"

[ファイル]-[使用するユニット]にてUnit3.hを追加すると以下となる。

Unit1.cpp
# include "Unit1.h"
# include "Unit2.h"
# include "Unit3.h"

追加場所のコントロール

こちらは、IDEを使っている中で偶然見つけた機能。

Unit1.cpp
# include "Unit1.h"

# define DEBUG_XXX
# ifdef DEBUG_XXX
# include "Unit2.h"
# endif

[ファイル]-[使用するユニット]にてUnit3.hを追加すると以下となる。

Unit1.cpp
# include "Unit1.h"
# include "Unit3.h"

# define DEBUG_XXX
# ifdef DEBUG_XXX
# include "Unit2.h"
# endif

どうも#ifdefで囲われたincludeは最後尾とみなさないようだ。

使いどころ

Productionとしての#includeと一時的な#includeを並べた時に、[ファイル]-[使用するユニット]ではProductionコード側にincludeするようにする、などに使えるかもしれない。

Unit1.cpp
// Production code
# include "ScreenUtil.h"
# include "UdpUtil.h"
# include "Unit1.h"
//    <<--- ここに追加される

# define DEBUG_XXX
# ifdef DEBUG_XXX
# include "DebugScreen.h"
# include "DebugUdpComm.h"
# endif

開発を進める中で、デバッグ用includeが並んだ後にProduction用includeが間に並んでしまい、出荷前に整理する、というのを何回が実施してきた経緯がある。

#ifdefの記載の意図が分かりやすいかどうかの問題はある。

気づいた事項 (2016/12/04)

  • 誤: #ifdefで囲った部分は無視される
  • 正: #ifdefの行以降は無視される

何が違うかというと、後者の場合、#ifdef ...#endifの後にincludeしたものがあったとしても、「その下に追加されず、#ifdefの前に追加される」ことになる。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?