C/C++ is one of the most popular programming language, mostly used in system programming (system software development e.g Most of the modern operating systems are programmed (primarily) either in C language or C++). C is a High Level language as compared to Assembly program, so it comes in the middle of Assembly and Java like High Level Language… That’s the beauty of C – it’s very easy to write program in C as compared to Assembly programming and fast execution time as compared to Java.

By default, Ubuntu 11.04 (code name:=Natty Narwhal) or any other Linux Based Distribution like Fedora, Linux Mint(10 or 9), Debian etc.. are very good for programming purposes(e.g C, C++, Java, PHP, Perl, Scripting, Ruby etc.). A lot of free and open source tools like IDE, debuggers are available which will makes the programming a lot Funky, with Ubuntu. In this post you will learn how to compile a C/C++ program on Ubuntu 11.04, from command line, you can also use some IDE like Eclipse, NetBeans if you want (but it’s not recommended for beginners). Ubuntu 11.04 comes with gcc(GNU Compiler Collection), installed by default which is one of the most widely used modern C compiler.

Compiling and Running C program on Ubuntu 11.04

#1 : Write a program, better use gedit (The Default Text editor). Right click on Desktop and create a new file – ‘fun.c’ and copy the code, given below and save it (CTRL+S). (C programs have the extension .c while C++ programs, have .cpp, although it’s a different fact that Linux doesn’t recognize file based on their extension but some applications may do that and it also avoid confusion, so better use proper extension)

#include<stdio.h>
void main()
{
/* Don't take it seriously, it's just for Fun! */
printf("Have Fun!\n");
}

#2 : Now you compile the program using GCC compiler (I think it’s installed by default in most versions in Ubuntu, but if it’s not the case then install it by typing – sudo apt-get install gcc at terminal). Open the Terminal (CTRL+ALT+T) and type the command (First move on to the directory where your file is located, I assume you have created the file on Desktop, If you are an absolute beginner – Learn Some basic Linux Commands).

cd Desktop
gcc fun.c -o fun1

The -o option (specifies the Output File Name) in the following command is optional, but it’s a good practice, because if you won’t specify that – then a default a.out file will be created (which will eventually overwrite older a.out file in that directory).

#3 : Execute/Run the program. Type  –

./fun1

#4 : That’s all. Here is one snapshot of output terminal –

compiling-executing-c-programCompile and Run C++ program

To compile C++ program, you may need to install g++.

Installing g++ compiler on Ubuntu 11.04

sudo apt-get install g++

Then all the remaining procedure is almost same. Just replace gcc by g++ from the above tutorial and instead of a basic/simple c program, use C++ program.

g++ file_name.cpp -o fun2
./fun2

Join the Conversation

25 Comments

  1. There’s a little change, in C++ code..for compiling the program using g++.
    #include
    int main()
    {
    /* Don’t take it seriously, it’s just for Fun! */
    printf(“Have Fun!\n”);
    return(0);
    }

    1. hi it is showing that  “ba.cpp:1:24: fatal error: iostream.h=””: No such file or directory”

  2. hi friends…i am a new user of ubuntu11.04 version…i installed it with in the windows7…when i am writing c program in “vi editor”” some keys are not working( e.g. direction arrows and backspace)..how to overcome this problem…please help me

    1. vi has various commands for navigation.
      Arrow based navigation or custom navigations (not the core vi navigations). All vi editor will support h,j,k,l for navigation. Use the keys and you will get it. there are many more navigation support in vi to navigate page up, down half top of page etc. details you can refer in man vi.

  3. hey frnz.. !!
    i have a bit of a problem.
    as soon as i execute ./fun1 after compiling the code under the directed procedure above i get the error msg as

    bash: ./fun1: Permission denied

    please help me wid the situation . !!

    1. Relax Mr. bhuvnesh singh, it’s not a big problem, just use this command (to make the file executable) before running ./fun –
      chmod +x fun1

    2. Relax Mr. bhuvnesh singh, it’s not a big problem, just use this command (to make the file executable) before running ./fun –
      chmod +x fun1

  4. Hi …. i’m using ubuntu 10.10 i wanted to compile my c++ program bt the thing is it asking to install g++ compiler i installed by using the command line nw while installing it is showing a box with ttf-mscorefonts-installer watt does it mean???? can some body suggest
     

  5. fun.c:2:11: error: ‘::main’ must return ‘int’
    this is the error I find when try C++

  6. hellooo….
    my terminal gives me this error when i execute my compiled program

    mohit@mohits:~$ gcc fi.c -o fi
    mohit@mohits:~$ fi
    bash: syntax error near unexpected token `fi’
    mohit@mohits:~$

  7. im unable to use the arrow key in editor it gives sum thing like A B C D. please tell me how to use it

  8. Hi! I am new to C programming and Ubuntu. Can anybody please help me with an issue I am having with a C program in Ubuntu? I developed a C program using Xcode and it runs perfectly on OS X, however when I run the same program on Ubuntu 12.04 the output of the program is correct with the exception that it is preceded by some unexpected characters. This is what the program does: reads two text files containing sorted numbers and merges them into a single new file. Then, sorts the numbers in the resulting new file. The resulting new file, when the program is run in OS X, contains:

    1
    2
    3
    4
    5
    6
    7

    when I run the program in Ubuntu 12.04, I get:

    >>
    ?
    ?
    y
    y
    i
    1
    2
    3
    4
    5
    6
    7

    Any ideas why the characters are being added? Thanks so much,

Leave a comment

Leave a Reply to Anonymous Cancel reply

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