

Unfortunately, the INTERFACE scope is a little bit difficult to understand. Interface Include Directories – The Most Uncommon Option Just like normal C++ inheritance, the PRIVATE scope means that my_lib will be able to see headers in some_directory, but targets linking against my_lib will not. target_include_directories(my_lib PRIVATE some_directory) This means that the include directories will not be searched for targets depending on our my_libtarget, so my_program cannot use the headers in the directory some_directory.
CMAKE INSTALL DIFFERENT VERSION CODE
In the CMake world, the meaning isn’t so different!Ĭhanging line 5 in the snippet above to the following code would make the “inheritance” of include directories private. In the English language, the opposite of public is private. Just like public inheritance! Keeping Directories To Yourself With Private Include Directories In other words, using the PUBLIC scope specifier will cascade the include directories to the dependent targets of a target.

In our example, some_directory will be searched for the compilation of my_lib and my_program, as it’s using (or linking) my_lib with the target_link_libraries(.) command. Having PUBLIC target include directories for my_lib simply means that the include directories of our library will be used for the compilation of it, as well as the targets that use the library. Public Target Includes – Cascading Include Directories
CMAKE INSTALL DIFFERENT VERSION HOW TO
Target_link_libraries(my_program PRIVATE my_lib)įor more information on adding libraries with CMake, check out how to use the add_library command in CMake. Target_include_directories(my_lib PUBLIC some_directory)Īdd_executable(my_program my_program_source.cpp) add_library(my_lib STATIC my_lib_source.cpp) CMake Include Directories Scope? What Are They? Just Like Inheritance In C++įirstly, to make things easier to explain, let’s assume we have a target for a static library called my_lib, and a target for an executable called my_program, that uses the library and its headers. So we’re essentially saying that “target_name” should look for header files (or any included files) in the directories specified after the scope.

Unsurprisingly, this CMake command adds include directories to CMake target, and the syntax is the following: target_include_directories(target_name directories.) The star of the show is the target_include_directories(.) command. 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 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.
