"g++ -I..." and CPATH Environment Variable

Provides a tutorial example on how to use 'g++ -I...' option and/or CPATH environment variable to specify paths of include header files for GCC compiler.

If you are compiling a C/C++ program that uses custom header files, you need to specify the path name where the header files are located.

1. Create a simple header file, ./include/myheader.h:

herong$ vi ./include/myheader.h

#define Name "herong"

2. Create a C++ program file that uses the header file:

herong$ vi HeaderTest.cpp

#include "iostream"
#include "string"
#include "myheader.h"
int main(void) {
  std::string who (Name);
  std::cout <<  "Hello " <<  who <<  "!" <<  std::endl;
  return 0;
}

3. Compile it without specifying the include path:

herong$ g++ HeaderTest.cpp
HeaderTest.cpp:3:10: fatal error: myheader.h: No such file or directory
 #include "myheader.h"
          ^~~~~~~~~~~~
compilation terminated.

4. Compile it with "-I ..." option to specify include path:

herong$ g++ -I ./include -v HeaderTest.cpp

...
#include "..." search starts here:
#include <...>  search starts here:
 ./include
 /usr/include/c++/8
 /usr/include/c++/8/x86_64-redhat-linux
 /usr/include/c++/8/backward
 /usr/lib/gcc/x86_64-redhat-linux/8/include
 /usr/local/include
 /usr/include
End of search list.
...

herong$ ./a.out
Hello herong!

5. Compile it with "CPATH=..." environment variable to specify include path:

herong$ export CPATH=./include

herong$ g++ -v HeaderTest.cpp

...
#include "..." search starts here:
#include <...>  search starts here:
 ./include
 /usr/include/c++/8
 /usr/include/c++/8/x86_64-redhat-linux
 /usr/include/c++/8/backward
 /usr/lib/gcc/x86_64-redhat-linux/8/include
 /usr/local/include
 /usr/include
End of search list.
...

herong$ ./a.out
Hello herong!

Now I know the "-I ..." option or "CPATH" environment variable is adding more paths to the GCC default list to search for include header files.

Table of Contents

 About This Book

 Introduction to Linux Systems

 Cockpit - Web Portal for Administrator

 Process Management

 Files and Directories

 Users and Groups

 File Systems

 Installing CentOS

 SELinux - Security-Enhanced Linux

 Network Connection on CentOS

 Software Package Manager on CentOS - DNF and YUM

 Running Apache Web Server (httpd) on Linux Systems

 Running PHP Scripts on Linux Systems

 Running MySQL Database Server on Linux Systems

 Running Python Scripts on Linux Systems

 vsftpd - Very Secure FTP Daemon

 Postfix - Mail Transport Agent (MTA)

 Dovecot - IMAP and POP3 Server

 Email Client Tools - Mail User Agents (MUA)

GCC - C/C++ Compiler

 Install GCC C/C++ Compilers

 "g++ --verbose" - GCC Compiler Steps and Settings

"g++ -I..." and CPATH Environment Variable

 "g++ -l..." to Link with Library Files

 "g++ -c" and "ar src" to Build Static Library

 "g++ -shared" to Build Dynamic Library

 "ldd" - Dynamic Library Dependency Checker

 "make" - Manage Program Build Process

 Conda - Environment and Package Manager

 Tools and Utilities

 References

 Full Version in PDF/EPUB