
In addition, line 4 creates the my_program executable from the file program.cpp. Precisely, lines 1-2 will set some required CMake project settings, which you can learn more about in my introduction to CMake post. Without further ado, the following lines of CMake will add include directories to a particular CMake target. Using CMake To Add C++ Include Directories In addition, CMake will work with any compiler you use, making the project’s build setup way more portable. Luckily for us, CMake wraps all this functionality into easy-to-understand commands. Obviously, the assumption here is that we’re working with the GCC compiler, but whether you’re using Clang or MSVC, the idea is the same: the compiler needs to know where your files are! More specifically, it tells GCC that our included files may live in those directories. Interestingly, the -I flag tells the compiler to look for files in the directory following the flag. gcc -Ifirst_dir -Isecond_dir -o my_program program.cpp

For example, compiling the code in the source file program.cpp that includes the header files first_dir/first_include.h and second_dir/second_include.h needs the following command. Telling the compiler where your include files are isn’t too difficult. So how do we tell CMake the location of our include files? CMake Makes Working With The Compilers Easier
Cmake include remote header how to#
However, the compiler needs to know how to find the file. Essentially, whatever file we include in that statement gets copied and pasted into the current source file by the compiler. With CMake, adding header include directories to your C++ project is as easy as using your head in football! Heading those C++ include directories is easy with CMake.Īs you are probably aware, you can include other source files in C++ with the #include pre-processor directive.

In C++, this can be achieved by having multiple header and source files for different components. A good coding practice is splitting up your code into different reasonable and relatable pieces of code.
