Presentation is loading. Please wait.

Presentation is loading. Please wait.

Day 01 Introduction to Linux and C

Similar presentations


Presentation on theme: "Day 01 Introduction to Linux and C"— Presentation transcript:

1 Day 01 Introduction to Linux and C

2 Why learn C (after Java)?
Both high-level and low-level language Better control of low-level mechanisms Performance better than Java Java hides many details needed for writing OS code But,…. Memory management responsibility Explicit initialization and error detection More room for mistakes

3 Goals of this tutorial To introduce some basic C concepts to you
so that you can read further details on your own To warn you about common mistakes made by beginners

4 Creating an executable
During the linking stage, the header files and library functions are used. Source:

5 Types of files C source files (.c) C header files (.h)
Object files (.o) Executable files (typically no extension – by default : a.out) Library files (.a or .so) *.a files are mostly static libraries *.so files are mostly dynamic libraries linking to static libraries includes the actual code for the library functions/procedures with the executable and its size will be large when compared to an executable which uses dynamic libraries.

6 External library files libname.a or libname.so
Special functionality is provided in the form of external libraries of ready-made functions Ready-compiled code that the compiler merges, or links, with a C program during compilation For example, libraries of mathematical functions, string handling functions, and input/output functions Look for the library files under /usr/lib and header files under /usr/include .a – static library files. The object code is added to the object files at the linking stage. .so – shared library files that are dynamically linked. Two step process: 1. During linking create a symbol table that will have an entry for every reference in the library file. Blank addresses against each entry. 2. At loadtime/runtime, when the required library file is loaded into memory, then fill in the addresses in the symbol table. This is slightly slower but allows sharing of the library file by two more programs and results in smaller executables.

7 Example 1 – What is the output of this program?
#include <stdio.h> //#include “myheader.h” int main() { printf(“Hello World. \n \t and you ! \n ”); /* print out a message */ return 0; }

8 Summarizing the Example
#include <stdio.h> = include header file stdio.h No semicolon at end Small letters only – C is case-sensitive int main(){ … } is the only code executed printf(“ /* message you want printed */ ”); \n = newline \t = tab \ in front of other special characters within printf creates “escape sequences”. printf(“Have you heard of \”The Rock\” ? \n”); \t – tab \b – backspace \\ - slash \” – quotes Section 2.3 of K&R has the entire list as does the printf man page.

9 Compiling and running from the command line in UNIX/Linux
prompt>gcc eg1.c (Creates a.out) prompt>./a.out (Runs the executable) prompt>gcc eg1.c –o eg (Creates eg1 not a.out) prompt>./eg1

10 To compile, use flag “l” and name i.e. –lname.
Eg. gcc –o test test.c –lm where “m” in “lm” comes from libm.so i.e. the math library.

11 Linux and C Introduction
Login to Angel Point your browser to “Course Resources” Expand the “CSSE 332 Courses” link Click on the “Course Resources Page” link Click on Introduction to Linux and C Follow the tutorial If you have any questions I am there to help


Download ppt "Day 01 Introduction to Linux and C"

Similar presentations


Ads by Google