Nay a nordic nerd nor a nemesis to the novus-ordum; I merely am a noble nexus to a nomadic nous;
and I nominate no claim to be normal, neither notably nonpareil.
Nevertheless, notwithstanding the noxious nod of the nocturnal noir, my notions shall remain nubile;
and you can call me "N".
Catching Elephant is a theme by Andy Taylor
[This post is intended mainly for my classmates who are finding difficulty in the migration to GCC compiler from the Turbo C compiler they’re used to; as the OS Programming Lab has to be carried out in a POSIX environment. And a note to Linux geeks, pardon me, that’s bare basic stuff I’m talking about, but this post, like mentioned, is intended for total newbies]
Introduction
The biggest question in the mind of a learner on migrating to Linux would be, “so where exactly is my IDE, where I can type, compile and run the program and have my output served on a plate?”. On a barebone Linux system, like for instance, you’d find in your college computer labs, there won’t probably be an IDE installed. This means that you will have to master a few tricks, juggle back and forth among a couple programs to get your task accomplished.
If you have Linux installed on your home PC, get a good IDE (my personal favorite is Eclipse IDE) and install it - and you will be back to the type-compile-run bliss once again. Without an IDE, you’ll have to type your program in a text editor; and compile and run it from a command line, which we’ll explain in a short while.
Coding C for GCC - The Differences
The exact code that you write for a Turbo compiler won’t probably work on GCC - that’s not GCC’s fault - GCC confirms, to each nail and raft, to the ANSI C specifications, while Turbo C does not - it’s an outdated and nonstandard compiler, which is at times, buggy too. The lecture aside, the following things are what you should keep in mind while coding for GCC:
That’s pretty much to care about if you are coding primarily in C - the list is slightly longer in case of C++, which is omitted here for the sake of brevity.
Coding, Compiling and Running the Program
So now we have the basics, it’s time to learn how to actually go about coding and getting a program to work. Firstly, you need to open your text editor - mind you, text editors in Linux are not mundane notepads - most of them offer features syntax highlighting and multi-tab editing; so that you won’t miss your IDE much.
Open your “Applications” menu - look for “Text Editor”, which will most probably be residing in the “Accessories” or “System Tools” sub-menu. Open it. Once you have the editor window, save your file with a “.c” extension - this is the easiest way to tell the editor that you are working with a C file; so that it can active the syntax-highlighting scheme for C. Type your full program, and save your file - don’t change the default location (ie, your Home folder), it would be easier to compile if you don’t. Assume your file name was “foobar.c”.
Once you are done, its time to compile your program. To do this, open terminal. Look for “Terminal” or “Terminal Emulator” or something similar in “System Tools” or a similar sounding menu. Open it.
Type
gcc foobar.c
and press enter. (If you saved the file some place else other than the Home folder, you might have to replace foobar.c with the full qualified path to the file) If the program you typed has no errors, it should compile successfully, else GCC will display some error messages. In the latter case, correct whatever errors you find and try again. If successfully compiled, GCC will generate no message whatsoever, your cursor will simply skip to the next line.
So you have the program - to execute it, enter the following command:
./a.out
[Yes, dot-slash-a-dot-out] If done correctly, the program shall now execute and produce the output you wanted. Bingo! You’ve successfully coded, compiled and executed your first program in Linux.
Bonus Material
In case you are not terribly intimidated with keyboard shortcuts and weird-looking commands, here’s how you can accomplish the above tasks with ease: