How to Install C and C++ Compilers in Ubuntu and testing your first C and C++ Program

Sponsored Link
If you are a developer you need C and C++ Compiler for your development work.In ubuntu you can install the build-essential for C and C++ compilers.

Install C and C++ Compilers in Ubuntu

sudo aptitude install build-essential

This will install all the required packages for C and C++ compilers

Testing C and C++ Programs

Compiling Your first C Programs

Now you need to open first.c file

sudo gedit first.c

add the following lines save and exit the file

Firstly compile the code using the following command

cc -c first.c

that would produce an object file you may need to add to the library.

then create an executable using the following command

cc -o first first.c

Now run this executable using the following command


Output should show as follows

Hello, world

Compiling your first C++ program

If you want to run c++ program follow this procedure

g++ is the compiler that you must use.

you should use a .cpp file extension rather than a .c one

You need to create a file

sudo gedit first.cpp

add the following lines save and exit the file

Run your C++ Program using the following command

g++ first.cpp -o test


Output should show as follows

Hello World!

Sponsored Link

Related posts

161 thoughts on “How to Install C and C++ Compilers in Ubuntu and testing your first C and C++ Program

  1. Worked perfectly on first try for both C and C++ test programs. Very clear instructions. Thanks!

  2. “can know the link to download C++ compiler for Ubuntu 9.04”

    Copy paste the following in terminal:

    sudo aptitude install build-essential

  3. Guys , i did install the essential , started the editor, input the test for both C++ ( which i am interested in) and C.
    Does not build, nor execute.

    Plz help i have the feeling i am not in theproper directory.

  4. When I enter:

    sudo aptitude install build-essential

    it says:

    Couldn’t find any package whose name or description matched “build-essential”

    Perhaps I am in the wrong directory?

    Any advice would be greatly appreciated.

    Thank you,

  5. OK, that last post was totally newbie. However, just in case some other green horn is out there with the same issue:

    If you have a fresh install of Ubuntu Jaunty, you must first go online and download all updates. After this, the ‘sudo aptitude install build-essential’ command will install correctly.

    Good luck.

  6. You are awesome !!! I did install and successfully compiled and ran it…

  7. Check if you have put \n after Hello worlod, i.e. the line should read here as under;

    printf(”Hello, world\n”);

  8. haha thanks dude, i broke my leg and its the summer and i figured i’d try learning C++ in linux so i dont go crazy here. This is pretty cool, it all worked fine except the C++ tutorial:

    int main()
    std::cout << "Hello World!" << std::endl;
    return 0;

    i had to remove the pipe (|) after the first { and compiled fine

  9. [Quote]thanks dude … for tht thig ….its running
    but the problem is tht when i executed it
    the output came as
    helo [email protected]:~$[/Quote]

    ok, the
    [code][email protected]:~$[/code]
    is just prompting you for another command… it’s gonna show up if you did everything else properly.

  10. Hi. Sorry to bother but I am super new to Ubuntu and when I ran this program I got all of these errors…. Can you help me please? Thanks

    hello_world.cpp:4: error: stray ‘\302’ in program
    hello_world.cpp:4: error: stray ‘\250’ in program
    hello_world.cpp:4: error: stray ‘\302’ in program
    hello_world.cpp:4: error: stray ‘\250’ in program
    hello_world.cpp: In function ‘int main()’:
    hello_world.cpp:4: error: ‘cout’ is not a member of ‘std’
    hello_world.cpp:4: error: ‘Hello’ was not declared in this scope
    hello_world.cpp:4: error: expected `;’ before ‘World’

  11. This got me started, so thanks for that. I was able to build my own socket server… C rocks!

    One thing I noticed is that you don’t get the header files like mysql.h gtk.h automatically.

    I’m ready for the next step, can you advise?

  12. Hello. Im new to linux and gcc.

    A question … do i always have to write “sudo gedit srcname” , quit qedit after making changes and compile with shell using “g++ srcname -o binname” ?

    Is there any gcc version with gui or something like more comfortable ?

  13. Great. Very clear. Thanks for the help. Particularly for this stuff at the beginning, I’m glad you did not underestimate the potential lack of knowledge.

  14. I am very new in Ubuntu & a CSE student. I want to use gcc compiler to do my coding in ubuntu. But I do not know anything even about how to open this compiler’s IDE. So please help me.

  15. hi , i hav ubuntu and i m new to it , i have c on my xp , can i copy paste that on ubntu and run c ?
    i didnt get all the above 🙁 ,
    is c already present on linux , shall i need to download from net …

  16. Thanks for the clear instructions – got everything working within minutes. After the apt-get, that is… 🙂

  17. Hey there, you don’t need sudo for gedit. gedit is an editor, you only want to invoke it with root privileges when editing files you don’t have direct write access to, such as /etc/hostname. Secondly, when you need to edit those files, it is a good habit to use gksu instead of sudo for gtk (graphical) applications, and kdesudo or kdesu can be used to obtain root privileges on KDE.

    So to use gedit to create or edit a source file, “gedit something.cpp” will suffice.


Leave a comment

Your email address will not be published. Required fields are marked *