0

OpenCL 言語で記述された別の動的プログラムのコードをアセンブルするプログラムを作成しています。この質問の目的のために、動的プログラム言語が C99 であり、すべてのコードを単一のコンパイル ブロックに入れる必要があるという制限があると仮定しましょう。

私のより大きなプログラムは、メイン関数で発生する一連のステップと、それらのステップを実装するために必要な C99 ソース ファイルを決定します。次に、C99 プリプロセッサ/コンパイラを呼び出して、動的プログラムの実行可能ファイルを生成します。-D SYMBOL=defn呼び出し引数を使用して、コンパイラ マクロ定義を指定できます。

これは、私にとってうまくいき、別の前処理ライブラリや他の多額の文字列操作ツールを利用することを避けるためのスキームの例です。mainfile.c としてリストされている文字列を動的に作成すると、含まれる他のファイルが実際に存在します。

別々の宣言ファイルと定義ファイルを 1 つのファイルに結合する方法はありますか?

// mainfile.c
#include "ParentDeclarationFile.txt"
#include "ChildDeclarationFile.txt"
#include "ParentDefinitionFile.txt"
#include "ChildDefinitionFile.txt"

int main()
{
    return aFunctionParent( 5 );
}

// ParentDeclarationFile.txt
typedef float Type1;
int aFunctionParent( Type1 t );

// ChildDeclarationFile.txt
int aFunctionChild( Type1 t );


// ParentDefinitionFile.txt
int aFunctionParent( Type1 t ) { return aFunctionChild( t ); }

// ChildDefinitionFile.txt
int aFunctionChild( Type1 t ) { return 2*t; }

#include を 2 回できる、このような 1 つのファイルが必要です。

// SingleParentFile.txt
#ifdef DECLARATION_MODE
    //declarations here
#else
    // definitions here
#endif

ただし、#define定義は #include されたファイルには渡されず(編集: 正しくないことが判明しました)、提供され-Dた定義は前処理の途中で変更できません。右?最初のパスの後に並べ替えることができるプリプロセッサ状態変数のようなものはありますか?

注: 宣言セクションと定義セクションを別々のコードの場所に表示する必要がある簡単な例を提供しようとしました。私の質問の意図は、この単純な例でそれを回避する方法を見つけることではありません.

4

1 に答える 1

0

簡単な解決策の1つは#define、マクロを使用して、コード内の定義セクションから宣言を区別することです。DOESはファイル#defineに伝播し#includeます。

// mainfile.c
#define DECLARATION_MODE
#include "ParentFile.txt"
#include "ChildFile.txt"
#undef DECLARATION_MODE
#include "ParentFile.txt"
#include "ChildFile.txt"

int main()
{
    return aFunctionParent( 5 );
}

// ParentFile.txt
#ifdef DECLARATION_MODE
    typedef float Type1;
    int aFunctionParent( Type1 t );
#else
    int aFunctionParent( Type1 t ) { return aFunctionChild( t ); }
#endif

// ChildFile.txt
#ifdef DECLARATION_MODE
    int aFunctionChild( Type1 t );
#else
    int aFunctionChild( Type1 t ) { return 2*t; }
#endif
于 2012-04-05T00:54:16.413 に答える