Step-by-step first c++ program compile and run.
1. Open a text editor
2. Write your code. For instance:
Code:
#include <iostream>
int main( void ) {
std::cout << "Hello World!" << std::endl;
return 0;
}
3. Save your code--make note of the directory you saved it in (usually something like /home/username/Documents or /home/username/Projects)
4. Open a terminal (depending on your desktop environment, this could be named GNOMETerm, Konsole, Xterm, etc.)
5. In the new terminal window opened, change to the directory that contains your saved code (e.g. "cd /home/username/Documents")
6. Type the following command:
Code:
g++ -o executable_name source_code_filename
7. Open your code and correct any error identified by the compiler and save.
8. Repeat steps #6 and #7 until you receive no errors.
9. Run your program by typing (note: do not omit the leading './'):
Substitute whatever you want to name your program for
executable_name.
Substitute the name of your saved code file for
source_code_filename.
If you receive a "command not found" error message when you try step #6, you need to install a c++ compiler.
For Debian (as root): "apt-get install g++"
For Ubuntu: "sudo apt-get install g++"
For RedHat based distros: "yum install gcc-c++"