tags:

views:

346

answers:

3

How can I share source code between 2 projects in MS Visual Studio 2008 (I'm programming in C++)?
In my case, I have the code for the main game project and now I want to make a simple model editor that uses the game's code, so that whenever I change/add some code in one project it will be updated in the second one.

+1  A: 

You can move the required classes into a separate library project and then references this from the second project. Any changes will be automatically picked up.

(I'm not a C++ developer, however the above works for C# projects, I would assume it works for C++ projects too)

Jaimal Chohan
+1  A: 

A common method for doing this, (you'll see it everywhere in open-source packages), is to bundle all the headers into an 'include' folder and all the source into a 'source' folder.

Now in whatever project needs the code, you go to, 'Project Properties->c/c++->General->Additional Include Directories'. Then add the path to the 'include' directory. Finally, add the source/headers to your project, now both projects reference the exact same files, which are in a nice tidy shared location.

You can also build the shared code as a static library or better yet (IMO) a DLL. This involves creating a new project, and learning a little bit about the linker in VS 2008, but really nothing too complicated. This also has the advantage (in the case of a DLL) that the two projects don't re-compile the same code, but rather it is compiled once and used twice.

DeusAduro
+1 for dynamic linking, fits the "whenever I change/add some code in one project it will be updated in the second one" requirement
Graphics Noob
A: 

You basically have two options:

  1. Create a static library. In this, all the code in the library will be exported and visible to who ever links to this library.
  2. Create a DLL: here, you can define what classes and methods you would like to export and use those.

Lets say you have a class called classA which is defined in classA.h and implemented in classA.cpp and you want to use this same class from two different applications (application B and application C).

Using method 1, you would create a static library by going to file->new win32 project and in the box that pops up, choose application settings and make it a "Static Library". Then in this static library you add both your classA.h and classA.cpp.

To use this static library in application B or C, goto the references and add a reference to the static library project that you just created. then include classA.h in your application (don't forget to set the additional include directories path) and you are good to go.

The approach is very similar for a DLL as well, the difference here would be that you can choose what parts of your code in the DLL are exported (ie visible to outside callers).

From an overall point of view: With the static library approach, your code will be compiled into both the applications.

With the DLL approach, there will be just one copy of the shared code (in the DLL which will be a separate file) and this will be loaded as required.

obelix