Search

Wednesday, January 7, 2009

Compile C++ code in ubuntu

One of many question of user who switch from window to linux to have how to compile c/c++ source code. Most of study c or c++ at school or home an are usually use windows.

I think simple to start compile c/c++ in ubuntu to use first an editor like nano and create a source file then compile using gcc in command line. but first to install gcc complier and survival other utilities for compile software. use .

sodu apt-get install build-essential

build-essential is a meta package - a package which only depends on other packages, so installing it will automatically install several tools like gcc, g++ or make.

Next, create your source file using a text editor of choice (I used Nano for this example):

nano main.c

Enter the content, e.g.:

#include

int main ()
{
printf ("Hello, world!\n");
return 0;
}


Notice that I also included a newline after the close bracket, otherwise the compiler will issue a warning. Save it with CTRL+O, then exit Nano using CTRL+X. To compile your source, simply use:

gcc main.c -o myapp

The output, myapp, will automatically be executable, so to run it use:

ubuntu@ubuntu:~$ ./myapp
Hello, world!

This is the simplest way of creating and compiling C or C++ code.

No comments: