开发者

Visual Studio 2010 - files in folders without ../../Include.h

开发者 https://www.devze.com 2023-01-11 10:35 出处:网络
My physical file structure for a project I have is something like: Source folder Engine Folder1 Folder2 etc.

My physical file structure for a project I have is something like:

  • Source folder
  • Engine
    • Folder1
    • Folder2

etc.

I have some files in 'Source', some in 'Engine', some in 'Engine/Folder1', etc.

On my project, I have gone All Configurations->Source Directories and included Source, Engine, Engine/Folder2, etc. However, I still get errors that it cannot find files when I try to include "Foo.h" or whatever from a different folder. Is there a way to make it so I don't have to have ../Folder1/ in front of 开发者_如何学Goeverything?


Is there a way to make it so I don't have to have ../Folder1/ in front of everything?

Yes, there is. The answer depends on several factors and I'm sure I'll miss a few.

Check the following:

  • In compiler settings check "Additional Includes" under "C/C++"
  • Also check in "VC++" the value for "Include Directories"
  • Check the setting for "Ignore Standard Include Paths" in "C/C++/Preprocessor"
  • Check your precompiled header settings
  • Check your "#define" / "#undef" in your source files and the compiler settings
  • Check the property sheets your project may be using or inheriting

If you use "foo.h" (rather than <foo.h>) the preprocessor will look in your project specific folders first and in the IDE specific folders last. If you use <foo.h> it starts in the standard include folders first, e.g. those that are required for standard runtime libraries.

When a file uses "../foo.h" its a path relative to the location of the file that includes the file. There can be tricky exceptions.

There are many more things that can influence how the preprocessor finds its include paths. If you are unsure what the preprocessor is doing with a particular file you can make the preprocessor output visible by switch on "Preprocess to a file" in the preprocessor settings. The file shows you the source code for a file after the preprocessor is finished and before the compiler starts its work.

The whole thing becomes much easier with more experience and in particular a clear strategy for the folder/project structure and how to include files. For example make sure you have "#pragma once" as the first non-comment line in each include file.

I hope this gives you a few ideas for the next steps. Good luck!


I consider this to be a good practice:

  • When the included file path doesn't require the use of "..", use relative paths
  • When it requires the use of "..", use absolute paths (that is, relative to the root folder of your source files.

For absolute paths to work, add the root source folder to the include directories list (relative to the project's file location).

0

精彩评论

暂无评论...
验证码 换一张
取 消