Tag Archives: compile

c programming in ubuntu 11.10

How to Compile and Run C/C++ program on Ubuntu 11.10

Ubuntu 11.10 (Oneiric Ocelot) is one of the most popular operating system for programming because there are lot of great open source applications, tools, compilers, debuggers, IDEs are available free of cost. Some of them are  – GCC – The greatest compiler for C language (from FSF (‘Free Software Foundation’ by Stallman); Linux Torwalds used GCC while developing Linux Kernel), Eclipse IDE (The most popular ‘Integrated Development Environment’ for Java programmers), Netbeans, KDevelop, Codelite  etc.

C/C++ language is a high level programming language (although the term high and low is used in relative sense e.g C is a high level language as compared to Assembly but if we compare it with java then C is a low level programming language; the term high or low basically describes the closeness with hardware). Most of the operating systems has been written in C language. This post has been written for beginners who just started learning C/C++ or the programmers who have migrated from Windows to Ubuntu (although the commands are almost same for all Linux based operating system).

Compiling and Executing C program in Ubuntu 11.10

1. Write and save the program

c programming in ubuntu 11.10

Open a simple text editor (e.g gedit), IDE (Eclipse) or command line code editor (Nano or Vim). I’ll be using gedit as it is very simple to use and it’s recommended for beginner programmers. Right Click on Desktop or any directory (Browse File using Nautilus) and select create new File – hello.c (.c extension is used to indicate that it’s a c program). Then write a simple program like this (and save the program press Ctrl+S)

#include<stdio.h>

void main()
{
	printf("Hello! This is my first C program with Ubuntu 11.10\n");
	/* Do something more if you want */
}

2. Compile the program

GCC (GNU Compiler Collection) is installed by default, in Ubuntu. To compile the program, open the terminal and move on to the target directory type the command – (where gcc implies compiler name, then it asks for the file name of the source program while -o option specifies the file name of the output program)

gcc hello.c -o hello1

If there is no syntax/semantic error in you program then the compiler will successfully generate an executable file, otherwise fix the problem in your code.

3. Execute the program

To execute the program, you need to run –

./hello1

running a C program in Ubuntu 11.10

Compiling and Executing C++ program

The steps are almost same as above but you need to install g++ compiler, the file extension should be .cpp and in compilation phase replace gcc with g++. To install G++ compiler, execute the command –

sudo apt-get install g++

If you have any problems then share it through comments. One more thing, This video might help you in running your first C program in Ubuntu 11.10(I’ve recorded it in Gnome Shell interface) –

compiling-executing-c-program

how to compile and run C/C++ program on ubuntu 11.04

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
how to program in java on ubuntu 10.04

Java Programming on Ubuntu 10.04

Java is multi platform programming language.It uses JDK(includes jre) to create a class file from the source code and then the generated class file can be run on any computer by using Java Virtual Machine(JVM).So to write and execute java programs Java Development Kit(JDK) must be installed on the computer.

Installing JRE /JDK on Ubuntu 10.04 for java programming
If you have not installed the JDK yet, then first install jdk on ubuntu 10.04 and then return to this page.
When you have installed JDK successfully on ubuntu ,then you will be able to write and execute jav programs.Follow these simple steps to write and execute your first java program on ubuntu 10.04 –

How to compile and run java program on Ubuntu 10.04

1. Open Gedit OR Write click on Desktop and select Create Documents->Empty File.

2. Write this simple java code to test..

// A program to print some text.
public class Welcome
{
// main method begins execution of Java application
public static void main( String[] args )
{
System.out.println("Hello World!");
System.out.println("Welcome to Java Programming! on Ubuntu 10.04");

} // method main ends here
} // end of the class Welcome

3. Save it as “Welcome.java” on your Desktop(or in any other directory).
4. Now compile this java program to create the class file by using this command on terminal(CTRL+ALT+T).

$ javac Welcome.java

5. Now you will notice a class file “Welcome.class” on your Desktop.Now JVM will execute this code through the command given below –

$ java Welcome

6. Then the java Program will be execute by jvm and you will see the following output.
Hello World!
Welcome to Java Programming! on Ubuntu 10.04

how to program in java on ubuntu 10.04