如何在Visual Studio中的项目之间共享c++源代码文件?

时间:2022-03-19 15:08:45

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

我正在编写一个跨平台的应用程序。一个版本将在Win32下运行,在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).

我想重用我的c++核心——特别是里面没有平台依赖,只有STL。为此,我希望在两个项目中使用相同的源文件:静态Win32库(.lib)和Windows Phone组件(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:

本库编译为.DLL文件,然后导入Win32应用程序:

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.

由于win32在二进制级别上与windows phone不兼容,所以我不能直接使用这个库。但是由于库只使用STL,所以我可以创建Windows Phone组件,将它的所有源代码放在那里并构建。

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?

我希望这两个文件与库中使用的文件完全相同。我应该如何组织这个项目来达到这个效果?

2 个解决方案

#1


5  

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.

您可以从公共位置向多个项目添加源代码。我经常这样做;我在一个公共目录中有代码,该目录层次结构与我的大多数项目文件相同。只需将.h和.cpp文件从公共目录添加到不同的项目中。

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.

我确实注意到VisualStudio有点古怪,如果您使用网络驱动器作为公共源,那么会比较困难,所以我不这么做。但是,只要您的所有源代码都在本地磁盘上,并且IDE知道在哪里可以找到它们,那么在许多项目中拥有相同的源文件是没有问题的。

#2


0  

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

一种方法是将所有的.hpp和.cpp文件放到一个分开的文件夹中——称之为“共享”。

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

然后只将共享文件夹中的.cpp文件添加到项目中

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

NB !如果你包含了“stdafx”。h“在共享文件夹中的.cpp文件中,然后注释掉它们。

#1


5  

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.

您可以从公共位置向多个项目添加源代码。我经常这样做;我在一个公共目录中有代码,该目录层次结构与我的大多数项目文件相同。只需将.h和.cpp文件从公共目录添加到不同的项目中。

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.

我确实注意到VisualStudio有点古怪,如果您使用网络驱动器作为公共源,那么会比较困难,所以我不这么做。但是,只要您的所有源代码都在本地磁盘上,并且IDE知道在哪里可以找到它们,那么在许多项目中拥有相同的源文件是没有问题的。

#2


0  

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

一种方法是将所有的.hpp和.cpp文件放到一个分开的文件夹中——称之为“共享”。

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

然后只将共享文件夹中的.cpp文件添加到项目中

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

NB !如果你包含了“stdafx”。h“在共享文件夹中的.cpp文件中,然后注释掉它们。