[SATLUG] include paths

Borries Demeler demeler at biochem.uthscsa.edu
Mon Jun 16 18:51:48 CDT 2014


That's a very helpful explanation - thanks very much, Chad!
It makes sense that the preprocessor should handle the cascade of includes.

Regards, -Borries


On Sun, Jun 15, 2014 at 07:36:24PM -0700, Chad Wilson wrote:
> The paths for system headers are set/configured when GCC is compiled. Invocation of gcc with -I is your 'API' for altering the search path.
> 
> As far as sub-directories go, you have to specify the path relative to the search paths. Read the manual for proper use; which is usually sections 2 and 3 for system calls and clib functions respectively. For example, 'man 2 listen' indicates that sys/types.h and sys/socket.h should be included:
> 
> LISTEN(2)                  Linux Programmer's Manual                 LISTEN(2)
> 
> NAME
>        listen - listen for connections on a socket
> 
> SYNOPSIS
>        #include <sys/types.h>          /* See NOTES */
>        #include <sys/socket.h>
> 
>        int listen(int sockfd, int backlog);
> 
> 
> Many of the headers only contain macros that include other headers as well. You should allow the pre-processor figure that out for you; especially if you want to me cross-platform compliant. For example, on my system, '/usr/include/errno.h' includes <bits/errno.h>, which includes <linux/errno.h>, which includes <asm/errno.h>, which includes <asm-generic/errno.h>, which includes <asm-generic/errno-base.h>, which  FINALLY includes the errno definitions. But, it would be wrong to simply include <asm-generic/errno-base.h> in your program. Again, read the manual and let the pre-processor do the work. 'man 3 errno' tells you which header to include:
> 
> ERRNO(3)                   Linux Programmer's Manual                  ERRNO(3)
> 
> NAME
>        errno - number of last error
> 
> SYNOPSIS
>        #include <errno.h>
> 
> 
> Chad Wilson
> 
> 
> On Sunday, June 15, 2014 9:34 PM, Chad Wilson <chad_w_wilson at yahoo.com> wrote:
>  
> 
> 
> The paths for system headers are set/configured when GCC is compiled. Invocation of gcc with -I is your 'API' for altering the search path.
> 
> As far as sub-directories go, you have to specify the path relative to the search paths. Read the manual for proper use; which is usually sections 2 and 3 for system calls and clib functions respectively. For example, 'man 2 listen' indicates that sys/types.h and sys/socket.h should be included:
> 
> LISTEN(2)                  Linux Programmer's Manual                 LISTEN(2)
> 
> NAME
>        listen - listen for connections on a socket
> 
> SYNOPSIS
>        #include <sys/types.h>          /* See NOTES */
>        #include <sys/socket.h>
> 
>        int listen(int sockfd, int backlog);
> 
> 
> Many of the headers only contain macros that include other headers as well. You should allow the pre-processor figure that out for you; especially if you want to me cross-platform compliant. For example, on my system, '/usr/include/errno.h' includes <bits/errno.h>, which includes <linux/errno.h>, which includes <asm/errno.h>, which includes <asm-generic/errno.h>, which includes <asm-generic/errno-base.h>, which  FINALLY includes the errno definitions. But, it would be wrong to simply include <asm-generic/errno-base.h> in your program. Again, read the manual and let the pre-processor do the work. 'man 3 errno' tells you which header to include:
> 
> ERRNO(3)                   Linux Programmer's Manual                  ERRNO(3)
> 
> NAME
>        errno - number of last error
> 
> SYNOPSIS
>        #include <errno.h>
> 
> 
> Chad Wilson
> 
> 
> 
> On Sunday, June 15, 2014 1:06 PM, Borries Demeler <demeler at biochem.uthscsa.edu> wrote:
>  
> 
> 
> Thanks, Bruce! That's very helpful.
> 
> -Borries
> 
> On
>  Sun, Jun 15, 2014 at 12:01:36PM -0500, Bruce Dubbs wrote:
> > Borries Demeler wrote:
> > >Can anyone tell me where the default include paths on a
> > >regular unix system are defined?
> > >
> > >I.e., where are these paths set:
> > >
> > >      /usr/local/include
> > >      libdir/gcc/target/version/include
> > >      /usr/target/include
> > >      /usr/include
> > >
> > >so that gcc can find them.
> > >
> > >Also: If I include a file like this:
> > >
> > >#include <file.h>
> > >
> > >The assumption is that the file can be found in the include
> > >path that has been set in the environment.
> > >
> > >So what if I have a subdirectory in /usr/include, say "sys", do I have set
> > >this manually in my search path (i.e., #include <sys/file.h> or can unix
> > >find it automatically? I see many subdirectories under /usr/include but
> > >it is not clear if their contents are automatically supposed to be found.
> > 
> > It's built into gcc.
> > 
> > $ echo 'main(){}' > dummy.c
> > $ gcc dummy.c -v 2>&1 | grep -B10 'search list'
> > 
> > #include "..." search starts here:
> > #include <...> search starts here:
> >  /opt/xorg/include
> >  /usr/lib/gcc/x86_64-unknown-linux-gnu/4.8.2/include
> >  /usr/local/include
> >  /usr/lib/gcc/x86_64-unknown-linux-gnu/4.8.2/include-fixed
> >  /usr/include
> > End of search list.
> > 
> > It can be adjusted with CPATH, C_INCLUDE_PATH, and CPLUS_INCLUDE_PATH.
> > 
> > In my case, I have
> > 
> > CPLUS_INCLUDE_PATH=/opt/xorg/include
> > C_INCLUDE_PATH=/opt/xorg/include
> > 
> > https://gcc.gnu.org/onlinedocs/gcc-4.9.0/gcc/Environment-Variables.html#Environment-Variables
> > 
> >   -- Bruce
> > -- 
> > _______________________________________________
> > SATLUG mailing list
> > SATLUG at satlug.org
> > http://alamo.satlug.org/mailman/listinfo/satlug to manage/unsubscribe
> > Powered by Rackspace (www.rackspace.com)
> 
> -- 
> _______________________________________________
> SATLUG mailing list
> SATLUG at satlug.org
> http://alamo.satlug.org/mailman/listinfo/satlug to manage/unsubscribe
> Powered by Rackspace (www.rackspace.com)
> --
> _______________________________________________
> SATLUG mailing list
> SATLUG at satlug.org
> http://alamo.satlug.org/mailman/listinfo/satlug to manage/unsubscribe
> Powered by Rackspace (www.rackspace.com)


More information about the SATLUG mailing list