Sunday, August 29, 2010

Coding in C++ on Ubuntu Walk-through

I've been using Ubuntu as my main operating system for a month now. I haven't practiced coding for a whole month at least. I'm bad now. I didn't even remember exactly what to 'include' for a simple Hello World app. I'm still getting used to Ubuntu. I'm learning something new about Ubuntu everyday and just a couple days ago I learned how to make an app on Ubuntu using just the terminal and a text editor and C++.

step one: open the terminal

step two: mkdir ~/programs

Type mkdir ~/programs into the the terminal to create a folder or 'directory' called 'programs', in your 'home directory'. mkdir
stands for 'make directory'.

step three: cd ~/programs

Type cd ~/programs
to change the directory your terminal is referring to from your 'home folder' to your new 'programs' directory where you will write the code for your program.

step four: g++ --version

Type g++ -- version to make sure you have the GNU C++ compiler

g++ (Ubuntu 4.4.3-4ubuntu5) 4.4.3
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
The message above is the response I get from my terminal after I enter the command g++ --version. I believe this response indicates that I do have the GNU C++ compiler.

step five: Write the code

Now you have to open a text editor and write the code for your app. For my first app I wrote the code for "Hello World", simple text output. You have to save the text file with the .cpp extension or the G++ compiler may not recognize the source code file. Save the source code in the 'programs' directory you created.

step six: g++ -o hello_world hello_world.cpp

This command will compile the hello_world.cpp file to an executable called hello_world in the same directory.

step seven: Run your program with ./hello_world

./ means 'in current directory'. If you copy pasted or wrote the code yourself with no errors then the program should say "hello world" in the terminal. In Ubuntu you don't have to worry about the terminal shutting down as you have to in Windows.




No comments: