lab 1: first steps in c++ - eclipse - information ... · lab 1 semester 1, 2011 dr. markus lumpe 1...

21
Lab 1 Semester 1, 2011 Dr. Markus Lumpe 1 Lab 1: First Steps in C++ - Eclipse Step Zero: Select workspace 1. Upon launching eclipse, we are ask to chose a workspace: 2. We select a new workspace directory (e.g., “C:\Courses”): 3. We accept the selected directory as workspace location:

Upload: duongnhu

Post on 22-May-2018

213 views

Category:

Documents


1 download

TRANSCRIPT

Lab 1 Semester 1, 2011 Dr. Markus Lumpe

1

Lab 1: First Steps in C++ - Eclipse

Step Zero: Select workspace 1. Upon launching eclipse, we are ask to chose a workspace:

2. We select a new workspace directory (e.g., “C:\Courses”):

3. We accept the selected directory as workspace location:

Lab 1 Semester 1, 2011 Dr. Markus Lumpe

2

4. Now eclipse will start:

5. We see this screen and have to enter the workspace:

6. We are now ready to use the eclipse workbench:

Lab 1 Semester 1, 2011 Dr. Markus Lumpe

3

First Step: The main function 1. We create a new C++-Project named ‘FirstStep’:

To keep things simple, we always select “Empty Project” and “GCC” (i.e., MinGW on Windows). This way we create an empty project – we are in complete control!

2. We always visit the Application Settings! In the labs and assignments, we use the Debug configuration:

Lab 1 Semester 1, 2011 Dr. Markus Lumpe

4

3. You can also visit the Advanced Property Settings, if you wish to tinker with the project configurations (it is, in general, not necessary):

4. Once you selected “Finish”, the result should look like this:

Lab 1 Semester 1, 2011 Dr. Markus Lumpe

5

5. Now we add a New Source File:

6. We select Default C++ source template and type main.cpp as the name for the

source file:

Lab 1 Semester 1, 2011 Dr. Markus Lumpe

6

7. Our first program:

We write:

#include <iostream> Include the definitions for console I/O. We are using two I/O values: cout (standard output) and endl (the environment-specific newline).

using namespace std; Tell C++ to look for all library names in namespace std.

int main()

{

cout << “Hello World!” << endl;

return 0;

}

Our program just prints the string “Hello World!” to the screen.

At the end of the main function we return the value 0 – success – to the operating system (i.e., Windows)

Lab 1 Semester 1, 2011 Dr. Markus Lumpe

7

8. To run the program, we need to create a new Run Configuration:

If the current project is not automatically selected, then just browse the workspace and select (search) the project to run.

Press Run and watch the Console window of eclipse.

Lab 1 Semester 1, 2011 Dr. Markus Lumpe

8

9. We extend our main function to print the command line arguments to the screen. For this reason, we add int argc and char * argv[] as arguments to main. By convention, argc contains the number of arguments, that is the number of elements in argv. The parameter argv, on the other hand, contains an array of C-Strings (i.e., char arrays terminated with ‘\0’). The element at index 0 is always the name of the command. We use a simple for-loop to iterate over the argv-array and print each element in turn.

10. We need to tell the Run Configuration that we would like to attach some

Command Arguments:

Lab 1 Semester 1, 2011 Dr. Markus Lumpe

9

11. When we run FirstStep, then the result is as follows:

The program prints three lines, one for each argument. Remember, the first argument is always the command name. To see this screen in Visual Studio, you need to set a break point on the return statement. Otherwise, the program just runs and closes the console window immediately after the main functions terminates.

Lab 1 Semester 1, 2011 Dr. Markus Lumpe

10

Second Step: Hello World Console Application

Repeat 1-3 of First Step, but use ‘SecondStep’ as the project name.

4. Now we define a new class Hello. First, we select New Header File:

We select the Default C++ header template and called the header Hello.h. Please note that there is no one-to-one relationship between class name and file name in C++ as in Java. We will see later that one usually groups many related classes in one header, one .cpp file, or both.

Lab 1 Semester 1, 2011 Dr. Markus Lumpe

11

5. Class specification – Hello.h:

The first lines tell the GCC-C++ compiler to include the Hello header-file only once. The include mechanism of C++ is based on source code inclusion, that is, the code is copied into the including compilation unit. To prevent that the same code is included twice, which would lead to a compilation error, the programmer is required to take the necessary steps.

To give our Hello class a specify function, we add the private instance variable fMessage of type std::string, change the constructor to accept a parameter aMessage of type std::string, and add a void Display() method. The type std::string is defined in the include file string. The type string is defined in namespace std. For this reason we write std::string. In header file we never use the using namespace declaration. Therefore, we need to refer to string by its fully qualified name std::string.

Lab 1 Semester 1, 2011 Dr. Markus Lumpe

12

6. The Hello.cpp file:

We need to create a new source file.

In Hello.cpp we define the behavior of class Hello, that is, we include “Hello.h” and iostream, declare std as standard namespace for library lookup, and define the bodies of the constructor and the method Display. The body of the destructor ~Hello() remains empty (we are not using it just yet). When defining the member functions, the name of the member function always appears as fully qualified name. We write Hello::Hello for the constructor, Hello::~Hello for the destructor, and Hello::Display for the Display method.

The program should compile, but since we have not yet defined a main function the compiler will report a linker error (undefined reference).

Lab 1 Semester 1, 2011 Dr. Markus Lumpe

13

7. The main function:

In main we just check whether command line arguments given been given and initialize the local variable lMessage accordingly. Using lMessage, we declare object variable o of type Hello and call its Display method.

8. We create a new Run Configuration for SecondStep and run the program:

Lab 1 Semester 1, 2011 Dr. Markus Lumpe

14

We enclose the argument to SecondStep in quotes: “Hello World!”, so that it becomes a single command line argument.

Lab 1 Semester 1, 2011 Dr. Markus Lumpe

15

Third Step: Hello Class in Shared Library 1. We create a new Shared Library project named ‘ThirdStep’:

As usual, we create an empty project to have complete control.

2. We add the Hello class to the project. The code is the same as in SecondStep, except that we need to remove the main function. Libraries do not have a main function!

The compilation of the shared library (here a DLL, on Linux we shared libraries have file extension .so) should succeed with no problems.

Lab 1 Semester 1, 2011 Dr. Markus Lumpe

16

3. A Console Application that uses the shared library ThirdTest.

We add a New Project to the current workspace called UseThirdStep:

Again, we create an Empty Project for a Console application.

4. Our console application requires the shared library ThirdStep to function. We therefore have to add a proper configuration:

a. Open the Properties page for the current project, select C/C++ Build entry Settings, got to C++ Compiler Directories, and click on Add:

Lab 1 Semester 1, 2011 Dr. Markus Lumpe

17

b. Type ThirdStep add click OK:

This will add a flag to the C++ compile process to tell the C++ linker the name of the shared library.

c. We now add the Library search path:

Lab 1 Semester 1, 2011 Dr. Markus Lumpe

18

Select Workspace… to proceed and locate the Debug directory of ThirdStep. The actual image of the shared library currently resides in the Debug directory. Once you deploy the shared library you will either store it in a system-wide shared library directory, with a program using it, or both.

Lab 1 Semester 1, 2011 Dr. Markus Lumpe

19

5. Add main.cpp to the project (main is the same as in SecondStep) and compile:

If everything is set up correctly, the compilation should succeed without any problems.

5. How do we run UseThirdStep? We create a corresponding Run Configuration with proper arguments. But this time, we also have to add an Environment variable:

Lab 1 Semester 1, 2011 Dr. Markus Lumpe

20

The required environment variable is PATH (or LD_LIBRARY_PATH for Linux). This variable tells the runtime loader where to look for shared libraries a current program is referring to.

Type PATH (or LD_LIBRARY_PATH) in the Name field and click on Variables...

Select workspace_loc in list of eclipse variables and type ThirdStep/Debug as argument:

Once completed, press OK.

Lab 1 Semester 1, 2011 Dr. Markus Lumpe

21

The new variable should appear in the list of environment variables.

6. Run UseThirdStep. Success!