Why do projects use the -I include switch given the dangers?

后端 未结 4 960
独厮守ぢ
独厮守ぢ 2021-02-01 15:20

Reading the fine print of the -I switch in GCC, I\'m rather shocked to find that using it on the command line overrides system includes. From the preprocessor docs

4条回答
  •  萌比男神i
    2021-02-01 15:41

    Looking back at the GCC manuals it looks like -iquote and other options were only added in GCC 4: https://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Directory-Options.html#Directory%20Options

    So the use of "-I" is probably some combination of: habit, lazyness, backwards compatibility, ignorance of the new options, compatibility with other compilers.

    The solution is to "namespace" your header files by putting them in sub directories. For example put your endian header in "include/mylib/endian.h" then add "-Iinclude" to the command line and you can #include "mylib/endian.h" which shouldn't conflict with other libraries or system libraries.

提交回复
热议问题