is there a GCC compiler/linker option to change th

2019-01-14 16:55发布

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.

11条回答
不美不萌又怎样
2楼-- · 2019-01-14 17:19

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.

查看更多
闹够了就滚
3楼-- · 2019-01-14 17:19

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.

查看更多
Emotional °昔
4楼-- · 2019-01-14 17:19

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
}
查看更多
虎瘦雄心在
5楼-- · 2019-01-14 17:30

I had the same problem today: m1.c and m2.c both had a main function but needed to be linked and run one of them. Solution: user STRIP to remove the main symbol from one of them after compilation but before linking:

gcc -c m1.c m2.c;  strip --strip-symbol main m1.o; gcc m1.o m2.o; ./a.out

will run main from m2

gcc -c m1.c m2.c;  strip --strip-symbol main m2.o; gcc m1.o m2.o; ./a.out

will run main from m1

Without strip:

gcc - m1.c m2.c
m2.o: In function `main':
m2.c:(.text+0x0): multiple definition of `main'
m1.o:m1.c:(.text+0x0): first defined here
collect2: ld returned 1 exit status
查看更多
一夜七次
6楼-- · 2019-01-14 17:30

First of all, you can't have two functions named main in one compilation, so either the sources are in different files or you're using conditional compilation. Either way you've got to get two different .o files out. Therefore, you don't need a linker option; you just pass the .o file that you want in as an argument.

If you don't like that, you can do fancy things with dlopen() to pull main from any object file you name dynamically. I can imagine circumstances where this might be useful—say, you take a systematic approach to unit tests, just put them all in a directory, and your code walks the directory, grabbing each object file, dynamically loading it, and running its tests. But to get started, something simpler is probably indicated.

查看更多
登录 后发表回答