2

I'm trying to generate two exes, client and server using a C Project in eclipse. Both have main functions since they are different exes. Both use a common confutils.c file. How do I solve the multiple definitions of main problem. I know that we cannot have two mains in a single project. My makefile looks like below.

.c.o:
    gcc -g -c $?

# compile client and server
all: confclient confserver

# compile client only
confclient: confclient.o confutils.o
    gcc -g -o confclient confclient.o  confutils.o

# compile server program
confserver: confserver.o confutils.o
    gcc -g -o confserver confserver.o  confutils.o

I replaced eclipse file with this. I don't know if eclipse read it properly. I don't like to split this project into two since it is such a simple program. I am able to run these files in unix with no issues. I'm trying to find if there is an option in eclipse using which we can setup build configurations.

TechCrunch
  • 2,924
  • 5
  • 45
  • 79
  • It is unlikely you made this mistake, since it appears you know what you're doing, but check the output of the error in detail. Is this a link-time error with either the client or server? Or is this simply an error flagged by eclipse but the project actually links fine? Specifically look at the object code modules that are claiming residence of the multiple `main()` definitions. Is it remotely possible somehow you put main in *all three* ? – WhozCraig Sep 20 '13 at 22:17
  • It works when I run the files in Unix. But when I try the same in eclipse, I get this error. I'm certain that the error is because I have main in confclient.c and confserver.c, but both don't go into same exe file. – TechCrunch Sep 20 '13 at 22:19
  • Then it sounds like eclipse is trying to link your project for you by linking all three files. This should be doable as two executables with the proper setup in eclipse. Alas my eclipse skillz are not leet, or I'd send you in the right direction. – WhozCraig Sep 20 '13 at 22:20
  • @WhozCraig, thank you for taking time to respond. Let us see if some eclipse experts can help us. – TechCrunch Sep 20 '13 at 22:21
  • Wish I could help you more. Wishing you the best of luck. – WhozCraig Sep 20 '13 at 22:26

2 Answers2

2

It's possible to define multiple build configurations - each having different set of files that are built.

First open Project properties and navigate to Manage Configurations:

enter image description here

Create a new build configuration:

enter image description here

And set it active:

enter image description here

Finally define filter excluding file(s) to ensure that you build a source file with main() you want to use only:

enter image description here

And that's it:

enter image description here

Now you can choose target build configuration from Menu > Project > Build Configurations > Set Active.

Martin Dvorak
  • 790
  • 8
  • 15
0

I think you can set up multiple "Build Configurations" to avoid this. Under "Project" in the menu bar you can choose your active build configuration.

There are ways to exclude files in different configurations. Under your project properties you go to "C/C++ General", "Paths and Symbols" and here you choose "Source Location". Top most you can now choose your build configuration and then define different sorurce locations for you different build configurations. Here you add folders for your source code, and, here comes the trick, for the folders you choose to include you can put filter to ignore specific files.

In your case I would make two Build Configurations, one for confclient and for this one I would exclude confserver.c from the scource location, and vice versa.

You will need to build you project twice, but the utilspart will be left unchanged and hence it wont recompile which is a benefit over having two projects. Also it might be nice to have all the code in the same project since I guess the code on the server side and client side will be connected.

Emil
  • 1