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. Rudra says:

    @ Pral
    Hey in unix system there are no drive concept (like c:\,d:\ etc). all are these declare as a file system. you can find them at /media/(file system name). you want to paste some .h file? but there are no need for this because ubuntu have preinstall these library files.

    [Reply]

  2. Rodrigo says:

    Thanks for all.

    [Reply]

  3. Eddie says:

    It is useful to install manpages in addition to the compilers. You can do this for example using the following command:

    sudo apt-get install build-essential manpages-dev

    …or if you have already installed the build-essential package:

    sudo apt-get install manpages-dev

    [Reply]

  4. joseph Daniel says:

    Thanks for all

    [Reply]

  5. STEPS TO COMPILE C AND C++ ON UBUNTU

    For compilation of c code:
    1.Open : applications>accessories>terminal
    2.Type the following command in the terminal
    gedit name of ur file.c
    // I HAVE GIVE MY FILE NAME AS main.c
    For me i will type gedit main.c inside terminal
    3.Write ur code inside the gedit editor:
    #include
    #include
    int main(int argc, char** argv)
    {
    printf(“NAMAH SHIVAY\n”);
    return 0;
    }
    4.save the code by pressing ctrl+s
    5.In the terminal itself goto :File>Open terminal and do as follows in the newly opened terminal
    6.Compile the code using the following command :

    cc -c main.c
    7.This would produce an object file now you may need to add to the library.
    Then create an executable using the following command :
    cc -o main main.c
    8.Now run this executable using the following command :
    ./main

    FOR ANY DOUBTS MAIL ME
    [email protected]

    [Reply]

    sujay Reply:

    thank u dude working fine

    [Reply]

  6. ubuntu noob says:

    sudo aptitude install build-essential

    worked like a charm. thanks!

    [Reply]

  7. MM says:

    sudo aptitude install build-essential will not work any more.you should use sudo apt-get install build-essential

    [Reply]

  8. Jing says:

    THanks

    [Reply]

  9. jusitn says:

    “sudo aptitude install build-essential

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

    For this do u write that code in the terminal to download the compiler

    and if so the terminal says that
    the sudo:aptitude: command not found

    plzz help ty

    [Reply]

    jirO Reply:

    try,

    sudo apt-get install build-essential

    [Reply]

  10. viikky says:

    Guys i had a new idea install wine & get borland c 5.02.then install it with wine. it is most compatible for ubuntu 10.04 & ubuntu 11.04. if you use ubuntu 10.10 you must install borland c 4.5(or)turbo c 4.5.

    [Reply]

  11. viikky says:

    guys i think it will works for other versions of ubuntu too

    [Reply]

  12. Patricia says:

    Hey Guys, thanks for the help! This worked for me and I ran my C++ program. Once you create the editor file in Ubuntu, how would open the file back up?

    [Reply]

  13. Dutch says:

    Super helpful. Thanks!

    At patricia; Just open up (sudo gedit yourfile.cpp) and keep editing. the built (compiled) version of your code is a different animal all together from the cpp file that you are editing.

    [Reply]

  14. julle says:

    try this code:

    #include
    void main()
    {
    printf(“hello world!”);
    getchar();
    }

    is the same but you dont have to write “return 0;”
    :)

    [Reply]

  15. AH says:

    Hey, when I run the program, I typed as follow:
    ./test
    it shows the error message ” bash: ./MyFirstPointer: No such file or directory”

    Help me. What I need to do? Thanks a lot!

    [Reply]

    AH Reply:

    sorry, I typed as
    ./MyFirstPointer

    bash: ./MyFirstPointer: No such file or directory
    it’s what I got!

    [Reply]

  16. Mohammed says:

    That really helpful
    now I can develop apps using C and C++

    Thanks

    [Reply]

  17. Hello-

    HELP! Sometime soon, I would like to port some of my old C code projects to the Ubuntu environment, and compile them into executables there. Is there some documentation in one piece somewhere explaining the vagaries of the available standard compiler? Things like how to handle I/O devices other than console, floating point and other numerical precision options, interfacing to assembly language modules (and incidentally assemblers, preferably macro, that are available and compatible with the compiler). These and 100 other questions. Finding answers online might be possible for some things, but is tedious. Sorry my cyber-youth friends, but nothing beats a real book! A solid recommendation of such a book (perhaps one of the O’Reilly series) without a lot of generalized hand waving would be appreciated by this old timer, though Linux newbee.

    [Reply]

  18. Andrew P. says:

    The C++ code example shown above won’t compile with g++. To make it work, the vertical bar after the left brace in the fourth line must be deleted.

    The “bar” may actually be a screen capture of the text cursor. Why were screen images used in the code examples in this article, instead of actual text?

    [Reply]

    Andrew P. Reply:

    Here’s the corrected code:

    #include

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

    [Reply]

  19. Andrew P. says:

    (The comment form sorely needs a “preview” feature so that one will know how it will look before posting.)

    [Reply]

  20. jayesh betai says:

    lot of thanks for helping in installing “build-essential”package now I can make run program in c and c++ from linux
    thanx once again

    [Reply]

  21. Dalbir Singh says:

    Following is the code that I type in terminal.
    I have made hello.c file under /home/rana/C/ using gedit. please help

    rana@rana-FS-1050:~$ cc -c hello.c
    cc: error: hello.c: No such file or directory
    cc: fatal error: no input files
    compilation terminated.

    rana@rana-FS-1050:~$ cc -c /home/rana/C/hello.c
    rana@rana-FS-1050:~$ ./hello
    bash: ./hello: No such file or directory

    [Reply]

  22. madhura says:

    I installed c++ compiler using , sudo apt-get install build-essential

    when i type this code i got a message as below, what i must do now
    #include
    int main()
    {
    initscr();
    move(12,30);
    printw(“hello\n”);
    getch();
    endwin();
    return 0;
    }

    madhura@madhuwa:~$ g++ -omad first.cpp -lncurses
    first.cpp:1:21: error: ncurses.h: No such file or directory
    first.cpp: In function ‘int main()’:
    first.cpp:4: error: ‘initscr’ was not declared in this scope
    first.cpp:5: error: ‘move’ was not declared in this scope
    first.cpp:6: error: ‘printw’ was not declared in this scope
    first.cpp:7: error: ‘getch’ was not declared in this scope
    first.cpp:8: error: ‘endwin’ was not declared in this scope
    madhura@madhuwa:~$

    [Reply]

  23. Suresh says:

    Hi,

    Could someone please help me out in the below error, when I tried to compile the C program in my terminal.

    Program:
    #include

    int main()
    {
    printf(¨Hello, world¨);
    return 0;
    }

    $ cc -c first.c

    first.c: In function ‘main’:
    first.c:5: error: stray ‘\302’ in program
    first.c:5: error: stray ‘\250’ in program
    first.c:5: error: ‘Hello’ undeclared (first use in this function)
    first.c:5: error: (Each undeclared identifier is reported only once
    first.c:5: error: for each function it appears in.)
    first.c:5: error: stray ‘\302’ in program
    first.c:5: error: stray ‘\250’ in program
    first.c:5: error: ‘world’ undeclared (first use in this function)

    [Reply]

  24. Andrew P. says:

    @Suresh,

    The “quotes” surrounding “Hello, world” in your printf statement aren’t quotation marks. Be sure you’re composing your code on a text-only editor, such as vi, gedit, Notepad, etc., and not with a word processor such as Microsoft Word. The character you want appears on U.S. keyboards just above the apostrophe, on a key that is just to the left of the Enter key.

    [Reply]

  25. neha says:

    hey it is very easy to install c++

    [Reply]

  26. Matt says:

    I appreciate this post. I have been able to conquer a few issues. I am trying to figure out how to run programs with qt codes. Right now I am getting error messages:

    fatal error: QTextStream: No such file or directory
    compilation terminated.

    Here is the code. If anyone has any ideas, I will be very grateful!

    #include
    #include

    QTextStream cin(stdin);
    QTextStream cout(stdout);
    QTextStream cerr(stderr);

    int main() {
    int num1(1234), num2(2345) ;
    cout << oct << num2 << '\t'
    << hex << num2 << '\t'
    << dec << num2
    << endl;
    double dub(1357);
    cout << dub << '\t'
    << forcesign << dub << '\t'
    << forcepoint << dub
    << endl;
    dub = 1234.5678;
    cout << dub << '\t'
    << fixed << dub << '\t'
    << scientific << dub << '\n'
    << noforcesign << dub
    << endl:
    qDebug() << "Here is a debug message with "
    << dub << "in it." ;
    qDebug("Here is one with the number %d in it.", num1 );
    }

    [Reply]

  27. Viber Z ID says:

    Thanks! That is work! My text appear “Hello Linux! I love Ubuntu 12.10 because here I can learn C++ for my preparation to be System Programmer! Oh, Hello World! New System Programmer was born from Indonesia”
    And I think learn C++ is better than another programming language and become a System Programmer using C++ is funny I think! ?__? Thank you publisher! You was make my spirit improve! :D

    [Reply]

  28. vijay says:

    i have ubuntu and i need a c++ compiler…can anyone tell me how to install it..i have been working on c++ for the past one year in windows 7..and i don’t know..how to install the same..in ubuntu

    [Reply]

  29. shiva says:

    for c++ u used the gcc compiler for linux os

    [Reply]

Leave a Reply