最近更新したページ
2013-10-20
2013-09-29
2013-09-23
2012-01-07
2011-11-09
2011-10-23
2011-10-09
2011-10-01
2011-09-29
2011-09-03
2011-08-07
2011-08-02
2011-07-29
2011-07-10
2011-05-05
2011-05-04
2011-04-24
2011-04-13
2011-04-05
2011-03-26
2011-02-18
2011-02-15
2010-12-26
2010-12-07
2010-12-05
2010-11-23
2010-09-28
2010-09-23
2010-08-26
2010-08-22
2010-07-16
2010-01-17
2010-01-11
2009-10-04
2009-08-21
2009-08-13
2009-06-18
2009-06-01
2009-04-29
2009-02-16
2009-02-11
2009-02-03
2008-07-22
2008-07-21
2008-07-15
2008-07-14
2008-07-13
2008-07-12
2008-07-08
2008-07-05
2008-06-28
2008-06-17
2008-06-05
2008-06-02
2008-06-01
2008-05-29
2008-05-26
2008-05-21
2008-05-19
2008-05-18
2007-10-31
2007-10-27
2007-09-28
2007-09-23
2007-09-17
2007-09-16
2007-09-14
2007-09-11
2007-06-18
2007-04-15
2006-12-21
2006-11-30
2006-11-22
2006-08-17
2006-03-29
2006-03-28
2006-03-27

C/C++ 分割コンパイル(VC++ Turbo C++)


C/C++ 分割コンパイル

 基本的に"統合開発環境"下の分割コンパイルについてのメモ。
 主に VC++ 2005 利用だが、Turbo C++ もちょっと触れておく。

分割コンパイル

 例により sort プログラムを調べる内、派生的に必要性が発生した。はっきり言ってサンプルが長い。
 (TC++ では分割プログラムのための「プログラム」をした。短いソースへそのうち替えたいと思う...)
 コマンドラインと異なり、案外簡単。一度覚えればソース管理が楽になるかも。

 前提条件:同一ソリューション、同一プロジェクト内での分割コンパイル。

 ※気のせいと思うが、分割コンパイルで作成すると若干処理速度が落ちる…?…ような気がする。

 ・・・ 気のせい? ・・・


▲上へ [ 編集 ]

main プログラムのサンプル

 各種ソートプログラムの実行テストと利用目的に最適なソートを考察するのが目的。ついでに実行時間も表示。
 ※qsort が異常に早い。o01_sort の要素数渡しに問題あるかも。(...要確認...)
 ※qsort と「特定のアルゴリズム + ポインタ使用 + 出来る限りインライン化」したコードは、実行速度が確かに早い。 qsort では他と比較して 2桁 早い様で、非推奨だろうがこれは無視出来ない。

main_sort.cpp
#include <iostream>
#include <time.h>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
#include "main_sort.h"
// -----------------------------------------
// main 関数
// -----------------------------------------
void main() {
   const int size = SIZE;                // 配列要素数(作成データ要素数)

   sorting("o01_sort           ", o01_sort,  size); // o01
   sorting("o02_sort           ", o02_sort,  size); // o02
   sorting("qsort              ", cqsort,    size); // qsort
   sorting("std::sort1         ", ssort1,    size); // std::sort
   sorting("std::sort2         ", ssort2,    size);
   sorting("std::sort3         ", ssort3,    size);
   sorting("stable_sort1       ", stb_sort1, size); // stb
   sorting("stable_sort2       ", stb_sort2, size);
   sorting("stable_sort3       ", stb_sort2, size);
   sorting("partial_sort1      ", pat_sort1, size); // pat
   sorting("partial_sort2      ", pat_sort2, size);
   sorting("partial_sort3      ", pat_sort3, size);
   sorting("partial_sort_copy1 ", pcp_sort1, size); // pcp
   sorting("partial_sort_copy2 ", pcp_sort2, size);
   sorting("partial_sort_copy3 ", pcp_sort3, size);
   sorting("nth_element1       ", nth_sort1, size); // nth
   sorting("nth_element2       ", nth_sort2, size);
   sorting("nth_element3       ", nth_sort3, size);
}
// -----------------------------------------
// データ代入 速度計測 各ソートプログラム呼出
// -----------------------------------------
void sorting( const char *fn_name,
              sort_fn    go_sort,
              const int  size)     {
   vector < int >  v(size);
   vector < int > ::iterator it = v.begin();
   for ( ; it != v.end(); ++it) {                  // データ代入
      *it = rand() % RND_MAX;                      // 乱数値制御(修正)
   }

   double start_time = clock();                    // 計測開始
   // ---------------------------------------------------------
   go_sort(v);                                     // ソート実行
   // ---------------------------------------------------------
   double elapsed_time = clock() - start_time;	    // 計測終了

   printf("%s: %12.4f\n",                          // 表示
           fn_name,
           elapsed_time / CLOCKS_PER_SEC);
}
※これはメインになる一部部分のソース。


▲上へ [ 編集 ]

その他ソース


others.cpp
// -----------------------------------------
// テスト状態の表示用関数
// -----------------------------------------
void dsp_arr(const int* p, const int size) {
   for (int i=0; i < size-1; i++) {
      printf("%3d ", *(p+i) );
   }
   cout << endl;
}
※単にテストデータ表示用に作成した、表示用関数。


▲上へ [ 編集 ]

ヘッダーファイルのサンプル

 各関数プロトタイプを一括して記述しておく。プロトタイプ自体が include file を必要とする場合、それをインクルードしておく必要がある。(※ここでは、#include <vector> の記述が無いとエラーになる。)
 ヘッダーファイル名は、他のソースへ埋め込むため簡単に変更できないことに注意。
 その他のソースファイル名は、適当に随時変更しても余り問題無い様である。

main_sort.h
//#include <iostream>
//#include <time.h>
//#include <string>
#include <vector>
//#include <algorithm>
using namespace std;
//#include "main_sort.h"
void dsp_arr(const int*, const int);
// -----------------------------------------
// 比較関数
// -----------------------------------------
// o01_sort -> sub_sort_o01.cpp
// qsort    -> sub_sort_qst.cpp
//int basc_cmp(const void*, const void*);
//int bdes_cmp(const void*, const void*);
//int qasc_cmp(const void*, const void*);
//int qdes_cmp(const void*, const void*);
// 修正前 stl 用 比較関数
int ss1cmp(const int&, const int&);   // stl 2
class ss2cmp {                        // stl 3
public:
   bool operator()(const int&, const int&) const;
};
// 修正(追加)後の比較関数
int asc1_cmp(const int&, const int&);
int des1_cmp(const int&, const int&);
class asc2_cmp { public: bool operator()(const int&, const int&) const; };
class des2_cmp { public: bool operator()(const int&, const int&) const; };
// -----------------------------------------
// ソート関数
// -----------------------------------------
void o01_sort(vector < int > &);  // o01
void o02_sort(vector < int > &);  // o02
void cqsort(vector < int > &);    // qsort
void ssort1(vector < int > &);    // std::sort
void ssort2(vector < int > &);
void ssort3(vector < int > &);
void stb_sort1(vector < int > &); // stable_sort
void stb_sort2(vector < int > &);
void stb_sort3(vector < int > &);
void pat_sort1(vector < int > &); // partial_sort
void pat_sort2(vector < int > &);
void pat_sort3(vector < int > &);
void pcp_sort1(vector < int > &); // partial_sort_copy
void pcp_sort2(vector < int > &);
void pcp_sort3(vector < int > &);
void nth_sort1(vector < int > &); // nth_element
void nth_sort2(vector < int > &);
void nth_sort3(vector < int > &);
// -----------------------------------------
// その他初期設定等
// -----------------------------------------
typedef void (*sort_fn)(vector < int > &);     // 関数ポインタ型定義
void sorting(const char*, sort_fn, const int); // ソート実行速度計測関数
#define SIZE         10000                     // 要素数
#define PARTIAL_SIZE 100                       // 部分ソート時のソート要素数
#define RND_MAX      100                       // 乱数最大値(追加)
※外部ソースのプロトタイプ、他を纏めてインクロードファイルとする。
※VC++2005 の場合、作成したインクルードファイルの読込先をプロパティで設定しないと読み込まれない。
※Turbo C++ は、VC++ のように読み込み先指定しなくても勝手に読み込まれる。(サンプルソースは TC++ での動作チェックしていない。)


▲上へ [ 編集 ]

外部関数ファイルのサンプル


※以下別項参照

fn_comp.cpp -> ソートの比較用共通関数
※関数プロトタイプと共通項目を纏めてヘッダへ組み込みする。
※STL algorithm の各ソートのプロトタイプは、基本的に共通(使い回し可)して利用可能。

sub_sort_o01.cpp -> その他のソート
※他の頁(たしか「構造体とソート」の頁?)から単一データ仕様へ修正して使えるようにした関数。
※比較関数は、構造体ソート用を単一データ用へ修正。qsort の比較関数と互換なので代用可。
※swap 関数は、標準の swap 関数でも良い。動作的には 関数とせずインライン化すべき(これを関数にする意味は無い。この場所はオーバーヘッド要因を極力排除すべき場所(※最適化で速度を左右))と思うが面倒なのでそのまま利用。

sub_sort_std.cpp -> STL sort
※STL の std::sort は、内部的に Quick Sort といわれているが C標準 qsort と比較すると遥かに遅い結果が出てしまった。qsort が正しく動作してるか未確認なので...こちらが正しいのかも知れないが...

sub_sort_pat.cpp -> STL partial_sort
※partial_sort 1,2,3 部分ソート:劇的に遅い

sub_sort_pcp.cpp -> STL partial_sort_copy
※partial_sort_copy 1,2,3 部分ソートの結果を別配列へ書き出す。これも劇的に遅い。 

sub_sort_stb.cpp -> STL stable_sort
※stable_sort 1,2 安定ソートだが、案外処理が早い(...というかstd::sort と余り変わらない)ように思われる。

 メインプログラム、ヘッダファイル、外部関数(値を返さない sub_sort_***.cpp x 6 値を返す fn_comp.cpp x 1)と合計 9個のファイルで構成されたプログラム。


▲上へ [ 編集 ]

分割コンパイル時の注意点

 統合環境下で行う分割コンパイルなら、ヘッダファイル、各外部プログラムのプロトタイプ(2重管理となるのが面倒である)などに気を配れば案外簡単である。

 反面リンクエラーが出やすくなる。リンクエラーの原因は上記を再確認することで回避できる場合が多い模様。リンクエラーさえ克服できればソース管理が楽になる。
  

▲上へ [ 編集 ]

VC++


 分割コンパイルを簡単に出来る。
 但し、ヘッダファイル読込先をプロパティから指定しないとならない。
 (※DLL の際も再三行った(やらされた)作業なので、VC++ 自体の初期設定時云々と言うことでも無いと思われる)

▲上へ [ 編集 ]

Turbo C++

 ※注:上記サンプルは Turbo C++ で(多分)動かない。(String が標準で無いようなので...)
 ※TC++ で新規 C/C++ ファイルを作成すると、なんちゃら、カンチャラ、コードがおまけで付いてくるが、全部削除して書き直しても、何ら問題なく動いた。(VC++ では Empty project で作成してるので...)

 分割コンパイルを簡単に出来る。
 VC と異なり ヘッダファイルの場所設定をする必要なく動いた。
 (※逆に VC++ で指定しないといけないのが、不思議なほどである。)
 プロジェクトファイル管理が、平面空間(同一フォルダに存在)管理のようでチト面食らったが...
 C/C++(*.c *.cpp) 追加がわかりづらい。
 (※まあ、慣れの問題と思う)

▲上へ [ 編集 ]

その他の処理系

 コマンドラインから分割コンパイルするには、make とか、link とかもっと知る必要がある。また、処理系別にコマンドが異なる。はっきり言って敷居が高い。

▲上へ [ 編集 ]

リンク


内部リンク


外部リンク


  • 現在ありません

▲上へ [ 編集 ]
2008年07月22日(火) 02:43:58 Modified by cafeboy1




スマートフォン版で見る