Ubuntu Geek

Ubuntu Linux Tips,Howtos&Tutorials|Edgy,Feisty,Gutsy,Hardy

  • Subscribe RSS Feed

    subscribe to the ubuntu Geek RSS feed




  • Sponsor

  • Categories

  • Meta

  • Sponsor

  • Archives



  • WidgetBucks - Trend Watch - WidgetBucks.com

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

Posted by admin on May 4th, 2008

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!

If you want to be notified the next time we write something please subscribe to our RSS feed.Thanks for Visiting!

Related Articles

7 Responses to “How to Install C and C++ Compilers in Ubuntu and testing your first C and C++ Program”

  1. Martin Says:

    There is no need for being root, when editing your source code. So, call gedit WITHOUT sudo.

    Cheers,
    Martin

  2. replaced Says:

    > sudo gedit

    omg you fail

  3. karl Says:

    karl@ubuntu:~$ g++ first.cpp -o test
    first.cpp:5: error: stray ‘\357’ in program
    first.cpp:5: error: stray ‘\243’ in program
    first.cpp:5: error: stray ‘\271’ in program
    first.cpp: In function ‘int main()’:
    first.cpp:4: error: expected primary-expression before ‘|’ token
    first.cpp:5: error: ‘end’ is not a member of ‘std’
    karl@ubuntu:~$ ./test
    bash: ./test: No such file or directory

  4. karl Says:

    the c++ didn’t work for me
    karl@ubuntu:~$ g++ first.cpp -o test
    first.cpp:5: error: stray ‘\357’ in program
    first.cpp:5: error: stray ‘\243’ in program
    first.cpp:5: error: stray ‘\271’ in program
    first.cpp: In function ‘int main()’:
    first.cpp:4: error: expected primary-expression before ‘|’ token
    first.cpp:5: error: ‘end’ is not a member of ‘std’
    karl@ubuntu:~$ ./test
    bash: ./test: No such file or directory

  5. Thomas Says:

    Never call your test program “test”. There is already a system utility called “test”. If you run “test” instead of “./test” you’ll have endless hours of fun:-)

    “”"first.cpp:5: error: stray ‘\357’ in program”"”

    looks like non-ASCII characters in your source file. Did you use a wordprocessor to edit it by any chance?

    “”"first.cpp:5: error: ‘end’ is not a member of ‘std’”"”

    it’s “endl”, not “end”. Note the trailing “eL” for end-line.

  6. Akshar Says:

    It would be better if you use gcc compiler instead of cc.

  7. FrozenFire Says:

    Akshar, cc is gcc on *buntu.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>