2011年11月11日金曜日

プロットフォームの識別方法あれこれ

windowsでもlinuxでも動く、プラットフォーム独立なソフトってよく作るけどその時の小ネタとかメモ


  • GCCの定義済みマクロを調べたいとき
$ cpp -dM /dev/null

  • コンフィグ用のヘッダー
myconfig.h でプラットフォームの識別をして、各プラットフォームごとの処理はおのおののヘッダーに割り振る。

#ifndef __MY_CONFIG_H__
#define __MY_CONFIG_H__

#if defined(linux) && defined(__arm__) // ターゲット機器用
    #include "my_config_arm.h"
#elif defined(linux) && defined(__i386__) // PC Linux用
    #include "my_config_pc.h"
#elif defined(WIN32) // Windows用
    #include "my_config_win32.h"
#else // プラットフォーム不明
    #error
#endif

#endif // __MY_CONFIG_H__

0 件のコメント:

コメントを投稿