Visual Studio (C++) - 关于目录配置的最佳实践是什么?
(我使用的是 VS 2010,但大多数信息至少与 VS 2003 相关,可能在构建配置菜单 \GUI 的组织/布局方面略有不同)
配置项目构建时,有一个名为“VC++ 目录” 的部分,其中包含 6 个标签。其中 2 个是:
- 库目录
- 包含目录
此外,如果您转到 'C/C++' -> “附加包含目录”,您可以指定附加目录,据我所知(来自 MSDN 和 VS 帮助中这些目录的描述)与“包含目录”相同(尽管它们之间可能存在一些搜索顺序)。同样,如果您转到'Linker' -> “附加库目录”您可以指定与项目链接的库的附加路径(这里的描述更精确 - “允许用户覆盖环境库路径”,因此可以更快地搜索这些路径)。
我的问题是 -
是否有理由使用一个(路径)而不是另一个?最佳做法是什么?
请在您的答案中涉及使用属性页功能(这增加了不同项目配置的灵活性,并允许轻松重用现有项目,但使我对这里的最佳实践更加困惑)。 提前致谢。
(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:
- Library Directories
- 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让我们首先考虑仅包含路径。
Microsoft 文档指出编译器按以下顺序搜索目录:
包含源文件的目录。
使用
/I
选项指定的目录,按照 CL 遇到它们的顺序。在
INCLUDE
环境变量中指定的目录。现在,[“VC++ 目录”→“包含目录”] 被记录为与
INCLUDE
变量相对应。即,最后搜索这些目录。根据文档。并且[“C/C++”→“常规”→“附加包含目录”]被记录为对应于
/I
选项。即,首先搜索这些目录。根据文档。只要存在任何最佳实践,它可能是
保留覆盖包含的可能性,并且
最小化编译器调用命令行长度(以免给可怜的 Windows 带来压力 - 我记得曾经有 8 KB 限制,或大约如此)。
即,默认使用[“VC++目录”→“包含目录”]。
完整的环境变量对应关系:
[“VC++目录”→“可执行目录”]→
PATH
[“VC++ 目录”→“包含目录”] →
INCLUDE< /代码>
[“VC++ 目录”→“引用目录”] →
LIBPATH< /code>
(对于
#using
)[“VC++ 目录”→“库目录"] →
LIB
我是怎么找到这个的?
只需单击 GUI 并按 F1 即可获得帮助。 :-)
对于 RTFM 来说这始终是个好主意。
Let's consider first only include paths.
The Microsoft documentation states that the compiler searches for directories in the following order:
Directories containing the source file.
Directories specified with the
/I
option, in the order that CL encounters them.Directories specified in the
INCLUDE
environment variable.Now, the ["VC++ Directories" → "Include directories"] is documented as corresponding to the
INCLUDE
variable. I.e., these directories are searched last. According to the documentation.And the ["C/C++" → "General" → "Additional Include Directories"] is documented as corresponding the
/I
option. I.e., 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).
I.e., 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.
默认情况下,Visual Studio 将以下路径放入其
INCLUDE
变量中(VC++ 目录 -> 包含目录):$(VCInstallDir) include
$(VCInstallDir)atlmfc\include
$(WindowsSdkDir)include;$(FrameworkSDKDir)\include
它们是预先配置的,只需将它们保留在原处即可。如果您的项目依赖于一些附加组件/框架,请将其标头的路径添加到C/C++ ->一般->其他包含目录(
/I
编译器开关)。在这种情况下,请在#include
语句中使用尖括号。对于库也是如此 - 保留 Visual Studio 默认值并将其他组件/框架中的库路径添加到链接器 ->其他库目录。
By default, Visual Studio puts following paths in its
INCLUDE
variable (VC++ Directories -> Include directories):$(VCInstallDir)include
$(VCInstallDir)atlmfc\include
$(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.