Compile and run C++ programming on your OS
C++ is completely free and readily available on all platforms.
Follow the tutorial below for running C++ on your computer.
There are multiple compilers and text editors you can use to run C++ programming. These may differ from system to system.
If you want a quick start, you can also run C++ program online.
Run C++ program Online
The most effortless way to program C++ has to be online and there are several online compilers to choose from.
There are several sites that allows you to run C++ programming online. The one I prefer is ideone.com.
To run C++ programming in Ideone.com, select C++ language at the bottom of the editor, write your code and click Run.
I don’t recommend using online compilers other than testing as it is very limited in use (you can’t work with files or take inputs from user).
To unlock the full potential of C++, it’s recommended to install compiler and run C programs from your system.
Here’s is the easiest way to run C++ programming in your system (Mac OSX, Windows and Linux) that suit a beginner’s purpose.
Run C++ Programming in Mac OS X
The easiest way to run C++ Programming in Mac OS X is by downloading XCode.
1. Go
to download page of apple developer site.
2. Click the download Xcode link. It’s recommended to download the latest version even if it’s in beta.
3. When download is completed, open Xcode and follow the wizard to install it. You might want to put the Xcode in Applications for future use.
4. Then,
open Xcode and go to File > New > Project
5. Under OS X section, choose Application, choose Command Line Tool and hit next.
6. Provide
the Product Name, for example:
Hello. And, choose C++ under Languagesection. Then, click next.
7. Choose the location where you want to save the project in your Mac. You can uncheck Create Git repository button and click create.
8. Navigate
to main.cpp file in the
screen where you can see default code provided by XCode.
9. Change the code as you wish. And finally to run the code, go to Product > Runor simply hit Cmd + R.
10. By default, you will see the output at the bottom of your screen.
Run C++ programming on Linux
To run C++ programming on Linux, you need:
Here’s a step by step guide to run C++ in Linux (Ubuntu, Debian, CentOS, Fedora, Redhat and Scientific Linux):
1. Open
the terminal and issue the following command.
For Ubuntu and Debian distribution:
2. $ sudo apt-get update
$ sudo apt-get install build-essential manpages-dev
For CentOS, Fedora, Redhat and Scientific Linux:
# yumgroupinstall 'Development Tools'
This installs GNU GCC compiler and related tools on your system.
3. To verify if gcc compiler is installed, issue the command
$ gcc --version
If you get output similar like this, gcc is correctly installed on your system.
4. Open
the text editor of your choice and save a file with .cpp extension. I made hello.cpp file using
gEdit.
If you are a linux wizard, feel free to use vim or emacs. Any editor is fine
but, don’t forget to use .cpp extension; it’s important.
5. Switch to the directory where the file is located. And, issue the following command.
$ g++ program-source-code.cpp-o name-of-your-choice
Here, program-source.code.cpp is the filename you chose before. And, name-of-your-choice can be any name you prefer. In my case, I issued the following command.
$ g++ hello.cpp -o hello
6. If there is no error, an executable file named is created; hello is my case.
Finally, you can see the output using following command.
$ ./hello
As you might have imagined, you need to use the name of executable file you chose before. Also, you need to use path to the execute file if you are in a different directory.
Run C++ in Windows (XP, 7, 8 and 10)
To run C++ Programming in Windows, you’d need to download Code::Blocks.
There are others available as well but Code::Blocks makes installation a piece of cake.
It’s easy, simple and developer friendly.
To make this procedure even easier, follow this step by step guide.
1. Go
to the binary release download page of
Code:Blocks official site.
2. Under Windows XP / Vista / 7 / 8.x / 10 section, click the link with mingw-setup highlighted row either from Sourceforge.net or FossHub.
3. Open the Code::Blocks Setup file and follow the instructions (Next > I agree > Next > Install); you don’t need to change anything. This installs the Code::Blocks with gnu gcc compiler, which is the best compiler to start with for beginners.
4. Now,
open Code::Blocks and go to File > New
> Empty file (Shortcut: Ctrl+Shift+N)
5. Write
the C++ code and save the file with .cpp extension.
To save the file, go to File > Save (Shortcut: Ctrl+S). Important:
The filename should end with .cpp extension, like: hello.cpp, your-program-name.cpp
6. To run the program, go to Build > Build and Run (Shortcut: F9). This will build the executable file and run it.
If your program doesn’t run and if you see error message "can't find compiler executable in your search path(GNU GCC compiler)", go to Settings > Compiler > Toolchain executables and click Auto-detect. This should solve the issue in most cases.