Search code examples
cgccprogram-entry-pointentry-point

is there a GCC compiler/linker option to change the name of main?


My software has one main for normal use and a different one for unit tests. I would just love it if there was an option to gcc to specify which "main" function to use.


Solution

  • Put them in separate files, and specify one .c file for normal use, and one .c file for testing.

    Alternately, #define testing on the commandline using test builds and use something like:

    int main(int argc, char *argv[])
    {
    #ifdef TESTING
        return TestMain(argc, argv);
    #else
        return NormalMain(argc, argv);
    #endif
    }
    
    int TestMain(int argc, char *argv[])
    {
        // Do testing in here
    }
    
    int NormalMain(int argc, char *argv[])
    {
        //Do normal stuff in here
    }