Lecture 1 Course webpage http://csserver.evansville.edu/~hwang/s14-courses/cs375.html Handouts, assignments Supplemental resources Syllabus and schedule, textbooks CS Lab, Virtual Box, Cygwin Tuesday, January 14 CS 375 UNIX System Programming - Lecture 1 1 Outline What is UNIX? What is Linux? UNIX programming environment Review of compiling C and C++ programs Libraries Tuesday, January 14 CS 375 UNIX System Programming - Lecture 1 2 What is UNIX? - Features A multiuser, multitasking operating system UNIX is portable (written in C). UNIX is secure (file and process security). A UNIX system includes hundreds of utilities, tools, and libraries. Tools for text processing and programming are standard. Tuesday, January 14 CS 375 UNIX System Programming - Lecture 1 3 What is UNIX? - Features The TCP/IP protocols (Internet) were developed under UNIX and are now a core part of the OS. While not a core part of the OS, a Graphical User Interface is standard (X Window System or X). The POSIX (portable operating system interface) standards are now used to define UNIX-like systems. Standards define interfaces, shells, tools, security, etc. Tuesday, January 14 CS 375 UNIX System Programming - Lecture 1 4 What is UNIX? - History UNIX was developed by Bell Labs (AT&T) in the early 1970s. AT&T gave the source code of the OS to universities and licensed it to many other companies. (Several companies developed their own versions of UNIX.) At Berkeley, several enhancements resulted in Berkeley System Derived (BSD) UNIX. The UNIX trademark belongs to the Open Group. The SCO Group owns the original AT&T source code. Tuesday, January 14 CS 375 UNIX System Programming - Lecture 1 5 What is UNIX? - History Tuesday, January 14 CS 375 UNIX System Programming - Lecture 1 6 What is Linux? - History Linux is a free, POSIX compatible OS originally written by Linus Torvalds. It was first released in 1991. (Linus was a student in Finland at the time.) It is now maintained and updated by people around the world. The term Linux properly refers to only the kernel of the OS, but is usually used to describe the complete OS (kernel, libraries, core utilities and video support) or distribution. Tuesday, January 14 CS 375 UNIX System Programming - Lecture 1 7 What is Linux? - Features Like most modern OSes Linux supports: true multitasking, threads, virtual memory, shared libraries, demand loading, shared copy-on-write executables, memory management, loadable device driver modules, video frame buffering, and TCP/IP networking Although originally written for the Intel 80386 processor it has been ported to many other processors. Tuesday, January 14 CS 375 UNIX System Programming - Lecture 1 8 What is Linux? - Distributions There are several Linux distributions: Red Hat/Fedora, SuSE, Slackware, Arch Linux, Debian, Gentoo, Knoppix, Ubuntu, etc. A distribution consists of the complete Linux OS, plus administration and user applications. An installation program also is included. Distributions provide software in packages along with a package management program. Tuesday, January 14 CS 375 UNIX System Programming - Lecture 1 9 What is Linux? - Tux, the Mascot Tuesday, January 14 CS 375 UNIX System Programming - Lecture 1 10 UNIX Programming Environments CSLAB/csserver (recommended) CSLAB/KC267/KC136 dual-boot Linux and Windows. Also remote log on to csserver via ssh. Putty and WinSCP are free ssh and sftp Windows clients. ALL PROGRAMS MUST BE TESTED ON LAB MACHINE BEFORE SUBMISSION!!!!!!!!!!! Tuesday, January 14 CS 375 UNIX System Programming - Lecture 1 11 UNIX Programming Environments VirtualBox Virtual Machine (recommended) Run Linux in a virtual machine. Programming, network and administration Ubuntu 12.04 installation disk available from instructor Tuesday, January 14 CS 375 UNIX System Programming - Lecture 1 12 UNIX Programming Environments Install Linux Either stand-alone or dual boot Ubuntu installer can be used to resize a Windows partition to make room for Ubuntu Ubuntu 12.04 installation disk available from instructor Tuesday, January 14 CS 375 UNIX System Programming - Lecture 1 13 UNIX Programming Environments Cygwin Cygwin is a free UNIX-like environment for Windows. It includes C and C++ compilers, shells, Perl, Python, UNIX emulation libraries and X (and much, much more). Tuesday, January 14 CS 375 UNIX System Programming - Lecture 1 14 Compiling C and C++ Programs Create source file using a text editor $ emacs first.cpp & #include <iostream> using namespace std; int main () { cout << "CS 375 is off and running!" << endl; return 0; } Tuesday, January 14 CS 375 UNIX System Programming - Lecture 1 15 Compiling C and C++ Programs Compile and run (or just “make first”) $ g++ ­o first first.cpp $ ./first Refer to the man or info pages for info on g++ Use the “-v” option to get full details: $ g++ ­v ­o first first.cpp Using built­in specs. Target: i486­linux­gnu Configured with: ../src/configure ... Tuesday, January 14 CS 375 UNIX System Programming - Lecture 1 16 PATH Environment Variable The PATH variable contains a list of directories that is searched when looking for programs: $ echo $PATH # display PATH /usr/local/sbin:/usr/local/bin:/usr/sbin ... A program not in the PATH can be run by using the complete PATHNAME: $ /home/hwang/cs375/examples/first $ ./first # . indicates current directory You can add the current directory to the PATH (not recommended): $ PATH=$PATH:. $ first Tuesday, January 14 CS 375 UNIX System Programming - Lecture 1 17 Include Files Let's build a slightly more complex application: $ emacs second.cpp & #include <iostream> #include "mymath.h" using namespace std; int main () { int a, b, c; cout << "Enter first integer: "; cin >> a; cout << "Enter second integer: "; cin >> b; c = sumtwo (a, b); cout << "Their sum is: " << c << endl; return 0; } Tuesday, January 14 CS 375 UNIX System Programming - Lecture 1 18 Include Files Here are the other files needed: $ cat mymath.cpp # Source file ­ utility funcs #include "mymath.h" int sumtwo (int x, int y) { return x + y; } $ cat mymath.h # Header file int sumtwo (int x, int y); To compile and link: $ g++ ­o second ­I. second.cpp mymath.cpp Tuesday, January 14 CS 375 UNIX System Programming - Lecture 1 19 Include Files g++ automatically looks in the current directory so the “-I.” is optional here. Standard include directories such as /usr/include, /usr/local/include, /usr/include/c++/4.6 are automatically searched. Tuesday, January 14 CS 375 UNIX System Programming - Lecture 1 20 UNIX Libraries Linux supports static and shared libraries. Shared libraries are used by default. Code from shared libraries is loaded at runtime and may be shared by many applications. Shared libraries are usually located in /lib or /usr/lib and have a .so filename extension. Static libraries are copied into the executable making them much larger, but may be desired if you want to distribute a program in binary form. Use the -static option with g++. Tuesday, January 14 CS 375 UNIX System Programming - Lecture 1 21 UNIX Libraries Example: Put the sumtwo function into a static library $ g++ ­c mymath.cpp # generate mymath.o object file $ ar rv libmymath.a mymath.o # add obj file to lib $ ranlib libmymath.a # not required under Linux $ g++ ­o second second.cpp ­L. ­lmymath ­L. option (required) tells g++ to look in the current directory for libraries. ­lmypath tells g++ to open the library name libmypath.a gcc automatically looks in standard directories for libraries: /usr/lib, /usr/local/lib Tuesday, January 14 CS 375 UNIX System Programming - Lecture 1 22 UNIX Libraries The ar utility is used to create, extract, list, and add modules to libraries. $ ar t /usr/lib/i386­linux­gnu/libm.a # List modules in math lib $ ar x libmymath.a mymath.o # Extract obj file The nm utility can be used to list all of the symbols in a library, object file, or executable. $ nm ­C libmymath.a $ nm ­C mymath.o $ nm ­C second Tuesday, January 14 CS 375 UNIX System Programming - Lecture 1 23 UNIX Libraries The ldd utility will list the shared libraries required by a program: $ ldd /bin/ls linux­gate.so.1 => (0xb774b000) librt.so.1 => /lib/tls/i686/cmov/librt.so.1 ... ... $ ldd second linux­gate.so.1 => (0xb77d7000) libstdc++.so.6 => /usr/lib/libstdc++.so.6 ... libm.so.6 => /lib/tls/i686/cmov/libm.so.6 ... ... ldconfig is used by the administrator to make shared libraries available to others on the system. Tuesday, January 14 CS 375 UNIX System Programming - Lecture 1 24