Lab 1: First Steps in C++ - Eclipse

Similar documents
CS2141 Software Development using C/C++ Compiling a C++ Program

CS 376b Computer Vision

My First Command-Line Program

Lab 1: Introduction to C Programming. (Creating a program using the Microsoft developer Studio, Compiling and Linking)

CSI33 Data Structures

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

Object Oriented Design

IDE: Integrated Development Environment

CE221 Programming in C++ Part 1 Introduction

C++ Lab 03 - C++ Functions

Laboratorio di Programmazione. Prof. Marco Bertini

PROGRAMMING IN C++ CVIČENÍ

Linked List using a Sentinel

Laboratorio di Tecnologie dell'informazione

Using Eclipse for C, MPI, and Suzaku

Eclipse CDT Tutorial. Eclipse CDT Homepage: Tutorial written by: James D Aniello

C++ Programming for Non-C Programmers. Supplement

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

What will happen if we try to compile, link and run this program? Do you have any comments to the code?

Lab: Supplying Inputs to Programs

Building And Integrating CppUnitLite in Eclipse on Linux

y

A brief introduction to C++

Getting Started with Eclipse/Java

Module 3: Working with C/C++

C++ Support Classes (Data and Variables)

Lab 8. Follow along with your TA as they demo GDB. Make sure you understand all of the commands, how and when to use them.

Linux Tutorial #1. Introduction. Login to a remote Linux machine. Using vim to create and edit C++ programs

Your First C++ Program. September 1, 2010

ICOM 4015 Advanced Programming Laboratory. Chapter 1 Introduction to Eclipse, Java and JUnit

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Check the Desktop development with C++ in the install options. You may want to take 15 minutes to try the Hello World C++ tutorial:

Understanding main() function Input/Output Streams

CSE 333 Lecture 9 - intro to C++

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Chapter 1 INTRODUCTION

Programmazione. Prof. Marco Bertini

Starting to Program in C++ (Basics & I/O)

Getting Started with Visual Studio

Separate Compilation of Multi-File Programs

CSCE 110 PROGRAMMING FUNDAMENTALS

CS240: Programming in C

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

Short Notes of CS201

Cpt S 122 Data Structures. Introduction to C++ Part II

CS201 - Introduction to Programming Glossary By

Laboratorio di Tecnologie dell'informazione

Polymorphism Part 1 1

A A B U n i v e r s i t y

ANSI C. Data Analysis in Geophysics Demián D. Gómez November 2013

CS93SI Handout 04 Spring 2006 Apr Review Answers

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

assembler Machine Code Object Files linker Executable File

CS520 Setting Up the Programming Environment for Windows Suresh Kalathur. For Windows users, download the Java8 SDK as shown below.

Use the dot operator to access a member of a specific object.

Introduction. Key features and lab exercises to familiarize new users to the Visual environment

The University of Nottingham

Compiling with Multiple Files The Importance of Debugging CS 16: Solving Problems with Computers I Lecture #7

CS2141 Software Development using C/C++ C++ Basics

Unified Modeling Language a case study

Implementing an ADT with a Class

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

HOW TO USE CODE::BLOCKS IDE FOR COMPUTER PROGRAMMING LABORATORY SESSIONS

CSCE 206: Structured Programming in C++

ECE 462 Object-Oriented Programming using C++ and Java Design Issues and Multiple Inheritance in C++

Laboratory Assignment #3 Eclipse CDT

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

UEE1302 (1102) F10: Introduction to Computers and Programming

Deadline. Purpose. How to submit. Important notes. CS Homework 9. CS Homework 9 p :59 pm on Friday, April 7, 2017

Reliable C++ development - session 1: From C to C++ (and some C++ features)

Lab 2 - Introduction to C++

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

C++ Code Structure. Cooperating with the Compiler

CSE 333. Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington

Common Misunderstandings from Exam 1 Material

CPSC 427: Object-Oriented Programming

Code::Blocks Student Manual

Code::Blocks Student Manual

CS11 Intro C++ Spring 2018 Lecture 1

CPSC 427: Object-Oriented Programming

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

Cours de C++ Introduction

Program template-smart-pointers-again.cc

clicking on the on the New

CS 11 C++ track: lecture 1

A506 / C201 Computer Programming II Placement Exam Sample Questions. For each of the following, choose the most appropriate answer (2pts each).

Exercise 1.1 Hello world

PIC 10A Objects/Classes

การทดลองท 8_2 Editor Buffer Array Implementation

Lab 2: ADT Design & Implementation

esi-risc Development Suite Getting Started Guide

1) Log on to the computer using your PU net ID and password.

CS 216 Fall 2007 Midterm 1 Page 1 of 10 Name: ID:

21. Exceptions. Advanced Concepts: // exceptions #include <iostream> using namespace std;

Program template-smart-pointers.cc

Using the Xcode Debugger

Introducing C++ to Java Programmers

4. Structure of a C++ program

Transcription:

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: 1

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: 2

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: 3

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: 4

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: 5

7. Our first program: We write: #include <iostream> using namespace std; int main() { cout << Hello World! << endl; return 0; } Include the definitions for console I/O. We are using two I/O values: cout (standard output) and endl (the environment-specific newline). Tell C++ to look for all library names in namespace std. 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) 6

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. 7

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: 8

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. 9

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. 10

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. 11

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). 12

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: 13

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

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. 15

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: 16

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: 17

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. 18

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: 19

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. 20

The new variable should appear in the list of environment variables. 6. Run UseThirdStep. Success! 21