Namespaces, Includes, and the Standard Library

Setting Include Paths

How do I set up my compiler to find header files in different directories?

Abstract art representing computer programming

To set up your compiler to find header files in different directories, you need to specify the include paths using compiler flags. The exact flag depends on yourĀ compiler.

  • For GCC and Clang, use theĀ IĀ flag followed by the directory path.
  • For Microsoft Visual C++, use theĀ /IĀ flag followed by the directory path.

For example, if you have headers in a directory named "include" within your project directory, you would compile your code likeĀ this:

GCC/Clang:

eg++ -I./include main.cpp

MSVC:

cl /I.\include main.cpp

You can specify multiple include paths by using multiple -I or /IĀ flags.

In your code, you can then use the #include directive with the header file name, without specifying the fullĀ path:

#include "myheader.h"

The compiler will search for myheader.h in the specified includeĀ directories.

Many IDEs and build systems (like CMake) provide ways to set include paths in their project settings, so you don't have to specify them manually on eachĀ compile.

This Question is from the Lesson:

Namespaces, Includes, and the Standard Library

A quick introduction to namespaces in C++, alongside the standard library and how we can access it

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Namespaces, Includes, and the Standard Library

A quick introduction to namespaces in C++, alongside the standard library and how we can access it

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright Ā© 2024 - All Rights Reserved