moeminthar ၏

စိတ္ကူးတည့္ရာေရးထားေသာ ဘေလာ့ခ္

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

Posted by Zwe on April 22, 2012

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.

1. Install the build-essential package by typing the following command in the terminal: sudo apt-get install build-essential
2. Now create a file that has the extension .c (if you plan to write a C program) or .cpp (for a C++ program).
3. Write the code in that file.
4. Now open a terminal and go to the place where you saved that file using the cd command (e.g. cd Desktop).
5. If you have a C program type in the terminal
gcc -Wall -W -Werror hello.c -o hello.
The first line will invoke the GNU C compiler to compile the file hello.c and output (-o) it to an executable called hello.
The options -Wall -W and -Werror instruct the compiler to check for warnings.
6. If you have a C++ program simply replace gcc with g++ and hello.c with hello.cpp. The options do the same things.
7. If you get a permissions error, you need to make the file executable. You can do this with chmod +x hello.cpp
8. Now type in the terminal ./hello and the program will run.

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!

Advertisements

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

  1. […] ၏ How to Install C and C++ Compilers in Ubuntu and testing your first C and C++ Program- If you are a developer you need C and C++ Compiler for your development work.In ubuntu you can […]

  2. hi beautiful blog. We have a big classified site in Myanmar. Please visit us back at: http://ads.com.mm/?cid=4fd60e51e4b0fa6db841e336&utm_campaign=ads_mm_lb_blog_phung&utm_source=ads_lb_blog&utm_medium=lb_blog

  3. C/C++ blog said

    Ubuntu is better but working windows with visual studio is more enjoyable

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: