Check standard header names
Standard library headers use exact names. A transposed letter in <iostrem> does not refer to <iostream>. Check spelling before changing compiler settings. Keep angle brackets for standard headers and include the header that declares each library name.
This failing file asks for a header that does not exist:
#include <iostrem>
int main() { return 0; }
Run g++ -std=c++17 -Wall -Wextra main.cpp -o sample. MSVC reports:
fatal error C1083: Cannot open include file: 'iostrem': No such file or directoryGCC diagnostic core:
fatal error: iostrem: No such file or directory
Correct the spelling and rebuild. The include directive becomes #include <iostream>. Compiler wording may mention the source line and file, but C1083 and the missing name identify the relevant problem.
#include <iostream>
int main() { std::cout << "Ready\n"; return 0; }
This corrected source compiles with cl /std:c++17 /EHsc /W4 /permissive- main.cpp.
Find project headers
Project headers are files your program or team supplies. Their location matters. A quoted include such as #include "settings.h" searches locations associated with the including source and configured include directories. Angle brackets such as #include <settings.h> normally signal a library or configured include directory. Neither delimiter creates the file or searches every folder on your computer.
Suppose message.h is stored in an include subdirectory. The source names <message.h>, but the build command does not add that folder. This is the failing file:
#include <message.h>
int main() { return message(); }
#pragma once
int message();
Build it from the project root with g++ -std=c++17 main.cpp -o sample. MSVC reports fatal error C1083: Cannot open include file: 'message.h': No such file or directory when its search path omits include.
GCC diagnostic core:
fatal error: message.h: No such file or directoryAdd the folder to the compiler search path. Keep angle brackets or switch to quotes based on your project convention; the path configuration is what makes this folder searchable.
Set an include path
GCC accepts -Iinclude; MSVC accepts /Iinclude. These options add the project's include folder to header lookup. Use the path relative to the compiler's working directory, or provide an absolute path when your build system needs one. If you run the command from a different directory, a relative path can point somewhere else.
The same example works once the include directory is configured. The header declares message, and this complete program defines it locally:
#include <message.h>
#include <iostream>
int main() { std::cout << message() << '\n'; return 0; }
#pragma once
inline int message() { return 17; }
Build from the project root with g++ -std=c++17 -Iinclude main.cpp -o sample or cl /std:c++17 /EHsc /W4 /permissive- /Iinclude main.cpp. Both commands add the directory before compiling.
Check the full include spelling, the directory layout, and the compiler's working directory together. If your build uses an IDE or a build system, set its include directory there rather than relying on a command that the actual build never runs.
17
Add files that the project does not contain
A project include can also fail because the file was never created, was not copied into the checkout, or has another extension. Look for the actual file in the project tree and compare its spelling and capitalization. On case-sensitive file systems, Settings.h and settings.h are different paths.
This example includes a header that does not exist in the directory:
#include "settings.h"
int main() { return 0; }
MSVC reports:
fatal error C1083: Cannot open include file: 'settings.h': No such file or directoryCreate or restore the header at the intended path, then make sure your build includes the directory. Do not rename an unrelated file just to silence the message if the program expects a particular interface. Compare the included name, the file in version control, and the project structure.
GCC diagnostic core:
fatal error: settings.h: No such file or directoryA successful open only proves that the preprocessor found a file. If that file is empty or lacks a declaration, later diagnostics can report an unknown name. Fix include lookup first, then rebuild and address any new compiler message as a separate issue.
Exercise
Create a project with an include directory. Put a header named label.h there with an inline function that returns the integer 8. Include it from main.cpp, print the return value, and build with both a GCC include option and the matching MSVC option. Expected output:
8
Show one possible solution
#include <label.h>
#include <iostream>
int main() { std::cout << label() << '\n'; return 0; }
#pragma once
inline int label() { return 8; }
Compile from the project root with g++ -std=c++17 -Iinclude main.cpp -o sample. For MSVC, use cl /std:c++17 /EHsc /W4 /permissive- /Iinclude main.cpp.
Related guides
- Fix undefined references
- Fix multiple definitions
- Understand missing type declarations
- Search compiler diagnostics
Keep learning with CppPath
Continue with the Header & Source Files and Compiling & Linking lessons. Each full lesson includes a quiz and a checked coding challenge with a free account.