tags:

views:

151

answers:

9

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.

+6  A: 

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
}
Billy ONeal
Also, just FYI to the OP, just add `-DTESTING` to the list of arguments to gcc.
Marc Bollinger
+2  A: 

Edit: Billy beat me to the answer, but here's a bit more background

More directly, main is usually more a function of the standard library. The thing that calls main isn't C, but rather the standard library. The OS loads the application, transfers control to the library's entry point (_start in GCC), and the library ultimately calls main. This is why the entry point for a windows application can be WinMain, and not the usual. Embedded programming can have the same sort of thing. If you don't have a standard library, you have to write the entry point that the library normally provides (among other things), and you can name it whatever you want.

In the GCC toolchain, you can also replace the library's entry point with your own by using the -e option. (For that matter, you can also remove the library entirely.)


Make your own:

int main(int argc, char *argv[])
{
#if defined(BUILD_UNIT_TESTS)
    return main_unittest(argc, argv);
#endif
#if defined(BUILD_RUNTIME)
    return main_run(argc, argv);
#endif
}

If you don't like ifdef, then write two main modules that contain only main. Link one in for unit tests and the other in for normal use.

mschaef
+4  A: 

I'm assuming that you're using Make or something similar. I would create two files that contain different implementations of the main function, then in the makefile, define two separate targets that have identical dependencies on the rest of your files, except one uses your "unit test main" and the other your "normal main". Something like this:

normal: main_normal.c file1.c file2.c
unittest: main_unittest.c file1.c file2.c

As long as the "normal" target is closer to the top of the makefile, then typing "make" will choose it by default. You would have to type "make unittest" to build your test target.

Paul
builds with autotools
Arthur Ulfeldt
+1: I vastly prefer this approach to trying to cram both mains into the same main routine and have preprocessor defines or linker options switch between them.
Owen S.
A: 

` #ifdef TESTING

int main()

{

/* testing code here */

}

#else

int main()

{

/* normal code here */

}

#endif

$ gcc -DTESTING=1 -o a.out filename.c #building for testing

$ gcc -UTESTING -o a.out filename.c #building for normal purposes `

man gcc showed me the -D and -U

vpit3833
The only downside of this is that you need to have the entire code of `main` inside a `define`... some syntax coloring of some IDEs will grey out all the testing code (since it's not normally defined) which can become quite annoying.
Billy ONeal
+3  A: 

You can use macros to rename one function to main.

#ifdef TESTING
#define test_main main
#else
#define real_main main
#endif

int test_main( int argc, char *argv[] ) { ... }
int real_main( int argc, char *argv[] ) { ... }
drawnonward
+3  A: 

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.

Carl Norum
A: 

You may have to run ld separately to use them, but ld supports scripts to define many aspects of the output file (including the entry point).

Jerry Coffin
But `main` isn't the entry point (at least on most systems).
Carl Norum
+2  A: 

I'd tend to use different files and make for testing and production builds, but if you did have a file with

int test_main (int argc, char*argv[])

and

int prod_main (int argc, char*argv[])

then the compiler options to select one or the other as the main are -Dtest_main=main and -Dprod_main=main

Pete Kirkham
A: 
Norman Ramsey