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

./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 thoughts on “How to Install C and C++ Compilers in Ubuntu and testing your first C and C++ Program

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

    Copy paste the following in terminal:

    sudo aptitude install build-essential

    [Reply]

  2. 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]

  3. 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]

  4. 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]

  5. 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]

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

    printf(”Hello, world\n”);

    [Reply]

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

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

  9. 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]

  10. 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]

  11. 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]

  12. 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]

  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.

    [Reply]

  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.

    [Reply]

  15. 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]

  16. 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]

  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.

    Cheers.

    [Reply]

Leave a comment

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