0

Currently i am trying to make Client Server prototype with protocol buffer in visual studio 2010, what i am doing now, i have to create two different project(One for server file and another one for client file) in the same solution and it works in this way perfectly.

My problem is that if i put both server and client cpp file in a single project it gives error because there are two mains(Main in server and main in client file). Do any of you have any idea to execute both files by leaving them in a single project instead of creating two seperate projects in the same solution using Visual Studio?

  • 1
    Now why would you want to do that? – Tony Hopkinson Jan 13 '14 at 17:09
  • I feel its more convinient and to see how the things work other way around. –  Jan 13 '14 at 17:13
  • I feel its' good way to get your client and server code thoroughly intertwined, but okay. You could look at renaming them and then linking using the entry point. Not sure where it's sits in VS's project config though. See here http://msdn.microsoft.com/en-us/library/f9t8842e.aspx – Tony Hopkinson Jan 13 '14 at 17:20
  • 1
    Don't try to do this - this is exactly *why* the solution/project hierarchy exists in Visual Studio in the first place, so that you can group together related but separate projects. If both projects have a `main` function, then they are producing separate outputs and should be in separate projects. If you want to share code between the projects then this is not a problem - use references and include paths. – JBentley Jan 13 '14 at 17:38
  • Hmm.. Thanks, Based on all answers and comments. i got the point :) –  Jan 13 '14 at 17:41

3 Answers3

2

You should create multiple projects.

You can then right-click the solution, open Property Pages, and set multiple startup projects.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Than you but as i said set as multiple start project and by setting action to ->Start is easier, i can do this but if you can please explain if there is any opportunity(in any case if its feasible only then) to keep several main files in a single project ? –  Jan 13 '14 at 17:08
  • @Alm: You can make a single `main()` function that calls multiple other functions. You may want to use threads. Beware of shared global state. – SLaks Jan 13 '14 at 17:23
2

You can combine them as clienMain and serverMain in another main that is the real main function and in which you wait for inputs for executing the functions. (Or that one shoulf be the serverMain)

thedarkside ofthemoon
  • 2,251
  • 6
  • 31
  • 48
  • Can you please write a bit more how its possible ? or if you have a sample code it would be great! Thanks –  Jan 13 '14 at 17:14
  • What i want to say is that you can add the client in the server project and send request, at a certain time (see [time](http://www.cplusplus.com/reference/ctime/time/)) and simulate the thing. Or have you seen [this one](http://stackoverflow.com/q/16120178/1360570)? – thedarkside ofthemoon Jan 13 '14 at 17:38
2

I think you'll have to create multiple projects. This is a fundamental weakness of Visual Studios, although in your case, I'd probably use three projects anyway: one which creates a static library with all of the protocol handling code, and one each for the server and for the client, both of which link against (depend on) the library with the common code.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • I wouldn't necessarily consider this a weakness - a project is meant to represent a single output (.lib, .dll, .exe, etc.) and a solution is meant to represent a collection of related outputs. Having a `main` function qualifies as being a "separate output", so it makes little sense to mix two sets of code together if they both contain `main`. Throughout most of my solutions, I have an extended version of your suggested structure - common backend code in a static lib, server and client specific backend code in static libs, then GUIs (with `main`) which link to those. – JBentley Jan 13 '14 at 17:42
  • @JBentley It's a decided weakness in a few special cases. In his case, as I said, I don't think it is: he has two exportables (output), and so should have two separate projects. But I've a couple cases (under Unix) where I build a small executable to generate code for the exportable. This executable is not exported, and is only used in the project which builds it. Having to create a separate project for an executable which shouldn't be visible outside a different project is a bummer. – James Kanze Jan 13 '14 at 18:00