Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 845
Arthur Ulfeldt Avatar asked Jun 22 '10 23:06

Arthur Ulfeldt


People also ask

Does gcc have a linker?

GCC uses a separate linker program (called ld.exe ) to perform the linking.

What are gcc options?

When you invoke GCC, it normally does preprocessing, compilation, assembly and linking. The "overall options" allow you to stop this process at an intermediate stage. For example, the -c option says not to run the linker.

Is G ++ and gcc the same?

DIFFERENCE BETWEEN g++ & gccg++ is used to compile C++ program. gcc is used to compile C program.

What is the default program name when compiling with gcc?

By default, gcc assumes that you want to create an executable program called a.exe. Here are the common options that you'll use to change what gcc does. To change where the output file goes, use the -o option, like "gcc hello. c -o hello.exe".


2 Answers

The other answers here are quite reasonable, but strictly speaking the problem you have is not really one with GCC, but rather with the C runtime. You can specify an entry point to your program using the -e flag to ld. My documentation says:

-e symbol_name

Specifies the entry point of a main executable. By default the entry name is "start" which is found in crt1.o which contains the glue code need to set up and call main().

That means you can override the entry point if you like, but you may not want to do that for a C program you intend to run normally on your machine, since start might do all kinds of OS specific stuff that's required before your program runs. If you can implement your own start, you could do what you want.

like image 168
Carl Norum Avatar answered Oct 14 '22 03:10

Carl Norum


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
}
like image 22
Billy ONeal Avatar answered Oct 14 '22 03:10

Billy ONeal