VC2010 C ++-组织源文件 [英] VC2010 C++ - organizing source files

查看:74
本文介绍了VC2010 C ++-组织源文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对如何在VC 2010 C ++项目中组织源文件有一些疑问.最终所包含的默认过滤器将不够用,因为我将拥有大量的.cpp和.hpp文件,因此会变得凌乱.我想在项目的根目录下创建一个名为源"的文件夹,然后在源"内为我要使用的各种源文件类别创建文件夹.右键单击解决方案时,我只能选择添加过滤器,而不是文件夹.我可以在Windows资源管理器中手动创建文件夹,然后将其包含在内,但是随后我就失去了添加过滤器的能力.有没有办法添加新文件夹(不使用Windows资源管理器)并且仍然使用过滤器?

I had some questions about how to organize source files in a VC 2010 C++ project. The default filters that are included won't be sufficient as ultimately, I'm going to have a lot of .cpp and .hpp files so it's going ot be messy. I'd like to create a folder at the root of the project called "source" then create folders inside "source" for the various source file categories I'd like to use. When I right click the solution, I only get the option to add a filter, not a folder. I can create folders manually in windows explorer, then include them, but Then I lose my ability to add filters. Is there a way to add new folders (without using windows explorer) and still use filters?

此外,是否有一些类似于$(CURDIR)的东西可以用来包含源文件文件夹,而无需绝对路径?

Additionally, is there something similar to $(CURDIR) that I could use to include the source file folder without needing an absolute path?

预先感谢您的帮助

推荐答案

您对Visual Studio如何使用C ++文件感到有些困惑,但这没关系,因为文档太糟糕了.首先,第一件事:与C#或Java不同,项目中文件的排列与磁盘上文件的排列无关.文件和文件夹实际上不在项目中.该项目仅列出磁盘上必须已经存在的文件和文件夹的名称和路径.

You're a little confused about how Visual Studio works with C++ files, but that's OK, since the documentation is lousy. First things first: unlike with C# or Java, the arrangement of files in the project has no relation to the arrangement of files on your disk. Files and folders aren't "in" the project, really; the project only lists the names and paths of the files and folders, which have to already exist on your disk.

通常,您创建文件夹的布局,并在这些文件夹中包含源文件.您可以在Visual Studio的外部中进行此操作.您还将创建一个项目文件.当您向项目添加文件"时,您要做的就是告诉项目在磁盘上找到文件的位置.

In general, you create the layout of folders, with source files inside of those folders. You do this outside of Visual Studio. You also create a project file. When you "add a file" to the project, all you do is tell the project where to find the file on disk.

我们来看一个具体的例子,我将向您展示如何组织它.假设您正在制作一个名为 SuperCalc 的图形计算器程序.您将拥有一个Source文件夹,然后在Source内创建文件夹以容纳不同的文件.假设您创建的文件夹是:

Let's work with a specific example and I'll show you how I would organize it. Suppose you are making a graphing calculator program called SuperCalc. You'll have a Source folder, and then create folders inside of Source to hold the different files. Suppose the folders you create are:

  • SuperCalc\Source\Input
  • SuperCalc\Source\Math
  • SuperCalc\Source\Math\Matrix
  • SuperCalc\Source\Output
  • SuperCalc\Source\Input
  • SuperCalc\Source\Math
  • SuperCalc\Source\Math\Matrix
  • SuperCalc\Source\Output

您有Source的3个子目录:InputOutputMath. Math子目录有一个名为Matrix的子目录.您将在Input,Math,Matrix和Output中拥有.cpp文件.您将使用Windows资源管理器或命令提示符创建这些目录(文件夹).

You have 3 subdirectories of Source: Input, Output, and Math. The Math subdirectory has a subdirectory called Matrix. You'll have .cpp files in Input, Math, Matrix, and Output. You'll create these directories (folders) using Windows Explorer or the Command Prompt.

现在,您还需要创建一个解决方案文件(SuperCalc.sln)和一个项目文件(SuperCalc.vcxproj& SuperCalc.vcxproj.filters).在Visual Studio中执行此操作.通常,项目文件位于解决方案目录的子文件夹中.这将由Visual Studio自动为您完成.选择一个位置 解决方案文件-它应与源代码位于同一目录结构(文件夹树)中.我建议将其放在Source目录的旁边,

Now you will also want to create a solution file (SuperCalc.sln) and a project file (SuperCalc.vcxproj & SuperCalc.vcxproj.filters). Do that inside of Visual Studio. Usually the project file lives in a sub-folder of the solution directory; this will be done automatically for you by Visual Studio. Pick a location for the solution file -- it should be somewhere in the same directory structure (folder tree) as the source code. I would suggest putting it next to the Source directory, in:

  • SuperCalc\Build

进入VS并选择File > New > Project > Visual Studio Solutions > Blank Solution File.为解决方案指定一个名称(也许是"SuperCalc")和一个位置(我们刚刚在SuperCalc\Build目录中选择的位置).它将为您创建解决方案文件.

Go into VS and pick File > New > Project > Visual Studio Solutions > Blank Solution File. Give the solution a name (maybe "SuperCalc") and a location (the location we just picked in the SuperCalc\Build directory). It will create the solution file for you.

现在,在解决方案资源管理器中右键单击解决方案(解决方案SuperCalc,0个项目"),然后选择Add > New Project.选择一个名称-这将是您的程序可执行文件的名称,例如"SuperCalc"!选择 Win32 ,或者是 Win32控制台应用程序(这是一个DOS控制台程序),或者是 Win32 Project (这是一个普通的Windows GUI程序).通常,然后我单击应用程序设置"进行一些重要更改:一方面,我选择空项目",因为除非我告知,否则我不希望Visual Studio为我创建文件和代码.当所有设置完成后,我单击完成".

Now right-click on the solution in the Solution Explorer ("Solution SuperCalc, 0 projects"), and pick Add > New Project. Pick a name -- this will be the name of your program executable, like "SuperCalc"! Choose Win32, either Win32 Console Application (this is a DOS-console program) or Win32 Project (this is an ordinary Windows GUI program). Usually I then click on Application Settings to make some important changes: for one thing, I pick Empty Project because I don't want Visual Studio creating files and code for me unless I tell it to. When it's all set up the way I want it, I click FINISH.

现在,您已经拥有由Visual Studio创建的解决方案文件和项目.您还可以在Visual Studio之外创建源代码,或至少具有将在其中创建源代码的目录结构(文件夹树).现在是将这两件事联系在一起的时候了.

Now you've got your solution file and project, created by Visual Studio. You also have your source code, or at least the directory structure (folder tree) where your source code will be, created outside of Visual Studio. It's time to connect the two things together.

如果需要,可以将所有源文件列出到项目的源文件"过滤器中.即使它们来自不同的目录(Input,Matrix等),磁盘上文件的位置与其在项目中的外观之间也不存在必需的关系.但是,如果您有很多文件,则创建子过滤器",在源文件"过滤器内部进行过滤并为它们指定源"子文件夹的名称会更容易.这样,您就可以在项目文件中复制磁盘目录的结构.

If you wanted, you could list all your source files into the Source Files filter of your project. Even though they will come from different directories (Input, Matrix, etc.), there's no required relationship between the locations of the files on disk and their appearance in the project. But if you have a lot of files, it is easier if you create "sub-filters", filters inside the Source Files filter, and give them the names of the sub-folders of Source. That way, you replicate the structure of your disk directories inside of your project file.

右键单击"SuperCalc"项目的源文件"过滤器,然后选择添加">添加新过滤器".将其命名为Input(SuperCalc \ Source目录的第一个).同时创建过滤器MathOutput.右键单击Math过滤器,然后选择添加">添加新过滤器",以创建一个名为Matrix的子过滤器(在Math的内部).现在,您有了以下过滤器:

Right-click on the Source Files filter of the "SuperCalc" project, and pick Add > Add New Filter. Give it the name Input (the first of the SuperCalc\Source directories). Also create the filters Math and Output. Right-click on the Math filter and pick Add > Add New Filter, to create a sub-filter called Matrix (inside of Math). Now you have these filters:


   SuperCalc
      Source Files
         Input
         Math
            Matrix
         Output

与您先前创建的目录(文件夹)平行. 这纯粹是对人类的方便安排. Visual Studio对此不了解任何特殊之处.如果仅告诉VS添加文件",则不会将文件放入正确的过滤器中.您必须告诉它放在哪里.

which parallels the directories (folders) you created earlier. This is purely a convenient arrangement for humans. Visual Studio doesn't understand anything special about it. If you just tell VS to "add a file" it won't put the file in the correct filter. You'll have to tell it where to put it.

要添加或创建.cpp文件,请选择与.cpp文件所在目录相对应的过滤器名称.因此,要添加或创建文件SuperCalc\Source\Math\Matrix\matrix_multiply.cpp,请在解决方案资源管理器中右键单击Matrix过滤器,然后选择添加">添加新文件"或添加现有文件". (添加现有文件"适用于已经编写了matrix_multiply.cpp并且只想告诉项目在哪里的情况.)使用对话框导航至Source\Math\Matrix目录.对整个程序中的所有源文件重复此过程.

To add or create your .cpp files, select the filter name corresponding to the directory where the .cpp file is. So, to add or create a file SuperCalc\Source\Math\Matrix\matrix_multiply.cpp, right-click on the Matrix filter in Solution Explorer, and pick Add > Add New File or Add Existing File. (Add Existing File is for when you have already written matrix_multiply.cpp and you just want to tell the project where it is.) Use the dialog box to navigate to the Source\Math\Matrix directory. Repeat this process for all the source files in your whole program.

您还存在一个问题:是否有类似于$(CURDIR)的东西可以用来包含源文件文件夹而无需绝对路径?"真幸运:Visual Studio项目不使用绝对路径!他们使用相对路径.项目文件存储从包含.vcxproj文件的目录到包含源文件的目录所需的相对路径.因此,如果在我建议的地方(SuperCalc\Build目录)创建了SuperCalc.slnSuperCalc.vcxproj,并且在源子目录中添加了.cpp文件,则可以使用记事本在SuperCalc.vcxproj文件中查找,并且会看到像这样的行:

You also had the question "is there something similar to $(CURDIR) that I could use to include the source file folder without needing an absolute path?" You are in luck: Visual Studio projects don't use absolute paths! They use relative paths. The project file stores the relative path required to get from the directory containing the .vcxproj file to the directory containing the source file. So if you created SuperCalc.sln and SuperCalc.vcxproj where I suggested (the SuperCalc\Build directory), and you add your .cpp files in the source subdirectories, you can go look inside the SuperCalc.vcxproj file using Notepad, and you'll see lines like:


<ClCompile Include="..\..\..\Source\Math\Matrix\matrix_multiply.cpp" />

由于没有绝对路径,因此您可以将整个SuperCalc目录树移到其他位置,并且仍然可以使用.不需要$(CURDIR)之类的环境变量.

Since there are no absolute paths, you could take the entire SuperCalc directory tree and move it somewhere else, and it would all still work. No need for environment variable hacks like $(CURDIR).

要了解的最后一件事:由于将源文件放在多个目录中,因此#include ing标头(.h或.hpp文件)可能会遇到问题.您必须告诉编译器头文件所在的目录.它们可能会分散在多个目录中.因此,编辑项目设置:在解决方案资源管理器中右键单击项目名称,选择属性",然后向下钻取到配置属性">"C/C ++">常规".属性表中的第一个字段为其他包含目录". 执行其他操作之前,请单击配置"下拉菜单,然后选择"所有配置" .如果您同时具有32位和64位版本,请单击平台"下拉菜单,然后选择所有平台.现在转到其他包含目录",并将所有路径添加到所有源目录,并指定相对于项目文件目录的路径.因此,对于SuperCalc示例,它看起来像:

One final thing to know: since you are putting your source files in multiple directories, you might have problems with #includeing headers, the .h or .hpp files. You must tell the compiler the directories where your header files are located. They will probably be scattered among multiple directories. So edit the project settings: right-click on the project name in Solution Explorer, choose Properties, and drill down to Configuration Properties > C/C++ > General. The first field in the property sheet says "Additional Include Directories". Before you do anything else, click on the Configuration drop-down menu and choose All Configurations. If you have both a 32-bit and 64-bit build, click on the Platform drop-down menu and choose All Platforms. Now go to the "Additional Include Directories" and add all the paths to all the source directories, with the paths specified relative to the project file's directory. So for the SuperCalc example, it would look like:


..\..\..\Source\Input;..\..\..\Source\Math;..\..\..\Source\Math\Matrix;..\..\..\Source\Output

进行此更改后,Source \ Math \ Matrix \ matrix_multiply.cpp之类的文件可以具有 线

Once this change is made, a file like Source\Math\Matrix\matrix_multiply.cpp can have a line

#include "input_configuration.hpp"

从输入目录中#include一个文件,它将全部正常工作.

to #include a file from the Input directory, and it will All Just Work.

(如果并非所有方法都可行,通常的方法是返回项目属性"并在其他包含目录"前面摆弄..\序列的数量.记住每次进行更改时,都必须再次选择所有配置,否则所做的更改将仅适用于当前配置(调试或发布). >.)

(If it doesn't All Just Work, the usual approach is to go back into Project Properties and fiddle with the number of ..\ sequences in front of your Additional Include Directories. Remember that every time you go to make a change you must choose All Configurations again -- otherwise your changes will only apply to the current Configuration (Debug or Release). That setting is not sticky.)

这篇关于VC2010 C ++-组织源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆