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.
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Index of single bit in long integer (in C) [duplic
- Equivalent of std::pair in C
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).If you use into
LD "-e symbol_name"
(where symbol_name is your main function, of course) you need also"-nostartfiles"
otherwise error of"undefined reference to main"
will be produced.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 told
. My documentation says: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 ownstart
, you could do what you want.$ 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 -UI'd tend to use different files and make for testing and production builds, but if you did have a file with
and
then the compiler options to select one or the other as the main are
-Dtest_main=main
and-Dprod_main=main
You can use macros to rename one function to main.