You could also simulate having two main
functions in the same project by having main
call either mainClient
or mainServer
(your two main functions renamed) depending on a condition of your choice.
Two main() in different source files under same project
-
03-10-2022 - |
Frage
Hey guys can we use two or more main()
in different C source files under same project in eclipse? What am I actually trying is to write a server and a client source file under same project with main() in each of it. I am getting an error main() redeclaration
. is there any way to do this? If yes please tell me how to run that successfully in eclipse CDT Kepler. Regards,
Lösung
Andere Tipps
Yes, you just have ton include each one selectively when linking your two programs.
A build system (Makefile, IDE…) helps.
Example
If you have these source files:
Client only:
- main-client.c
- source1-client.c
Server only:
- main-server.c
- source1-server.c
Common sources:
- source1-common.c
- source2-common.c
- source3-common.c
Then a simple (stupid) Makefile is:
all: client server
client:
gcc -o client main-client.c source1-client.c source1-common.c source2-common.c source3-common.c
server:
gcc -o server main-server.c source1-server.c source1-common.c source2-common.c source3-common.c
Hey guys we can actually do it by following what Simon and Brandin suggested. Also see this example if anyone still have doubts. Thankyou!
#define my main()
my()
{
printf("hello frnz");
}
So we can have as many mains as we wish. Vola