2

I am developing a small client / server application in C. At the moment I was calling gcc by hand and using my own make files. I had two main() functions, one in server.c and other in client.c.

I imported all the files I had in my project's folder into Eclipse, but when trying to compile it warned me that I had multiple definitions of main. I understand that a given executable should have a single main() function, but in this case I'd like to have 2, so I can run the server and the client, being that they share almost all the other .c files.

How to solve this? The only idea that comes to mind is to separate this in 2 (maybe 3?) projects? I'll want to run both the server and the client concurrently and I'd like to do so (if possible) through Eclipse itself.

Thanks

Edit:

makefile:

table-client: client-lib.o
    gcc -o tabela-client table_client.c table.c base64.c list.c client-lib.o

table-server:
    gcc -o tabela-server table_server.c message.c base64.c entry.c data.c table_skel.c table.c list.c

client-lib.o:
    gcc -c remote_table.c network_client.c message.c data.c entry.c
    ld -r data.o entry.o message.o network_client.o remote_table.o -o client-lib.o

clean:
    rm -f tabela-server
    rm -f tabela-client
    rm -f *.o
devoured elysium
  • 101,373
  • 131
  • 340
  • 557
  • What does your `Makefile` look like? – sarnold Nov 14 '11 at 10:27
  • Possible duplicate of [Building multiple binaries within one Eclipse project](https://stackoverflow.com/questions/2424795/building-multiple-binaries-within-one-eclipse-project) – e4c5 Oct 19 '18 at 06:18

4 Answers4

3

Yes, you better to split your project. And may be it's better to make 3 different projects: server, client, shared library for common functions.

Andrey Atapin
  • 7,745
  • 3
  • 28
  • 34
1

Splitting it up in three projects (server, client and common code in a library) is a good idea. Set the server and client projects up so they depend on the common code library. The common code library can be made as a static library.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

Hey guys we can actually do it like this. I faced the same problem and able to resolve it as follows : Thankyou!

#define my main() 
my() 

{ 

printf("hello frnz"); 

}

So we can have as many mains as we wish. Vola!

jeevanreddymandali
  • 395
  • 3
  • 8
  • 23
0

You can set up so that specific files are included/excluded from a build. By setting up a client build and server build, you can have multiple mains as long as they are kept in separate files and builds.

EDIT: Nevermind, the answer Having 2 or more main() methods in C with Eclipse is better from a maintainability perspective.

Community
  • 1
  • 1
Niklas Hansson
  • 503
  • 2
  • 16