Friday, February 5, 2010

You're Lazy And I Love It


Ubuntu ftw ^

In an ideal world you'd all have downloaded Code::blocks by now and tried to have a fiddle but, from past experiences (namely my own) I'm over 9000% sure that this is not the case. This doesn't really bother me but, our evil dictator Benjamin Aristotle has higher expectations of this year's intake and believes that without prompting, you'll all go here some time this week. Luckily I'm slightly nicer than him and have decided to take some time out of my busy schedule to point you in the right direction. Ignore my advice at the risk of Velociraptors and Ben. Or both. I'm still deciding what's worse.

In this post I'll be covering how to install an editor/IDE and how to write/compile code.

WINBLOWS (aka windows)
Just download the one file (the MinGW release). Run it. ????. Profit.
http://www.codeblocks.org/downloads/5

Mac OSX
You have two options: download codeblocks, or use Xcode. I suggest you use Code::blocks for the time being as Xcode can be a bit fiddly at the start (but is SOO worth it due to gargantuan amounts of sexiness).

http://www.codeblocks.org/downloads/5#mac
First, create a folder on your desktop called Programming. Next, download Code::Blocks from the link above. Due to the nature of Macs, compiling on OSX is a little different to how you'd do it on Windows; you write the code in an editor and you compile (run) your code through terminal with a separate compiler.

After installing Code:blocks and writing your program and saving it in your programming folder, open up terminal and copy paste the following:

cd ~/Desktop/Programming

g++ ProgramName.cpp -o ProgramName.app -O3 && ./ProgramName.app

Let me break that down for you.



(cd = change directory)
The first line changes the 'directory' which is basically where your computer is looking. By default, terminal opens at /Users/YourName/ which is the current user's Home folder (see above). In my case this is /Users/James/. /Users/YourName/ contains most of your files and all the main folders such as 'Desktop', 'Documents', 'Pictures' etc. ~ Is an extension which can be used to refer to the current user's home folder. So instead, the first line could read like this:

cd ~/Desktop/Programming


Since you didn't save your program in /Users/YourName/, you must tell your computer where to look instead by changing the directory. In this case is /Users/YourName/Desktop/Programming.

The second line is what will actually run your code. It calls upon g++ ,which is a compiler (what translates your code into something readable by the computer), and tells it to run your program. Because you're a Mac user (respect), you can only run .app files. -o ProgramName.app basically saves a copy of your code as a .app file. As a result, you will notice a number a few extra files for each program you have compiled. Finally, the && ./ProgramName.app runs the app file.

Linux
Mostly the same deal as the Mac; write your code in an editor and then compile it with a compiler (such as GNU G++).

To start, you'll probably want a decent code editor. You might have gedit or a similar text editor to start with, but to be honest, it's like coding with notepad.

Instead, open up terminal. (Either Applications->Accessories->Terminal, or hit Alt+F2 and type in gnome-terminal for Ubuntu, konsole for any KDE release or find your own equivalent).
From here:
sudo apt-get install konsole kate g++ build-essential.

This will install Kate and its in-built terminal, konsole, as well as install g++ and the basic gcc compiler.
From here, open up kate and code there. You might want to create a directory on your desktop to save all your code in, too. Let's call this directory "Programming" (right-click your destop, New Folder, etc. etc. to create this folder)

Once you've coded something up, open up either the konsole from kate or gnome-terminal from Alt+F2 or any other terminal you can find.
cd ~/Desktop/Programming/
This will change the directory terminal is working in to the current user's Programming folder, which was found inside the Desktop (as stated above, ~ is an extension for the current user's home folder - so my username is "ben", thus ~ expands to /home/ben)
Finally, g++ sourcecode.cpp -o sourcecode && ./sourcecode.
This uses g++ to turn your sourcecode.cpp file into a program named "sourcecode" and then run it. Of course, g++ has various options that it can take - for instance, you can optimise your code with -O2 or -O3 or -O1 or -Osize, or turn warnings on with -Wall, or turn warnings completely off with -w.
It would be a good idea to type in man gcc and read them all.

Any questions? Send me an email or IM me at butler_j@live.com or Ben at bgbnbigben@hotmail.com.

James.


Note: Make sure that you save your programs with the .cpp file extension. 10 points for whoever guesses what that does.

No comments:

Post a Comment