How to write and compile C++ program in Linux

Here is a step by step detailed guide on showing you how to write and compile a C++ program in Linux. For demonstration purposes, I’ll show you how to write and compile Hello World!



Difficult: Relatively Easy

How to write and compile Hello World in C++

1. First, let’s locate and open up our handy terminal.


terminal c++ hello world



1a. We might have to install the tools necessary to start writing, and compile our code. No, it isn’t like Windows where you need to find the website to download the appropriate program. In Linux, you simply say what you want to install, and it finds it for you. Neat eh? If you already have the programs installed, it will either upgrade, or do nothing so either way there is no harm in trying this out.


Installing g++ and emacs


Note: The password being asked is your password to log into your OS.

Note: If there are multiple versions of emacs available, choose emacs22 or something of the sort.
Explanation of code:

sudo apt-get install g++ //Gives permission to install G++
sudo apt-get install emacs //Gives permission to install emacs



2. Now, let’s create a new folder and open a file in that folder to write our Hello World program. I’ll explain the commands shown on the screen in more detail below the image.


terminal emacs hello world C++



Explanation of code:

ls //Simply displays all contents of current folder
mkdir FOLDERNAME //Makes a directory with FOLDERNAME
cd FOLDERNAME //Changes directory into FOLDERNAME
emacs hello_world.cpp & // Using emacs, creates a hello_world.cpp file. 



3. With emacs open, simply click on the editor and it should turn blank. Write the following code for your Hello World program. When done, simply hit save. (It will save in the folder you called emacs hello_world.cpp)


Hello World!



4. In your terminal type in the following commands to compile your program.
Hello World!



Explanation of code

g++ hello_world.cpp // Compiles your hello_world.cpp using g++ compiler
./a.out //Executes your program




Congratulations! You are done! I tried making it as SIMPLE as possible. If you still have any questions, feel free to drop a comment or suggestion to make this tutorial better!

Useful resources

C++ Guide Your guide for programming in C++


Related Posts

1 Star2 Stars (7 votes, average: 4.29 out of 5)

View Comments to “How to write and compile C++ program in Linux”

  1. Ugh. I am so confused. I just switched to Linux a few days ago. When i tried to install emacs “sudo apt-get install emac” it wouldn’t work and how do i open the c++ program thing. Man, i want to go back to vista.

  2. It usually depends on the Linux distribution you have. Many times, you can go into the Install Manager, and install the applications by searching for them.

    Also, you installed Ubuntu over your Vista? You should be able to use your Recovery CD to go back.

  3. This is the first blog i come across that teaches C and C++.
    I like your idea.will check regularly!!!

  4. Thanks for this quick steps in compiling C/C++ programs in GNU/Linux!

  5. Thanks for the tutorial. I found it via search engine. I was just about to give up on C++ then I realized that I didn’t even need an IDE! NetBeans is 150MB to DL… screw that. Using the CLI makes much more sense – it just works straight away!

  6. Nguyen Viet Hung 21. May, 2009 at 8:55 AM

    Thanks! It helps a lot!
    Looks easier than on Windows when you want to write and test just a few codes. On Windows, you must create a whole project (like Visual Studio).

  7. I’m glad this tutorial has been helpful!

    Yes, it may be much simpler on linux. I prefer using the terminal, g++, and vi to test simple programs.

  8. when i type ./a.out it tells me:
    bash: ./a.out: No such file or directory

  9. i compiled no errors i can’t excute it.

  10. Try performing the ls command.
    Do you see a.out in the first place?
    Did you compile the program using

    g++ main.cc or something similar?
    Try seeing if a file is created afterward.

  11. it’s easy to understand.
    And helped me to start compiling c++ in linux.
    Thanks a lot.

    for the question of
    Devon Says:
    November 5th, 2008 at 6:18 pm
    Ugh. I am so confused. I just switched to Linux a few days ago. When i tried to install emacs “sudo apt-get install emac” it wouldn’t work and how do i open the c++ program thing. Man, i want to go back to vista.

    corrected:?sudo apt-get install emacs(instead of emac)… I just noticed this one. Linux is better than vista. You made a bad decision to go back to vista. ^_^

  12. Thank u very much for these instructions , but they only worked with SUSE linux , when i tried this with Ubuntu it says that c++ and emacs are not installed ! , i tried to install using : sudo apt-get update but its taking a very long time …. also what is : using namespace std ? is writing this statement a must ?

  13. I believe with Ubuntu you can use the installer to find these packages.

    Simply look for emacs, g++ in the package manager.

    The answer to your second question is at:
    http://talkbinary.com/programming/c/hello-world-in-c/

    You usually find it in your programs. I’d just stick with it.

    If you have any more questions feel free to ask.

  14. Hi again , i have a number of questions regarding Ubuntu Linux ,
    Q1: i have installed Ubuntu side by side with my Windows Vista , so now i have a dual boot , unfortunately the space given to ubuntu wasnt sufficient ( 3.2 Gb ) and due to a large number of packeges being ionstalled later i couldnt simply execute C++ programs with emacs !! ,a message says : {cant execute , not enough space on device }, so i cant know easily identify the packeges installed , cause i typed : sudo-apt get update , now how can i uninstall ubuntu in order to install it again “with the idea of givin’ it a much larger space , wich i dont know how !

    Q2: When i try to select the normal or extra choices under the Visual effect tab , i get a message saying”Desctop effects couldnt be enabled !” now..compiz is installed and when i try to install my laptop drivers via the DVD { i have a Dell studio laptop with Intel Core 2 Duo 2 Ghz , 4 giga of RAM and 256 mega of video RAM on an Ati Radeon card}, it contains Linux installation file that ends with rpm extension , it refuses to be installed saying that rpm extension is not recignized, so i reaaly want to experience ubuntu visual effects ,,, any helpp please?

  15. For your first question:

    If you have a Partition Program, I believe you can allocate more space to your Ubuntu installion that way either on Windows or Ubuntu. I don’t remember exactly.

    Other way you can try is to remove Ubuntu and install it again. Do not remove Ubuntu without learning how to do it properly first! Best thing I did was first modified GRUB so Windows would boot first and then I removed Ubuntu. If you remove Ubuntu first, GRUB would try to boot and since its on Ubuntu, you wouldn’t be able to boot to any OS anymore. BAD!

    For the second question:
    Try the ubuntu forums for specifics on installing video drivers. They have tons of people who ask similar questions. Maybe someone with the same configuration already solved the problem?

  16. Hi again … using the Code Blocks C++ compiler , if iam writing a program that contains both main() , and a function , my Question is : can i write the main() in one file and the function in another file ? and when the program executes the main() will call the file that contains the function and gives the appropriate output ?

  17. All I get is an error message that says, “iostream: no such file or directory” Nothing works! I have no idea how to get this program to compile.

  18. @saif Yes you can, but doing that is a different topic on its own. Look into “makefiles”

  19. @Markus Did you install gdb.

    Did you try cutting and pasting the code? Maybe you mispelled something?

    If you haven’t gotten it to work yet, reply, and I’ll try to help you through with it.

  20. Awesome!!! Thank You! Wow using terminal makes it so much easier…..This helped me out a lot. Thanks again!

  21. i wrote the above lines in terminal but while inserting password(login) it shows error that sorry try again .after 3 attempts it stops and come back to home directry..
    plz .put ur advice …

  22. Are you the administrator or root user on Linux?

    Do you login to your homescreen with a password? Try using that one.

  23. i get this error when compiling on Opensuse 11.2 and i know there is no problem in my code.

    Documents/c> gcc hello.cpp
    hello.cpp:5: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x
    hello.cpp:7: error: expected ‘}’ before ‘;’ token
    hello.cpp:7: error: invalid conversion from ‘void*’ to ‘int’
    hello.cpp:9: error: expected unqualified-id before ‘return’
    hello.cpp:10: error: expected declaration before ‘}’ token

  24. can anyone help me writing di code above,,please i want a full code of dis

  25. while iam executing my program in linux I got an error that “expected ; befor ) token” but not in windows. What is this error and why it is so?

blog comments powered by Disqus