Advertisement

Friday 27 July 2012

How to start a C++ project in Linux/emacs environment?


How to start a C++ project in Linux/emacs environment?

1)  If it is the first time that you have logged in to your CS account, you can add a button to your task bar (bar on the bottom of the desktop) that will open up a new terminal when you left click it. To do this, right click on the task bar and select "Add to Panel->Launcher from menu->System Tools->Terminal".
     OR, you can also open a terminal by pressing alt-F2 to open the "Run Command" dialog box.  Type the word "konsole" in the command line, and click "run" to start the terminal.

2)  If you were able to find the path "Add to Panel->Launcher from menu->System Tools->Terminal" on your computer, the Terminal button should appear on your task bar from now on, every time you log in to your account (otherwise you will have to use the alt-F2 method from above).  If you added the button to your task bar, open a terminal by left clicking the button you added, if you used alt-F2, then the konsole should already be open as pictured below.  

open_terminal



3)  You will manage your files and directories from a Terminal (also called a Shell) command line.  The command line has the form "username@host $ ".  Each time you start a new project, you should create a new directory to put your files in.  To do this, type "mkdir folder_name" on the command line.  For example, to create a directory called tutorial, you would type "mkdir tutorial" and press enter, go ahead and do this.

make_directory



4)  To look at the contents of the current directory, use the command "ls".  To see what directory you are currently in (the present working directory), use the command "pwd".  In the screenshot below, the directory contains a file called add_terminal.png, a file called open_konsole.png, and several directories (note that the contents of your home directory will be different than mine).  Notice that the name of one of the directories is tutorial.  This directory was created when the command mkdir was used in step 3 above.  The directory that I am currently working in is "/home/csgrads/mfast".

ls_and_pwd



5)  Now you will need to descend into the directory that you created.  To do this you will use the change directory command "cd".  On the command line type "cd tutorial".  If you type "pwd" you will see that you're now in the directory "/home/csmajs/username/tutorial".

cd_descend



5)  If you want to move up (ascend) out of a directory you are currently in, you can use the command "cd ..".  Type "cd ..", and then "pwd" to confirm that you have changed back to the "/home/csmajs/username" directory.  Once you have confirmed your new directory with pwd, cd back into tutorial (/home/csmajs/username/tutorial).  Now let's create a file that we can write a program in, and call that file "main.cpp" (.cpp means C++ file).  We will use an editor called emacs, type "emacs main.cpp &" and press enter.

emacs



6)  It is very important that you include the "&" symbol after "emacs main.cpp", if you don't do this, you won't be able to use the Terminal at the same time that you are using emacs.  Now let's write a simple hello world program.  In the emacs editor, type the following program:

#include<iostream>

using namespace std;

int main(){
    cout << "hello world!\n";
    return 0;
}

Once you have finished writing this program, click the picture of the disk (or click File->Save (current buffer)) to save your work.

emacs_save



7)  Now it's time to compile our program!  To do that, click just once anywhere on the Terminal to make it the active window, and type the command "g++ main.cpp"
 on the command line.  If you wrote the program correctly you won't get any error messages, and a new file will be created called "a.out".  You can type "ls" to confirm that the a.out file was created.

g++



8)  Now it's time to run our program, which will be awesome.  All you have to do is type "a.out".  After typing a.out, you should see the message "hello world!" printed to the command line.

hello_world



9)  Bingo Bango, you're a programmer!  Try changing the message in your program to something else (maybe "I rock at programming!\n").  Save your new program, and again type "g++ main.cpp" on the command line.  Type "a.out" and see if your new message prints to the command line.

I_Rock

10)  Alright!  Now lets make our home directory a little more organized.  We want to create a directory just for this class, and inside of that directory, we want subdirectorys for each programming assignment that we do.  So lets ascend up one level to to our home directory by typing "cd ..".  Now make a directory called cs10, just type "mkdir cs10".  After these two steps, type "ls" to verify that you created the directory.  You should see both the tutorial directory, and now you should also see a cs10 directory.

made cs10 directory

11)  Now descend into the cs10 directory, and make another directory called "lab_1" from inside cs10.  Now descend into the lab_1 directory.  If you type pwd, you will see that you are now in the directory "/home/csmajs/username/cs10/lab_1".  If the path you have is different, ask for help from your TA (it's OK if the only difference you have is that it says something other than "csmajs" i.e. /home/blah/username/cs10/lab_1).

made lab_1 directory

12)  We want to write another program, so lets close the emacs window that we were using to write our first program.  Just click the "X" shaped button in the top right corner of the emacs window (be careful you don't close this tutorial by clicking the X in the browser window!).  Now from inside the lab_1 directory, go though the same steps above to write and compile a program.  The program you will write this time will be just like the one above, but this time put your own creative message to print to the terminal.  The main idea for this step, is that now we are saving our program in a different location in the memory hierarchy.  Once you have finished your new program, refer to the directions in the lab 1 description, part 3, to turn in your code using electronic turn-in.


NOTE :The above data is extracted from another EDUCATIONAL WEBSITE .