Share of source code between projects [VS2008, C ++]

How can I share the source code between two projects in MS Visual Studio 2008 (I am 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 code so that whenever I change / add code to one project, it will update in the second.

+2


source to share


3 answers


A common method for doing this (you'll see it all over the place in open source packages) is to merge all headers into an "include" folder and all source into a "source" folder.

Now, in whatever project the code requires, you go to "Project Properties -> c / C ++ -> General -> Additional Inclusion Directories". Then add the path to the "include" directory. Finally add the sources / headers to your project, now both projects are linking to the same files in a good common location.



You can also create shared code as a static library or better yet (IMO) DLL. It involves creating a new project and learning a little about the linker in VS 2008, but really nothing fancy. This also has the advantage (in the case of DLLs) that the two projects do not recompile the same code, but rather are compiled once and used twice.

+8


source


You can move the required classes to a separate library project and then reference that from the second project. Any changes will be automatically picked up.



(I am not a C ++ developer, but the above works for C # projects, I would guess it works for C ++ projects too)

+1


source


Basically you have two options:

  • Create a static library. In this case, all the code in the library will be exported and displayed to those who ever link to that library.
  • Create a DLL: Here you can define which classes and methods you want to export and use.

Suppose you have a class called classA, which is defined in classA.h and implemented in classA.cpp, and you want to use the same class from two different applications (App B and App C).

Using method 1, you will create a static library by going to file-> new win32 project and in the window that appears, select the application options and make it "Static Library". Then, in that static library, you add both classA.h and classA.cpp.

To use this static library in application B or C, go to the links and add a reference to the static library project you just created. then include classA.h in your application (remember to include the additional path to the include directories) and you're good to go.

This approach is very similar to DLLs, the difference is that you can choose which parts of your code in the DLL are exported (i.e. visible to external callers).

From a general point of view: When using static library code, your code will be compiled into both applications.

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

0


source







All Articles