简体   繁体   中英

Visual Studio (C++)- what is the best practice regarding directories configurations?

(I'm using VS 2010 but most of the info is relevant at least down to VS 2003, perhaps with slight differences in the organization/layout of the build configuration menus\\GUI)

When configuring a project build, there is a section named "VC++ Directories" that contains 6 labels. 2 of them are:

  1. Library Directories
  2. Include Directories

In addition, if you go to 'C/C++' -> 'Additional Include Directories' , you can specify additional directories, that AFAIK (from the descriptions of these directories in MSDN and VS help) is identical to 'Include Directories' (though there is probably some search order between them). Likewise, if you go to 'Linker' -> 'Additional Library Directories' you can specify additional paths for libraries to link with the project (here the description is more precise- "Allows the user to override the environment library path", so these paths are searched sooner).

My question is-

is there a reason to use one (of the paths) over the other? what is the best practice?

Please relate in your answer to using the property pages features (which adds flexibility to the configuration of different projects and allows to easily reuse existing ones but are causing me more confusing regarding the best practice here). Thanks in advance.

Let's consider first only include paths.

The Microsoft documentation states that the compiler searches for directories in the following order:

  1. Directories containing the source file.

  2. Directories specified with the /I option, in the order that CL encounters them.

  3. Directories specified in the INCLUDE environment variable.

Now, the ["VC++ Directories" → "Include directories"] is documented as corresponding to the INCLUDE variable. Ie, these directories are searched last. According to the documentation.

And the ["C/C++" → "General" → "Additional Include Directories"] is documented as corresponding the /I option. Ie, these directories are searched first. According to the documentation.

Insofar as any best practice exists, it probably is

  • to leave open the possibility of overriding includes, and

  • to minimize the compiler invocation command line length (so as not to stress poor Windows – as I recall there was/is an 8 KB limit, or thereabouts).

Ie, use ["VC++ Directories" → "Include directories"] by default.


The complete set of environment variable correspondences:

  • ["VC++ Directories" → "Executable directories"] → PATH

  • ["VC++ Directories" → "Include directories"] → INCLUDE

  • ["VC++ Directories" → "Reference directories"] → LIBPATH (for #using )

  • ["VC++ Directories" → "Library directories"] → LIB


How did I find out this?

Simply by clicking in the GUI and pressing F1 for help. :-)

It's always a good idea to RTFM.

By default, Visual Studio puts following paths in its INCLUDE variable ( VC++ Directories -> Include directories ):

  • path to Microsoft's Visual C++ headers: $(VCInstallDir)include
  • path to MFC headers (for MFC projects): $(VCInstallDir)atlmfc\\include
  • path to Windows SDK headers: $(WindowsSdkDir)include;$(FrameworkSDKDir)\\include

They are preconfigured and just leave them where they are. If your project depends on some additional components/frameworks, add paths to their headers to C/C++ -> General -> Additional Include Directories ( /I compiler switch). Use angle brackets with your #include statements in this case.

The same is for libraries - leave Visual Studio defaults and paths to libraries from additional components/frameworks add to Linker -> Additional Library Directories .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM