Sie sind auf Seite 1von 33

Linux & C/C++ Introduction

CSE 3300 Spring 2013

TA: Haining Mo

Log in & out


To log in from Windows machines:
http://www.engr.uconn.edu/ecs/sshlin.htm Download putty for windows

To log in from Linux machines:


ssh username@fester.engr.uconn.edu

To log out from anywhere:


Type exit

GUI access
Download WinSCP

So lets talk about Putty and WinSCP first.

MS Windows Users
May code on Windows machines. But Need to test/run programs remotely on Linux machines. Students will need the following:
PuTTY SSH client
http://www.chiark.greenend.org.uk/~sgtatham/putty/ Use it to compile and run your code Use the Linux commands from previous slides

WinSCP GUI file access


http://winscp.net/eng/index.php Use it to move files to and from the Linux server
3

PuTTY
This is what you see when you open putty Dont worry about all the other stuff Just need to know Host Name (or IP address)
For Linux server, its fester.engr.uconn.edu

Keep everything else the same Save the session if you want so you dont have to keep typing in the host name Click Open

PuTTY
You will most likely (or definitely) see this if this is the first time accessing the server. Just click Yes

PuTTY
Lastly, just enter your SoE username and password (the same ones you use to log into the computers in the ITEB and E2 lab). Now, you can run Linux commands from your Windows machine to compile and run your code
6

WinSCP
This is what you see when you open WinSCP (similar to putty actually) Again dont worry about all the other stuff Just need to know Host Name (or IP address)
Again, its fester.engr.uconn.edu

Type in your username (required) Type in your password (optional) Keep everything else the same Save the session if you want so you dont have to keep typing in the host name Click Login
7

WinSCP

Like in putty, you will most likely (or definitely) see the above warning if this is the first time accessing the server. Just click Yes, then enter your password (as shown to the right) if necessary
8

WinSCP
Now, you can access the files and directories on the Linux server like using a Windows Explorer

Can create new files and folders (right click-> new, etc) Drag-and-drop files and folders to and from your Windows machine to the Linux server just like you would between 2 Windows folders

WinSCP
**Quick note: if you ever get a permission denied or access denied error, it means you need to change the permissions on the files on the Linux server Just right-click->properties the file Under Permissions:
R = read W = write X = execute

Just check whatever is appropriate


10

Basic Utilities
In Unix family everything is a file Directory is a file and also a collection of files
Its like a folder in Windows

Everything is done in the terminal


Like the command prompt in windows

Basic commands to know: pwd present working directory


Shows the path of the current folder

ls list the contents of the directory


Shows whats in the current folder
11

Basic Utilities
mkdir create a directory (rmdir deletes)
mkdir [new_directory_name]

cd Change directory
cd [directory_name] (go to specified directory) cd .. (go to previous directory) cd /home/<username> (goes back to main directory)

Creation of a file:
touch [filename] (creates a new empty file) rm [filename] (removes a file) vi [filename] (open file with vi text editor)

12

Basic Utilities
Compile source code into executables
Commands are given later in the C++ section

Run a program: ./[executable] [args]

Example Later

13

Additional Utilities
Many commands also have additional options
Ex: ls -l (more detailed listing of contents)

View a file : cat ,less, more etc User info : who, whoami, finger etc Process info : ps, kill, fg, &, etc Search utilities: grep, find, locate etc Many more commands
http://www.oreillynet.com/linux/cmd/ http://ss64.com/bash/ Google!

14

C++ programming
Great reference for looking up classes, functions, etc
http://www.cplusplus.com/reference/

In general, need to keep in mind pointers, references, memory locations, etc

15

Pointers
* signifies a pointer
type * name declares a pointer variable
type = type of the data that the pointer is pointing to Position of * doesnt matter
type* name == type *name == type * name

Ex: int* ptr = a pointer to an integer

& signifies the reference (address of) something


Ex: &a = gets the address of variable a

int x; // integer x int* ptr = &x; // pointer to address of x * also used to dereference a pointer (to read the value that the pointer is pointing to)
Ex: int j = *ptr; // j = integer x (from above)
16

Pointers
Pointers and arrays:
The arrays identifier is equivalent to the address of its first element
int numbers [20]; int * p;

Arrays are constant pointers, so they cannot be changed


p = numbers; numbers = p; x = pointer to the array x[0] = h, x[1] = e, etc // valid // NOT valid

char *x = hello; // pointer to character array


17

C++ programming
Refresher from CSE 123
Primitive types (int, float, char, bool) are passed into functions by value
A copy is passed into the function

Others are passed into functions by reference


The address value is passed into the function

Ex:
function(int i) { i++ } // function takes in a value and increments it within the function int a = 2; // as value is 2 function(a); // value is incremented // a is still 2 // a is not affected afterwards

function(char * c) { c[0] = a }
Char* txt = street; function(txt);

// function takes in a pointer to a character (array) and changes the first character // txt points to string street // value is incremented // txt now points to string atreet 18

Arguments for main()


int main (int argc, char* argv[ ]) //optional to
mention parameters argc = number of arguments argv = list of arguments (array)
argv[0] = program name

Example: client h http://www.engr.uconn.edu/html argc = 3 argv[0] = client argv[1] = -h


19

File Access
Theres 2 (and possibly more) ways to open a file for processing Method 1:
FILE * inFile; inFile = fopen(<filename>, "r"); if(inFile == NULL) { error } while(!feof(inFile)) { do stuff } fclose (inFile); Similar steps for writing to file
20

File Access
Method 2:
ifstream inputFile; inputFile.open(<filename>); if(inputFile.is_open()) { while(!traceFile.eof()) { do stuff } } inputFile.close(); Need to keep track of streampos (current position of the pointer in the file)
21

String Processing
char* strcpy(char* dest, const char* src);
different from dest = src (because that would be copying the pointers, not the strings)
src = hello; dest[64] strcpy(dest, src); //dest = hello;

int strcmp(const char* s1, const char* s2);


s1 == s2; s1<s2; s1>s2
A value greater than zero indicates that the first character that does not match has a greater value in str1 than in str2. And a value less than zero indicates the opposite.

If s1 = hello; s2 = world; strcmp(s1,s2) < 0; If s1 = hello; s2 = hello; strcmp(s1,s2) == 0;

22

String Processing Example

23

More String Processing


char *strchr(const char *s, int c);
char s1[10]; // character array strcpy(s1,husky); // copy husky into s1 p = strchr(s1, u); // husky, so p =usky; p = pointer to the u in husky

char *strstr(const char *haystack, const char *needle);


s1=husky s2=sky p = strstr(s1,s2); //husky, so p = sky p = pointer to sky in husky
24

String Processing
One good way to print a string
#include <stdio.h> printf( const char *format, ); Ex. s1: Dan:9000.1:123
char name[5]; float money = 9000.01; int ID = 123; strcpy(name, Dan); // copy Dan into name printf(My name is %s, ID = %d. I have $%f., name, ID, money);
This will print My name is Dan, ID = 123. I have $9000.01.
25

Basic Code structure


.c / .cpp files (source code) .h = header files (source code)
Write all your code in the source code files

.o = object files (object code)

Compiler = source code object code Linker = object code executable program

26

Compilation Commands
Gcc compiler for c, g++ for c++
gcc [file_name].c -o [executable_name] g++ [file_name].cpp -o [executable_name]

Various options, -O,-c,-o


-O sets optimization level -c only compile not link -o to create the executable (not a.out)

Examples:
g++ main.cpp -c
Creates the object file main.o

g++ main.cpp -o main


Creates the executable file main
27

Files and Compilation


Your code will compile if you correctly put everything into one file.
// #includes // classes // global variables // other stuff int main(int argc, char* argv[]) { //code }

Just run the appropriate compilation command However, the file will be very long and difficult to go through
28

Example program
Heres an example program with a lot of the concepts that were explained earlier
Copy/paste everything from ===START=== TO ===END=== (extends over multiple slides) into a file test.cpp

===START===

// compile with "g++ test.cpp -o test" // run with "./test" or "./test [additional arguments]" #include <stdio.h> #include <cstring> void function1(int* a) { // alters the contents of the array a[0] = 99; a[1] = 100; } void function2(int a) { // attempts to alter the passed-in value printf("BEFORE (inside) function2: a = %d\n",a); a += 10; printf("AFTER (inside) function2: a = %d\n",a); }

29

Example program
int main (int argc, char * argv[]) { // prints number of arguments printf("# args = %d\n", argc); // if there is an additional argument, print it if(argc > 1) printf("argument = %s\n", argv[1]); else printf("no additional arguments"); // demo on sscanf and strcpy char *s1 = "sam:9999:123"; // will generate a warning, but ignore it for now char s2[32]; strcpy(s2, s1); // copy s1 into s2 printf("s2: %s\n",s2); char name[5]; int ss; int yr; sscanf(s2,"%[^:]:%d:%d",name,&ss,&yr); printf("name: %s\n",name); printf("ss: %d\n",ss); printf("year: %d\n",yr);

30

Example program
// demo on array (pointers) and functions int list[] = {0,1,2}; printf("BEFORE function1: [%d,%d,%d]\n",list[0], list[1], list[2]); function1(list); printf("AFTER function1: [%d,%d,%d]\n",list[0], list[1], list[2]); // demo on int (primitives) and functions int i = 4; printf("BEFORE (outside) function2: i = %d\n",i); function2(i); printf("AFTER (outside) function2: i = %d\n",i); // demo on pointers, referencing, and dereferencing int *ptr = &i; // create a pointer to int i i++; int j = *ptr; // save in j, the value pointed to by ptr printf("j = %d\n",j); // printf("value pointed to by ptr: %d\n",*ptr); printf("value of ptr (an address): %p\n",ptr);

// comment out either of the following to test int * ptr2 = list; // generates no error //list = ptr; // generates error (because list is a const pointer to array) printf("ptr2: %d\n",ptr2[0]);
return 0; }

===END===

31

Overall Summary
So this is the summary of what some users might do if they needed to write code in Windows but needed to compile and test it remotely on the Linux server:
1. Write all my code using a text editor (like notepad) 2. Open WinSCP and connect to the Linux server 3. Drag-and-drop (to copy/paste) my code from my Windows machine to the Linux server using WinSCP 4. Open PuTTY and connect to the Linux server 5. Navigate to the directory where my code is and use the appropriate commands to compile and run my code using PuTTY 6. If there are errors, make changes to the code on the Windows machine and copy/paste the new code back into the Linux server using WinSCP (overwrite previous files) 7. Repeat as needed

Yes this method is rather cumbersome but thats probably just one possible way. There are definitely others.
32

End
I hope this helps. Good luck! If you have any questions or concerns please feel free to contact me and I will try my best to help.

haining.mo@engr.uconn.edu
33

Das könnte Ihnen auch gefallen