How to share C++ source code files between projects in Visual Studio?

I'm writing a cross-platform application. One version will work under Win32, second - on Windows Phone.

I'd like to reuse my C++ core - especially that there are no platform dependencies inside, STL only. I order to do so, I want to use the same source files in two projects: static Win32 library (.lib) and Windows Phone Component (C++/CLI).

How can I configure these two projects to use exactly the same source and header files?


Ok, let's take an example. Let's say, that I have project:

MyApp.Library [win32 lib]
    myClass.cpp
    myClass.h

This library is compiled to .DLL file and then imported in Win32 application:

MyApp.Win32App [win32 C#]

Since win32 is not compatible with windows phone on the binary level, I cannot use that lib directly. But since Library uses only STL, I can create Windows Phone Component, put all its sources there and build.

MyApp.Component [WinPhone component]
    myClass.cpp
    myClass.h

I want these two files to be exactly the same ones as used in Library. How should I organize the project to achieve this effect?


You can add source code from a common location to multiple projects. I do that a lot; I have code in a common directory that is at the same level in the directory hierarchy as most of my project files. It's simply a matter of adding .h and .cpp files from the common directory to your various projects.

I did notice that VisualStudio gets a little cranky and difficult if you use a network drive for common source, so I don't do that. But so long as all of your source code is on local disks, and the IDE knows where to find them, there is no problem having the same source file in very many projects.


one way is to put all .hpp and .cpp files into a seperate folder - call it "Shared".

Then add an additional include Directory inside your solution configuration property - this directory must be a relative path.

Then add ONLY the .cpp files relative from that shared folder into your project

NB! If you have include "stdafx.h" inside your .cpp files in the Shared folder, then comment those out.

链接地址: http://www.djcxy.com/p/81744.html

上一篇: Ray采用gluUnProject()

下一篇: 如何在Visual Studio中的项目之间共享C ++源代码文件?