将现有 C++ 源代码导入 Visual Studio
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4853550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Import Existing C++ Source Code into Visual Studio
提问by pinvoke
I am trying to import an existing c++ application's source into visual studio to take advantage of some specific MS tools. However, after searching online and playing with visual studio, I cannot seem to find an easy way to import existing c++ source code into visual studio and keep it structurally intact.
我正在尝试将现有的 C++ 应用程序的源代码导入到 Visual Studio 以利用某些特定的 MS 工具。但是,在网上搜索并使用 Visual Studio 后,我似乎找不到一种简单的方法将现有的 C++ 源代码导入到 Visual Studio 并保持其结构完整。
The import capacity I did find flattens out the directories and puts them all into one project. Am I missing something?
我发现的导入能力使目录变得扁平并将它们全部放入一个项目中。我错过了什么吗?
(This is all unmanaged C++, and contains specific builds for win/unix)
(这都是非托管 C++,并且包含针对 win/unix 的特定构建)
采纳答案by peterchen
I am not aware of any general solution under the constraints given - specifically having to create many projects from a source tree.
我不知道在给定的约束下有任何通用的解决方案 - 特别是必须从源树创建许多项目。
The best option I see is actually creating the project files by some script.
我看到的最佳选择实际上是通过一些脚本创建项目文件。
- Creating a single project manually (create empty project, then add the files),
- Configure it as close as possible as desired (i.e. with precompiled headers, build configurations, etc.)
- Use the .vcproj created as skeleton for the project files to be created
- 手动创建单个项目(创建空项目,然后添加文件),
- 根据需要尽可能地配置它(即使用预编译头、构建配置等)
- 使用创建的 .vcproj 作为要创建的项目文件的骨架
A very simple method would file list, project name etc. with "strange tokens", and fill them in with your generator. If you want to be the good guy, you can of course use some XML handling library.
一个非常简单的方法是使用“奇怪的标记”来文件列表、项目名称等,并用您的生成器填充它们。如果您想成为好人,您当然可以使用一些 XML 处理库。
Our experience:We actually don't store the .vcproj and .sln in the repository (git) anymore, but a python script that re-genrates them from the source tree, together with VS 2008 "property sheet templates" (or whatever they are called). This helps a lot making general adjustments.
我们的经验:我们实际上不再将 .vcproj 和 .sln 存储在存储库 (git) 中,而是从源树重新生成它们的 python 脚本,以及 VS 2008“属性表模板”(或任何它们叫做)。这有助于进行一般调整。
The project generation script contains information about all the projects specialties (e.g. do they use MFC/ATL, will it create DLL or an EXE, files to exclude).
项目生成脚本包含有关所有项目特性的信息(例如,它们是否使用 MFC/ATL,它会创建 DLL 还是 EXE,要排除的文件)。
In addition, this script also contains dependencies, which feeds the actual build script.
此外,此脚本还包含依赖项,用于提供实际的构建脚本。
This works quite well, the problems are minor: python requried in build systems, not forgetting to re-gen the project files, me having to learn some python to make adjustments to some projects.
这很有效,问题很小:构建系统中需要python,不要忘记重新生成项目文件,我必须学习一些python才能对某些项目进行调整。
@Michael Burr "How complex are the python scripts and whatever supporting 'templates' you might need?"
@Michael Burr “python 脚本和您可能需要的任何支持‘模板’的复杂程度如何?”
I honestly can't tell, since I gave the task to another dev (who picked python). The original task was to provide a build script, as the VS2008 solution build was not good enough for our needs, and the old batch file didn't support parallelization. .vcproj
generation was added later. As I understand his script generates the .vcproj and .sln files from scratch, but pulls in all the settings from separate property sheets.
老实说我不知道,因为我将任务交给了另一个开发人员(他选择了 python)。最初的任务是提供一个构建脚本,因为 VS2008 解决方案构建不足以满足我们的需求,并且旧的批处理文件不支持并行化。.vcproj
一代是后来添加的。据我了解,他的脚本从头开始生成 .vcproj 和 .sln 文件,但从单独的属性表中提取所有设置。
Pros:
优点:
Adding new configurations on the fly. Some of the projects already had six configurations, and planning for unicode support meant considering doubling them for a while. Some awkward tools still build as MBCS, so some libs do have 8 configs now. Configuring that from hand is a pain, now it just doesn't bother me anymore.
Global changes, e.g. moving around relative project paths, the folder for temp files and for final binaries until we found a solution we were happy with
Build Stability. Merging VC6 project files was a notable source of errors for various reasons, and VC9 project files didn't look better. Now things seem isolated better: compile/link settings in the property sheets, file handling in the script. Also, the script mostly lists variations from our default, ending up easier to read than a project file.
即时添加新配置。一些项目已经有六种配置,计划支持 unicode 意味着考虑将它们加倍一段时间。一些笨拙的工具仍然构建为 MBCS,因此一些库现在确实有 8 个配置。手动配置它很痛苦,现在它不再困扰我了。
全局更改,例如移动相对项目路径、临时文件和最终二进制文件的文件夹,直到我们找到满意的解决方案
建立稳定性。由于各种原因,合并 VC6 项目文件是一个显着的错误来源,并且 VC9 项目文件看起来并不好。现在事情似乎更好地隔离了:属性表中的编译/链接设置,脚本中的文件处理。此外,该脚本主要列出了我们默认的变体,最终比项目文件更容易阅读。
Generally: I don't see a big benefit when your projects are already set up, they are rather stable, and you don't have real issues. However, when moving into the unknown(for us: mostly VC6 -> VC9 and Unicode builds), the flexibility reduced the risk of experiments greatly.
一般来说:当你的项目已经建立时,我看不到什么大的好处,它们相当稳定,你没有真正的问题。然而,当进入未知时(对我们来说:主要是 VC6 -> VC9 和 Unicode 构建),灵活性大大降低了实验的风险。
回答by Zac
With no project/solution loaded, in Visual Studio 2005 I see this menu item:
在没有加载项目/解决方案的情况下,在 Visual Studio 2005 中我看到了这个菜单项:
File > New Project From Existing Code...
文件 > 从现有代码新建项目...
After following the wizard, my problem is solved!
按照向导操作后,我的问题解决了!
Switching the "Show All Files" button shows the complete hierarchy with all directories and files within.
切换“显示所有文件”按钮会显示包含所有目录和文件的完整层次结构。
If the New Project From Existing Code... option isn't available, you'll need to add it in Tools > Customize...
如果 New Project From Existing Code... 选项不可用,您需要将它添加到 Tools > Customize...
回答by John Dibling
Create a new empty solution and add your source code to it.
创建一个新的空解决方案并将您的源代码添加到其中。
For example,
例如,
File>New>Project... Visual C++>Win32>Win32 Console Application Application Settings> - Uncheck "Precompiled Header" - Check "Empty Project"
File>New>Project... Visual C++>Win32>Win32 Console Application Application Settings> - 取消选中“Precompiled Header” - 选中“Empty Project”
Project is then created. To add existing code:
然后创建项目。添加现有代码:
Project>Add Existing Item...> - Select file(s) to add
项目>添加现有项目...> - 选择要添加的文件
Recompile, done!
重新编译,完成!
回答by Michael Burr
In the "Solution Explorer" you can click on the "Show All Files" button to have Visual Studio display the files as they exist on the file system (directories and all).
在“解决方案资源管理器”中,您可以单击“显示所有文件”按钮,让 Visual Studio 显示文件系统(目录和所有)中存在的文件。
In my opinion this is an imperfect workaround, but I believe it's the best available. I'm unaware of a plug-in, macro or other tool that'll import a directory into an actual project with folders that mirror the file system's.
在我看来,这是一个不完美的解决方法,但我相信它是最好的。我不知道将目录导入实际项目的插件、宏或其他工具,其中包含镜像文件系统的文件夹。
回答by Brian
I know this question is already marked correct, but I was able to import existing code into a project with Visual Studio 2008 by doing "File" -> "New Project from existing code". The directory structure of my code was retained.
我知道这个问题已经标记为正确,但我能够通过执行“文件”->“从现有代码新建项目”将现有代码导入到带有 Visual Studio 2008 的项目中。我的代码的目录结构被保留了下来。
回答by Ronald Jacob
You can always switch view from project menu
您可以随时从项目菜单切换视图
For eg. Project->Show All Files
例如。项目->显示所有文件
The above will display the files in unformated raw file system order
以上将以未格式化的原始文件系统顺序显示文件
Not sure of older versions but it works on VS 2010
不确定旧版本,但它适用于 VS 2010
回答by ISTB
I understand you, I have the same problem: many .cpp and .h files organized in many folders and subfolders with include paths written for this folder structure. The only way you can do to import this folder structure together with the source files is to use "Show All Files" and then right-click on folders and select "Import in Project". This works for me when I am using C-Sharp projects. But it does not work for my C++ Projects. I am still searching for a solution...
我理解你,我有同样的问题:许多 .cpp 和 .h 文件组织在许多文件夹和子文件夹中,包含为此文件夹结构编写的路径。将此文件夹结构与源文件一起导入的唯一方法是使用“显示所有文件”,然后右键单击文件夹并选择“在项目中导入”。当我使用 C-Sharp 项目时,这对我有用。但它不适用于我的 C++ 项目。我仍在寻找解决方案...