包含的C文件也可以直接运行吗?

Can included C files be also ran directly?

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

此代码可由另一个 python 文件导入 (import ABOVESCRIPT) 以提供对 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

如何实现这个 'double-use' 脚本?

注意:C 文件不是脚本。

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

但是,有些东西非常接近于复制您想要的东西。你可以做的是首先,只在第一个包含文件中编写主要方法。在主文件中,将 C stdlib.h 文件(调用 main 末尾的另一个函数)中的 atexit() 函数设置为 main2() 函数(确保每个 main#( ) 在第一个 header 中运行,并最终实现所有的功能)。在函数中用原来的main定义一个宏叫MAIN_ONE。在每个连续包含的文件中,实现下一个 main 并创建一个宏,以便检查功能是否已实现。然而,用 C 编写程序的自然且最有效的方法是只有一个主要功能。

示例: //在第一个包含文件中 #include //有些IDE会自动包含这个。这必须包括在内,因为它是 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

您不能 link 同时 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();
}

我确实在生产中广泛使用的代码中看到过类似的东西,虽然我不推荐这种风格(但它有时是一个诱人的捷径)。

另一种选择是仅在定义了预处理器宏时才包含 main 函数,如下所示(在 myScript.c 中):

#include "myScript.h"

void functionsToBeImported() {
}

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

这在本质上类似于 Python 方法。但同样,您必须将此文件编译两次为单独的目标文件。