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.
GCC uses a separate linker program (called ld.exe ) to perform the linking.
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.
DIFFERENCE BETWEEN g++ & gccg++ is used to compile C++ program. gcc is used to compile C program.
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".
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.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With