Как #include'ы писать надобно
От: b0r3d0m  
Дата: 28.04.17 03:40
Оценка:
эй ниггаз.

Поспорили тут с коллегой на тему того, как #include'ы в C++ писать надобно. Речь пойдёт о VS.

Итак, имеются следующие варианты подключаемых файлов:
1. Файлы, являющиеся частью того же самого проекта.
2. Файлы, являющиеся частью другого проекта в том же solution'е.
3. файлы, являющиеся частью стандартной библиотеки C++.
4. файлы, являющиеся частью каких-либо third-party libraries (WinAPI, boost etc).

Я считаю расово верным следующий вариант:
Если путь до подключаемого файла указан в Additional Include Directories / системных путях / IDE, или если он является частью стандартной библиотеки языка, то должны использоваться angle brackets, т.е. #include <smth>.
В противном случае должны использоваться double quotes, т.е. #include "smth".

Таким образом, получаем:
1. ""
2. <> (ссылаемся на этот самый другой проект через Additional Include Directories)
3. <>
4. <> (ссылаемся на эти самые third-party libraries через Additional Include Directories)

По стандарту, по сути, оба варианта resolve'ятся в implementation-defined manner, но раз мы говорим про VS, то и смотреть надо в MSDN, а там сказано следующее:

Quoted form The preprocessor searches for include files in this order:
1) In the same directory as the file that contains the #include statement.
2) In the directories of the currently opened include files, in the reverse order in which they were opened. The search begins in the directory of the parent include file and continues upward through the directories of any grandparent include files.
3) Along the path that's specified by each /I compiler option.
4) Along the paths that are specified by the INCLUDE environment variable.

Angle-bracket form The preprocessor searches for include files in this order:
1) Along the path that's specified by each /I compiler option.
2) When compiling occurs on the command line, along the paths that are specified by the INCLUDE environment variable.


, что только подкрепляет высказанное мной предложение (ведь лучше идти навстречу компилятору, верно?).

Что скажете? Дискасс.
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.