简体   繁体   English

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

[英]How do I share C++ source code files between projects in Visual Studio?

I'm writing a cross-platform application. 我正在编写一个跨平台的应用程序。 One version will work under Win32 and the 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. 我想重用我的C ++核心-特别是里面没有平台依赖项,仅STL。 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). 我要这样做,我想在两个项目中使用相同的源文件:静态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 a Win32 application: 该库被编译为.DLL文件,然后导入Win32应用程序中:

MyApp.Win32App [win32 C#]

Since Win32 is not compatible with Windows Phone on the binary level, I cannot use that library directly. 由于Win32在二进制级别与Windows Phone不兼容,因此我无法直接使用该库。 But since the library uses only STL, I can create a Windows Phone component, put all its sources there, and build. 但是由于该库仅使用STL,所以我可以创建Windows Phone组件,将其所有源代码放在此处并进行构建。

MyApp.Component [Windows Phone component]
    myClass.cpp
    myClass.h

I want these two files to be exactly the same ones as used in the 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. 只需将common目录中的.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. 我确实注意到,如果您将网络驱动器用作通用源,VisualStudio会有些怪异和困难,所以我不这样做。 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. 但是,只要所有源代码都在本地磁盘上,并且IDE知道在哪里可以找到它们,那么在很多项目中使用相同的源文件就没有问题。

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. 如果在共享文件夹的.cpp文件中包含“ stdafx.h”,则将其注释掉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Visual C ++中的项目之间共享代码,或者在静态lib中为静态变量共享“未解析的外部符号” - How to share code between projects in Visual C++ or “unresolved external symbol” for static variables in static lib 如何在 Visual Studio 2019 中有条件地编译 c++ 源文件? - How do I conditionally compile c++ source files in Visual Studio 2019? 如何在 Visual Studio 中编辑 c++ 项目的源文件? - How do I edit the source files of a c++ project in Visual Studio? 如何在Visual Studio 2015中调试现有的C ++源代码? - How do I debug existing C++ source code in Visual Studio 2015? Visual Studio C ++项目管理。 如何处理项目中的非代码文件? - Visual Studio C++ project management. How do I handle non-code files in a project? 在两个Visual Studio 2010 C ++项目(DLL和Win32项目)中共享头文件 - Share header files within two Visual Studio 2010 C++ projects ( DLL and a Win32 project) 如何在C ++源文件之间共享静态变量? - How to share a static variable between C++ source files? 如何在c ++项目之间共享类? - how to share class between c++ projects? 如何在GitHub上正确设置Visual Studio C ++项目? - How do I properly set up Visual Studio C++ projects on GitHub? 如何使用extern在C ++中的源文件之间共享Globe变量? - How can I use extern to share globe variables between source files in C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM