Menu

Using .h and .cpp files in an online C++ compiler (and why most of them fail)
📰

Using .h and .cpp files in an online C++ compiler (and why most of them fail)

DEV Community: cpp·Aswani Kumar·6 days ago
#Os93p5yB
#devtocpp#devto#dev#include#ifndef
Reading 0:00
15s threshold

Most online C++ compilers give you one editor, and one editor means one file. That's fine until your course splits a class into Student.h and Student.cpp . Then the second line of main.cpp stops the build:

code.cc:2:10: fatal error: Student.h: No such file or directory 2 | #include "Student.h" | ^~~~~~~~~~~ compilation terminated.

Enter fullscreen mode

Exit fullscreen mode

Here's why that happens, three ways around it, and the linker errors you'll meet on the way.

Why the include fails

#include "Student.h" tells the compiler to read Student.h from the folder of the file that includes it. A one-file online compiler saves your code on its own in an empty folder, so there is no Student.h to read.

Pasting in just the header doesn't fix it either. The header only declares the class; the member functions are defined in Student.cpp .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.