2017-11-10 11 views
1

を別のスクリプト(ヘッダファイル経由)に含めることができるかどうかと、独自のmain関数を使用してCファイルを実行できるかどうかは疑問です。つまり、Cファイルをインクルードしてその機能を別のCスクリプトに提供することもできますが、代わりに別の機能を提供するために直接実行することもできます。Cのファイルもインクルードできますか?

たとえば、Pythonスクリプトでこれを行うことができます。

def functionsToBeImported(): 
    # code to be run by an importing script 
    pass 

if __name__ == '__main__': 
    # code to be run by this script independently 
    pass 

このコードはifブロック内のコードを実行する(python ABOVESCRIPT.pyfunctionsToBeImportedへのアクセス権を与えるか、または独立して実行する別のPythonのファイルで(import ABOVESCRIPT)インポートすることができます。

私は myScript.cを経由してCでこれを行うことを試みてきました

#include "myScript.h" 

void functionsToBeImported() { 
} 

int main (int narg, char* varg[]) { 
} 

myScript.h

#ifndef MY_SCRIPT_H_ 
#define MY_SCRIPT_H_ 

void functionsToBeImported(); 

#endif // MY_SCRIPT_H_ 

が、anotherScript.cでこれを含めるしようとしています

#include "myScript.h" 

int main (int narg, char* varg[]) { 

    functionsToBeImported(); 
} 

gcc -std=c99 -c myScript.c 
gcc -std=c99 -c anotherScript.c 
gcc -std=c99 -o anotherScript anotherScript.o myScript.o -lm 

を経由してコンパイルしようとは、どのように私はこの「二重使用」スクリプトを達成することができ、コンパイルエラー

duplicate symbol _main in: 
    myScript.o 
    anotherScript.o 

ことができますか?

+1

すべてを統治するmain()が1つです。 –

+0

私は、あなたが言っていることをやや複製し、私の答えを編集する方法を考えました。 –

答えて

1

を常駐場所あなたがanotherScript.omyScript.oの両方をリンクすることはできませんが、あなたはこのような何かを行うことがあるので、これは含まれていなければなりません私はこのスタイルをお勧めできませんが(これは時には魅力的なショートカットです)、これは生産で非常に広く使用されているコードです。

#include "myScript.h" 

void functionsToBeImported() { 
} 

#ifdef USE_MAIN 
int main (int narg, char* varg[]) { 
} 
#endif // USE_MAIN 

これはPythonのアプローチと精神に類似している:

別のオプションは、この(myScript.cで)ように、プリプロセッサマクロが定義されている場合にのみmain機能を含めることです。しかし再び、このファイルを別々のオブジェクトファイルに2回コンパイルする必要があります。

1

注:Cファイルはスクリプトではありません。

Cは手続き型言語であるため、一度に1つのことを行う必要があります(マルチスレッドでない限り、メイン関数は1つしかありません)。

ここで、あなたが望むものを複製するのに非常に近いものがあります。あなたができることはまず、最初のインクルードファイルでのみmainメソッドを書いてください。メインファイルで、atexit()関数をCのstdlib.hファイル(メインの最後に別の関数を呼び出す)からmain2()関数に設定します(各メイン#()関数のプロトタイプがあることを確認してください最初のヘッダーでも同様に、すべての関数を実装します)。 MAIN_ONEと呼ばれるマクロを元のメインの関数で定義します。連続してインクルードされている各ファイルで、次のmainを実装し、関数が実装されているかどうかを確認するためのマクロを作成します。しかし、Cでプログラムを作るための自然で最も効率的な方法は、ただ一つの主な機能を持つことです。

例: //最初にインクルードされたファイル #include //これにはいくつかのIDEが自動的に含まれています。私は実際のようなものを見てきました

#define main ignored_main 
// Include myScript.c, not myScript.h 
#include "myScript.c" 
#undef main 

int main (int narg, char* varg[]) { 

    functionsToBeImported(); 
} 

:それはatexitを()関数は

#define MAIN_ONE 
void main2(); //For the moment, this is only a prototype. 
void main3(); 
//etc. Until you have created the maximum number of main functions that you can have 
int main() { 
    //do something 
    atexit(main2); // This will execute the function main1() once main returns 
    //All "fake" mains must be void, because atexit() can only receive void functions 
} 

//In second included file 
#if defined(MAIN_THREE) //start from the maximum number of main functions possible 
    #define MAIN_THREE //The define is for preprocessor-checking purposes 
    void main4() { 
     atexit(main5); 
    } 
#elif defined(MAIN_TWO) //start from the maximum number of main functions possible 
    #define MAIN_TWO 
    void main3() { 
     atexit(main5); 
    } 
//Keep repeating until you reach #ifdef(MAIN_ONE) 
#endif 

//At the bottom of the main C file 
//This is done in order to make sure that all functions have actually been created and reside in memory so that an error does not occur 
//(all unused functions are initialized with an empty function here) 
#if defined(MAIN_THREE) //start from the maximum number of main functions possible 
    //Do nothing because if MAIN_THREE is defined when main4(), the last main in my example has already been implemented. 
    //Therefore, no more functions need to be created 
#elif defined(MAIN_TWO) //start from the maximum number of main functions possible 
    #define MAIN_TWO //Since more mains after main2 can be present, another macro for future checks needs to be defined 
    void main3() { 
    } 
//Keep repeating until you reach #ifdef(MAIN_ONE) 
#endif 
+0

私はあなたの最初の段落の妥当性を理解できません。プロポーザルにいくつかのスニペットを提供できますか? –

+0

@Anti Earth例を追加しました –

関連する問題