Read the program
The include directive makes declarations from the standard input and output header available. The preprocessor handles this directive before the compiler checks the program's C++ statements. The angle brackets identify a standard header. A misspelled header name prevents compilation before the compiler reaches the function.
#include <iostream>
int main() {
std::cout << "Ready to compile!\n";
return 0;
}
int main() defines the program's entry point and says it returns an integer status. Its braces group the statements that run when the program starts. The body contains one output statement and one return statement. Each statement ends in a semicolon. A missing semicolon changes how the compiler parses the next token, so the diagnostic can point at a later line than the actual omission.
std::cout names the standard output stream. The prefix std:: qualifies the name inside the standard namespace, where the library declares cout. The insertion operator << sends its right-hand value to that stream. Here the value is a string literal. The escape \n adds a newline, so the next output starts on a new line. It does not print the two characters backslash and n.
The final return 0; ends main and reports successful completion to the operating system. Output and status are separate: printing a success message does not set the status, and returning zero does not print anything. C++ also permits falling off the end of main to return zero, but writing the return statement makes the status visible to a beginner reading the function.
Compile and run
Save the source as main.cpp. With GCC, run g++ -std=c++17 -Wall -Wextra main.cpp -o hello. The standard flag selects C++17. The warning flags ask GCC to report common issues. The compiler reads main.cpp and writes an executable named hello. On Windows, invoke it as hello.exe; on many Unix-like shells, run ./hello.
With the Visual Studio developer environment open, compile the same file with cl /std:c++17 /EHsc /W4 /permissive- main.cpp. MSVC writes main.exe by default. The /std:c++17 option selects the language standard, /W4 enables a useful warning level, and /permissive- requests standard-conforming behavior. After compilation succeeds, start the executable from the same directory.
Both commands compile the source; neither command is the program's output. A successful build produces an executable. Running that executable prints:
Ready to compile!
If the compiler cannot create an executable, read the first diagnostic rather than guessing from the final summary. A syntax error usually names a token or line near the problem. A link error means compilation passed but the linker could not assemble the required program. Keep the complete command with the source file so you can compare what you typed with what the compiler actually built.
Fix three first errors
A missing semicolon
Every complete output expression needs a terminating semicolon. This short file leaves it out. The compiler may complain at the closing brace because it expected the statement to finish earlier. Add the semicolon after the string expression. See how to find a missing semicolon.
#include <iostream>
int main() {
std::cout << "Start\n"
return 0;
}
MSVC reports this case as:
error C2143: syntax error: missing ';' before 'return'
A missing std::
Names in the standard library live in namespace std. Without the prefix, this statement asks for an unqualified name named cout. Add std:: at the use site, or use a carefully scoped declaration. Qualifying the name directly keeps its origin clear. See how to fix a name that was not declared.
#include <iostream>
int main() {
cout << "Start\n";
return 0;
}
MSVC reports the unqualified name as:
error C2065: 'cout': undeclared identifier
A misspelled main
The implementation expects the exact entry-point spelling main. A function named mian is an ordinary function and does not become the entry point because it has a similar name. Rename it to main. This error often appears during linking because the source may compile while the required entry point is absent. Read the linker error guide for the distinction between compiling and linking.
int mian() {
return 0;
}
MSVC reports the missing entry point as:
LINK : fatal error LNK1561: entry point must be defined
These examples fail for different reasons, so their fixes differ too. A missing punctuation mark is a syntax problem. An unqualified library name is a name lookup problem. A renamed entry point is a link problem. First identify the build stage and the source token the diagnostic mentions. Then make one targeted edit and rebuild.
GCC diagnostic core for the missing semicolon:
error: expected ';' before 'return'GCC diagnostic core for the unqualified name:
error: 'cout' was not declared in this scope; did you mean 'std::cout'?GCC reports the missing main when it links:
undefined reference to `main'
collect2: error: ld returned 1 exit status
When a program still fails, verify that the editor saved the file you compiled. Check the working directory and the source filename in the command. A compiler can build an older copy if two files have similar names or if the terminal starts in another folder. Small programs make these checks easier because the build command and the source have few moving parts.
Compiler diagnostics are clues, not a replacement for reading the code. GCC and MSVC use different wording, and the first reported line can follow the actual mistake. Look for the earliest diagnostic in the build log. Fix that issue, compile again, and then inspect any new message. This sequence avoids chasing follow-on errors produced by the first invalid token.
Exercise
Write a complete program that prints CppPath is ready on one line. Use std::cout, include its standard header, and return a successful status from main. Compile the solution as C++17, then run it. Expected output:
CppPath is ready
Show one possible solution
#include <iostream>
int main() {
std::cout << "CppPath is ready\n";
return 0;
}
Related guides
Keep learning with CppPath
Continue with Program Structure, Input & Output, and Compiling & Linking. Each full lesson includes a quiz and a checked coding challenge with a free account.