2017-11-10 89 views
1

我想知道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 

此代码可以被导入(import ABOVESCRIPT)由另一个Python文件给访问functionsToBeImported,或独立运行(python ABOVESCRIPT.py)到if块内执行的代码。

我已经尝试通过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()来统治它们。 –

+0

我想到了一种方法来稍微复制你正在说的内容并在下面编辑我的答案。 –

回答

1

不能链接都anotherScript.omyScript.o,但你可以做这样的事情:

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

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

    functionsToBeImported(); 
} 

其实我已经看到的东西像这在代码中非常广泛地用于生产,虽然我不能推荐这种风格(但它有时是一个诱人的捷径)。

另一种选择是包括仅当预处理器宏被定义,像这样(在myScript.c)的main功能:

#include "myScript.h" 

void functionsToBeImported() { 
} 

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

这在本质上与Python的方法类似。但是,你必须再次将这个文件编译成两个单独的目标文件。

1

注意:C文件不是脚本。

你不能有两个主要函数,因为C是一种过程语言,这意味着你必须一次做一件事(除非你是多线程的,在这种情况下,你仍然只有一个主函数)。

然而,有一些相当复制你想要的东西。你可以做的是首先,只在第一个包含的文件中写入主要方法。在主文件中,从C stdlib.h文件(在main的结尾调用另一个函数)中设置atexit()函数到main2()函数(确保每个main#()函数的原型在第一个标题中,并最终实现所有的功能)。在原始主函数中定义一个名为MAIN_ONE的宏。在每个连续包含的文件中,执行下一个main并创建一个宏,以便检查函数是否已实现。然而,用C编写程序的最自然和最有效的方法是只有一个主要功能。

例如: //在第一个包含的文件中 #include //一些IDE automaticaly包含了这个。这必须包括在内,因为它是其中的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

@反地球我加了一个例子 –

相关问题