Visual C++: Files and Directories

Visual C++ does not respect (or use) the underlying directory structure of your existing source code files. Some may find this to be a failing, but I personally think this is a feature. It allows you to organize your files in the way you want into one (or more) projects inside Solution Explorer.

Visual C++ Files

If a simple Visual C++ solution named Foobar is created with source files named Main.cpp and Main.h, it will have the following structure:

Foobar
|   Foobar.sln
|
+---Foobar
        Foobar.vcxproj
        Main.cpp
        Main.h

Foobar.sln is the Visual C++ solution file and Foobar.vcxproj is the Visual C++ project file. A solution can contain one or more projects. By default, when a solution is created, one project is created in the same name as that of the solution.

If another project named Foobar2 is added to the Foobar solution, the directory structure would look like this:

Foobar
|   Foobar.sln
|
+---Foobar
|       Foobar.vcxproj
|       Main.cpp
|
+---Foobar2
        Foobar2.vcxproj
        Main2.cpp

The solution file (Foobar.sln) and the project files (Foobar.vcxproj and Foobar2.vcxproj) are the only non-source files that are needed. Visual C++ might create a few more files and directories, all of them are not essential. So, if you plan to use a version control system (like Subversion or Mercurial) on this solution, you only need to backup .sln, .vcxproj, .cpp and .h files. For the comprehensive list of Visual C++ files that need to be checkedin to version control see this post.

Directory Structure

You can setup a convenient directory structure inside the directory that contains Main.cpp and Main.h. For example, creating a directory Foo with source files Foo.cpp and Foo.h:

Foobar
|   Foobar.sln
|
+---Foobar
    |   Foobar.vcxproj
    |   Main.cpp
    |   Main.h
    |
    +---Foo
            Foo.cpp
            Foo.h

No matter what is the directory structure, the source files can be imported into the Foobar Visual C++ solution and arranged in any fashion the user wants. The root directory of the Foobar project is Foobar/Foobar.

Including Directories

Header files at the root directory of the project can be directly included:

// Main.cpp
#include "Main.h"

For the header files lying inside directories of the project root directory, there are 2 ways to include them:

  1. Include the header file providing the relative path from the source file to the header:
    // Main.cpp
    #include "Main.h"
    #include "Foo/Foo.h"
    
  2. Open Project → Properties. Choose C/C++ → Additional Include Directories and add Foo. To add more include directories to the project, separate them with semicolons. The header files in the Foo directory can now be included without specifying any path in any .cpp or .h file in the project:
    // Main.cpp
    #include "Main.h"
    #include "Foo.h"
    

As you might deduce, strategy 1 gets messy pretty quickly since each file that includes another header needs to know the path that leads to it. For example, to include Main.h in Foo.cpp:

// Foo.cpp
#include "../Main.h"

Compared to this, using strategy 2 one can include any header file in any source file without worrying about the directory where it lies. This is much easier to apply across the directory structure of a project.

About these ads

2 thoughts on “Visual C++: Files and Directories

  1. and what about resource files and vcxproj.filters files. don’t you need to add them in your version control system ?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s