Skip to main content
GameDev.net gamedev.net
🔒 Locked

Shared Source Files Between Projects in VS2003?

Started by Telamon Jul 12, 2004 at 9:06 AM 8 replies 600+ views
Original Post
Telamon
Telamon
Hi. I recognize this is a pretty basic question, but it's the kind of thing that is really hard to look up, so if anyone could help me out, that'd be really great. I am writing a multiplayer client/server game in C#, using the VS2003 IDE. All the code in my project belongs in one of three namespaces: StrategoClient, StrategoServer, and StrategoCommon. The code in the StrategoCommon namespace is basically a bunch of static constants and file IO code that I need for both projects. What I'd really like is to have three different folders/tree branches in my solution file - one for the client, one for the server, and one for the common code. Is there anyway to do this? For a similar exercise I put all the common code in the client project and made little shortcuts in the server project to point to the common files in the client project. But this was really ugly. For something as simple as this, I know there has to be a more "professional" way to set this up. Anyone?
VolkerG
VolkerG
Why not create a sperate assembly that contains all the shared classes? Then the server and the client use this dll.
Just add a new project (Class Library) to the solution and move all the shared code into it.
mnansgar
mnansgar
Here's what I recommend:

+ Make a Solution, then make each code branch as a separate folder within that Solution folder.
+ Make a separate folder called 'Common' or something similar that will hold all of your includes and such common to all three projects.

And now there are many different methods of approaching this:
1. Inside your normal code, when including something from the common project, #include "../Common/MyHeader.h"
2. Add your Common directory to the compiler's global directory paths (Tools->Options->Project->Directories) then you can just reference the headers as #include .
3. If this common code also includes source code as well as header files, then many times a library file will be built and placed in, for example, a /Lib folder. After doing this, each of your three projects can include the library functions merely by #include "../Common/LibHeader.h" and by #pragma comment (lib, "MyLib.lib"); or by setting it up for inclusion within the project settings.
h20, member of WFG 0 A.D.
Telamon
Telamon
Yep. If I were using C++ that's how I would do it. Since it is so easy to do in C++, using the same IDE, I am dumbstruck that the only way to do it in C# is to compile it to a dll or class library... which seems like more effort than should be required.
VolkerG
VolkerG
Quote:
Original post by Telamon
Yep. If I were using C++ that's how I would do it. Since it is so easy to do in C++, using the same IDE, I am dumbstruck that the only way to do it in C# is to compile it to a dll or class library... which seems like more effort than should be required.

It's not more work than creating a library in C++ (and you don't even need a header file), or am I missing something?
Telamon
Telamon
VolkerG: Yeah. If this was C++, I could just change the project settings of the client and server to also look in the common folder for source code. There's no need to make a library.

Whereas the project settings for C# projects don't give that option.
Arild Fines
Arild Fines
Ever seen what the DirectX SDK C# sample apps do? They create links to a bunch of files in a shared folder.

You can do this by right clicking on the project, choosing Add->Existing Item, locating the item in question *AND* clicking the little arrow next to the Open button and choosing "Link to file..." from the dropdown. Or was this what you meant by "creating little shortcuts"?

I have a feeling the DX SDK samples do this for the educational value of having all the source code in the solution, though. Extracting shared functionality into an assembly referenced by both the server and the client is the *correct* way of doing it.

You have to keep in mind that if you compile the same class into two assemblies, they are *NOT* the same type. Even if they are binary identical, the type identity is closely linked to the assembly identity, which, if you compile the same class into two different assemblies, is not the same. The consequence is that you cannot instantiate Foo from the client and pass it to a method in the server that expects a Foo.
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Telamon
Telamon
Hey thanks Arild, that was pretty helpful. I'm not looking to send common objects back and forth between the two projects - I'm really just trying to have a GlobalConstants.h file, like I might in a C++ program. Maybe the right way to do it would be to make a class library, but my multiuser stratego server is proving to be a real learning experience for me, so anyway I can structure my code to be easier to change later (after I mess it up) is all to the good.
VolkerG
VolkerG
Quote:
Original post by Telamon
VolkerG: Yeah. If this was C++, I could just change the project settings of the client and server to also look in the common folder for source code. There's no need to make a library.

Whereas the project settings for C# projects don't give that option.

Ah, now I understand! Took some time (it was late) ;)
So you don't actually want to share code, but only constants. But as far as I know, creating a class library is the price you have to pay for using .NET. But think positive, now you can use the constants even from a VB.NET project!
markr
markr
One perhaps not-so-obvious strategy which could equally be used in C++ and C#, is to make the client and server the same executable.

Then you can reuse whatever code you like without having to create separate projects.

Just pass some command-line parameters to the program to make it run as a dedicated server.

Mark

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.