May 4, 2008 · General · Email This Post

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

./first

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

./test

Output should show as follows

Hello World!

Sponsored Link

Incoming search terms:

Related posts

160 Comments to “How to Install C and C++ Compilers in Ubuntu and testing your first C and C++ Program”

  1. Rob says:

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

    [Reply]

  2. Simplementenis says:

    I’m going to try it.!!
    I hope it work.

    [Reply]

  3. Than Win Naing says:

    Good instructions.
    It’s OK for me.
    Thanks!

    [Reply]

  4. Sabri says:

    can know the link to download C++ compiler for Ubuntu 9.04

    [Reply]

  5. Unhackmee says:

    “can know the link to download C++ compiler for Ubuntu 9.04″

    Copy paste the following in terminal:

    sudo aptitude install build-essential

    [Reply]

  6. alex says:

    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.
    Thx

    [Reply]

  7. 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,
    Dennis

    [Reply]

  8. 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.

    [Reply]

  9. Samsuddin Wira says:

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

    [Reply]

  10. chinmay says:

    thanks dude … for tht thig ….its running
    but the problem is tht when i executed it
    the output came as
    helo worldvinamra@vinamra-desktop:~$

    [Reply]

  11. chinmay says:

    i dont want tht name with it

    [Reply]

  12. Bharat says:

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

    printf(”Hello, world\n”);

    [Reply]

  13. TkA says:

    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:
    #include

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

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

    [Reply]

  14. Brandt says:

    [Quote]thanks dude … for tht thig ….its running
    but the problem is tht when i executed it
    the output came as
    helo worldvinamra@vinamra-desktop:~$[/Quote]

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

    [Reply]

  15. gautam says:

    i want c-programing in my computer

    [Reply]

  16. New8 says:

    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’

    [Reply]

  17. New8 says:

    Nvm on my problem…. solved it myself :)

    [Reply]

  18. nuntius says:

    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?

    [Reply]

  19. Divyanshu says:

    wow!!!!
    its great……
    it wrkd……
    compiler installed in 10 min……
    thanks…

    [Reply]

  20. anonymous says:

    2TKA

    that pipe supposed to be cursor.

    [Reply]

  21. Av_ says:

    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 ?

    [Reply]

  22. hervman says:

    i don’t know how you do that guys, i follow the instructions but nothing works. i only have this message:
    hervman@ubuntu:~$ gedit
    hervman@ubuntu:~$ gedit first.cpp
    hervman@ubuntu:~$ g++ first.cpp -o test./test
    /usr/bin/ld: cannot open output file test./test: No such file or directory
    collect2: ld returned 1 exit status

    [Reply]

  23. Worked great. I wish they would add these packages as default. But none the less, it was no problem adding them.

    Thanks!

    [Reply]

  24. Tim says:

    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.

    [Reply]

  25. charlemagne says:

    how do i install c++ on my own computer?? well, this page is interesting…

    [Reply]

  26. Atanu says:

    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.

    [Reply]

  27. sanjay says:

    hey hervman,

    you did a mistake. check this out

    hervman@ubuntu:~$ gedit first.cpp
    hervman@ubuntu:~$ g++ first.cpp -o test
    hervman@ubuntu:~$ ./test

    [Reply]

  28. Ajinkya says:

    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 …

    [Reply]

  29. Andy says:

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

    [Reply]

  30. Tim says:

    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.

    Cheers.

    [Reply]

Leave a Reply