UNIX Makefile. C Project Library Distribution and Installation.

Similar documents
CSC 2500: Unix Lab Fall 2016

GDB and Makefile. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

CMPT 300. Operating Systems. Brief Intro to UNIX and C

Section 1: Tools. Kaifei Chen, Luca Zuccarini. January 23, Make Motivation How... 2

CS Basics 15) Compiling a C prog.

Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 2

Build automation. CSE260, Computer Science B: Honors Stony Brook University

Reviewing gcc, make, gdb, and Linux Editors 1

The Linux Programming Environment. Computer Science Department Texas State University

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

Makefiles SE 2XA3. Term I, 2018/19

independent compilation and Make

2 Compiling a C program

Continue: How do I learn C? C Primer Continued (Makefiles, debugging, and more ) Last Time: A Simple(st) C Program 1-hello-world.c!

GDB Linux GNU Linux Distribution. gdb gcc g++ -g gdb EB_01.cpp

Maemo Diablo GNU Make and makefiles Training Material

EL2310 Scientific Programming

CMPSC 311- Introduction to Systems Programming Module: Build Processing

CMPSC 311- Introduction to Systems Programming Module: Build Processing

Laboratorio di Tecnologie dell'informazione

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

CS201 - Lecture 1 The C Programming Language

Program Design: Using the Debugger

Introduction to Supercomputing

CS240: Programming in C

Multiple file project management & Makefile

Laboratorio di Programmazione. Prof. Marco Bertini

How to learn C? CSCI [4 6]730: A C Refresher or Introduction. Diving In: A Simple C Program 1-hello-word.c

EL2310 Scientific Programming

Separate Compilation of Multi-File Programs

Exercise 1: Basic Tools

CS631 - Advanced Programming in the UNIX Environment. UNIX development tools

What s next. Computer Systems A Programmer s Perspective

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

Basic Unix Commands. CGS 3460, Lecture 6 Jan 23, 2006 Zhen Yang

Problem Set 1: Unix Commands 1

Lab 1: First Steps in C++ - Eclipse

Using the Debugger. Michael Jantz Dr. Prasad Kulkarni

Laboratorio di Programmazione. Prof. Marco Bertini

INTERMEDIATE SOFTWARE DESIGN SPRING 2011 ACCESS SPECIFIER: SOURCE FILE

CAAM 420 Fall 2012 Lecture 15. Roman Schutski

Lab #1 Installing a System Due Friday, September 6, 2002

[Software Development] Makefiles. Davide Balzarotti. Eurecom Sophia Antipolis, France

Projects and Make Files

CS354 gdb Tutorial Written by Chris Feilbach

Outline. COMP 2718: Software Development Tools: gcc and make. C and Java 3/28/16

Debugging. ICS312 Machine-Level and Systems Programming. Henri Casanova

{C} Tools of the Trade

Your first C and C++ programs

Using gdb to find the point of failure

PRINCIPLES OF OPERATING SYSTEMS

CSE 374 Programming Concepts & Tools

COMP 2400 UNIX Tools

Laboratorio di Programmazione. Prof. Marco Bertini

The Make Utility. Independent compilation. Large programs are difficult to maintain. Problem solved by breaking the program into separate files

Chapter 1 Introduction to Computers and C++ Programming

The make utility. Alark Joshi COMPSCI 253

GNU make... Martin Ohlerich, Parallel Programming of High Performance Systems

#include <stdio.h> int main() { printf ("hello class\n"); return 0; }

Lecture 4: Build Systems, Tar, Character Strings

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 1. Spring 2011

The Make Utility. Independent compilation. Large programs are difficult to maintain. Problem solved by breaking the program into separate files

Unit 10. Linux Operating System

Draft. Chapter 1 Program Structure. 1.1 Introduction. 1.2 The 0s and the 1s. 1.3 Bits and Bytes. 1.4 Representation of Numbers in Memory

CSE 333 Interlude - make and build tools

Using the EE 109 Tools. Allan Weber Mark Redekopp

Using the Unix system. UNIX Introduction

Getting started with UNIX/Linux for G51PRG and G51CSA

PROGRAMMAZIONE I A.A. 2017/2018

Development Environment & Linux Guide

Data and File Structures Laboratory

Programming Requirements

Makefiles are a simple way to organize code compilation. Using a makefile it is possible to compile several source files to produce an executable;

Make: a build automation tool

KYC - Know your compiler. Introduction to GCC

Embedded Software TI2726 B. 3. C tools. Koen Langendoen. Embedded Software Group

Chapter 1 An Introduction to C++, Unix, SSH and Komodo Edit

Laboratory 1 Semester 1 11/12

CSE 351. GDB Introduction

Make: a build automation tool 1/23

COSC350 System Software

C: Program Structure. Department of Computer Science College of Engineering Boise State University. September 11, /13

Obtained the source code to gcc, one can just follow the instructions given in the INSTALL file for GCC.

Introduction to Supercomputing

EC 413 Computer Organization

Dr. Md. Humayun Kabir CSE Department, BUET

COMP Lecture Notes The Compiler

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

Compiler Theory. (GCC the GNU Compiler Collection) Sandro Spina 2009

TNM093 Practical Data Visualization and Virtual Reality Laboratory Platform

CS 107 Lecture 18: GCC and Make

Review of Scientific Programming in C and Fortran. Michael McLennan Software Architect HUBzero Platform for Scientific Collaboration

Carnegie Mellon. Linux Boot Camp. Jack, Matthew, Nishad, Stanley 6 Sep 2016

How Compiling and Compilers Work

Deitel Dive-Into Series: Dive-Into Cygwin and GNU C++

CS11 Advanced C++ Fall Lecture 4

CIT 595 Spring System Software: Programming Tools. Assembly Process Example: First Pass. Assembly Process Example: Second Pass.

23. Check that hello.txt now contains (substitute your info for mine): hello rcwhaley: Dr. Whaley

Maemo Diablo Reference Manual for maemo 4.1. GNU Build System

Section 1: Tools. Contents CS162. January 19, Make More details about Make Git Commands to know... 3

Transcription:

UNIX Makefile C Project Library Distribution and Installation.

Tarballs Most non-package software is distributed in source code format. The most common format being C project libraries in compressed TAR files known as tarballs. These files usually contain the string tar indicating the file it a tar (tape archive) format file. The file may also be compressed indicated by a filename suffix ending in.z (compress) or.gz (gnuzip). These file are then untarr red into the product library as setup by the project programmer

Tar commands tar cvf <tarfilename.tar> <target directories> - creates tar file. tar tvf <tarfilename.tar> - list tar file contents tar xvf <tarfilename.tar> - extracts tar file Can add z flag for newer LINUX distros with gnuzip for automatic compress/decompress (.gz suffux). Otherwise try compress command (.Z suffix) USAGE: - Always create tarfile in target directory (relative file/directory names) - Always list tarfile before extracting (insure relative file names) - Always extact tarfile in target directory (relative file/directory names) Example: - tar xzvf ~/bb-1_3a_tar.gz

The C compiler Standard C is the original language as developed by Ken Thompson et. al. This is the format taught in most basic C language classes. ANSI C first developed in 1987, approved in 1989 (C89). ISO in 1990. Introduced OOP programming structures inheritance etc. Source code designated as.c. GNU compiler is gcc. C99 came out as 1999 ANSI/ISO standard. Enforced stricter syntax rules introduced new arithmetic, data types and structures, Current 2011 standard version is ISO C11. Which includes numerous memory management and multithreading structures. C++ (C with classes) developed in 1979 by Bjarne Stroustrup of Bell Labs as a fullblown OOP language. Standardized buy ANSI in 1998 as C++99. Not strictly a superset of ANSI C but is backwards compatible with some exceptions. Source code designated as.cpp. GNU complier is g++. Used primarily for hardware level progrmming.

Compile Process Compiler Stage: All C language code in the.c file is converted into a lower-level language called Assembly language; making.s files. Assembler Stage: The assembly language code made by the previous stage is then converted into object code which are fragments of code which the computer understands directly. An object code file ends with.o. Linker Stage: The final stage in compiling a program involves linking the object code to code libraries which contain certain "built-in" functions, such as printf. This stage produces an executable program, which is named a.out by default.

C Compile Example main.c #include <stdio.h> #include <iostream> #include "functions.h" using namespace std; int main(){ print_hello(); std::cout << endl; std::cout << "The factorial of 5 is " << factorial(5) << endl; return 0; } Note the deprecated cout instruction requiring special handling under c++.

C Compile Example hello.c #include <stdio.h> #include <iostream> #include "functions.h" using namespace std; void print_hello(){ std::cout << "Hello World!"; } Note the deprecated cout instruction requiring special handling under c++.

C Compile Example factorial.c #include "functions.h" int factorial(int n){ if(n!=1){ return(n * factorial(n-1)); } else return 1; } functions.h void print_hello(); int factorial(int n); # recursive call

C Compile Example Compile the project g++ main.c hello.c factorial.c o hello This above command will generate hello binary. In our example we have only four files and we know the sequence of the function calls so it may be feasible to write the above command by hand and prepare a final binary. But for the large project where we will have thousands of source code files, it becomes difficult to maintain the binary builds. Run it.../hello =>Hello World! =>The factorial of 5 is 120

Shared Object Libraries "shared components" or "archive libraries", groups together multiple compiled object code files into a single file known as a library. There are two Linux C/C++ library types: - Static linked libraries (.a): Library of object code which is linked with, and becomes part of the application. - Dynamically linked shared object libraries (.so): There is only one form of this library but it can be used in two ways. 1) Dynamically linked at run time but statically aware. The libraries must be available during compile/link phase. The shared objects are not included into the executable component but are tied to the execution. 2) Dynamically loaded/unloaded and linked during execution (i.e. browser plug-in) using the dynamic linking loader system functions. Libraries are typically names with the prefix "lib". This is true for all the C standard libraries. When linking, the command line reference to the library will not contain the library prefix or suffix. Thus the following link command: gcc src-file.c -lm -lpthread The libraries referenced in this example for inclusion during linking are the math library and the thread library. They are found in/usr/lib/libm.a and /usr/lib/libpthread.a. See also ar, ld, ldd and strings commands.

Shared Object Libraries Creating a shared library: - gcc -Wall -fpic -c *.c - gcc -shared -Wl,-soname,libctest.so.1 -o libctest.so.1.0 *.o - mv libctest.so.1.0 /opt/lib The following creates the library libctest.so.1.0 and symbolic links to it. - ln -sf /opt/lib/libctest.so.1.0 /opt/lib/libctest.so.1 - ln -sf /opt/lib/libctest.so.1.0 /opt/lib/libctest.so.. - ln -sf /opt/lib/libctest.so.1.0 /opt/lib/libctest.so.1 - ln -sf /opt/lib/libctest.so.1 /opt/lib/libctest.so Creating a static library: - Compile: cc -Wall -c ctest1.c ctest2.c - Create library "libctest.a": ar -cvq libctest.a ctest1.o ctest2.o - List files in library: ar -t libctest.a - Linking with the library: cc -o executable-name prog.c libctest.a cc -o executable-name prog.c -L/path/to/library-directory lctest Linking the library to the program: - gcc -Wall -I/path/to/include-files -L/path/to/libraries prog.c -lctest -o prog - gcc -Wall -L/opt/lib prog.c -lctest -o prog Using: - Set path: export LD_LIBRARY_PATH=/opt/lib:$LD_LIBRARY_PATH - Run: prog

Replacement Makefile Makefile SHELL = /bin/sh MAKE = make CC = g++ LIBS= CFLAGS=-DSIGSETJMP -O hello: main.o hello.o factorial.o functions.h ${CC} ${CFLAGS} -o $@ main.o hello.o factorial.o ${LIBS} clean: rm -f *.o Run as make; make clean

Why Make and Makefile? How do we know how to compile complex C project libraries? We don t. Hence Makefile process. An offshoot of the UNIX Source Code Control System (SCCS) for C program development. We need a file that instructs make how to compile and link a program. Makefile. make f <filename> [Makefile] The make program allows you to use macros, which are similar to variables to codify how to compile a set of source code.

Makefile Macros are assigned as BASH variable: CFLAGS= -O -systype bsd43 LIBS = "-lncurses -lm -lsdl Special Macros used for regular substitution $@ - is the name of the file to be made. $? - is the names of the changed dependents. $< the name of the related file that caused the action. $* the prefix shared by target and dependent files. The are over 2 dozen conventional and special macros

Makefile Labels: contain lists of commands to be executed hello: main.c hello.c factorial.c $(CC) $(CFLAGS) $? $(LDFLAGS) -o $@ In this example $@ represents hello and $? or $@.c will pickup all the changed source files. It is very common that a final binary will be dependent on various source code and source header files. Dependencies are important because they tell to make about the source for any target. Consider the following example hello: main.o factorial.o hello.o $(CC) main.o factorial.o hello.o -o hello In this example we are telling to make that hello is dependent on main.o, factorial.o and hello.o so whenever there is a change in any of these object files then make will take action.

Makefile Same time we would have to tell to make how to prepare.o files so we would have to define those dependencies also as follows: main.o: main.c functions.h $(CC) -c main.c factorial.o: factorial.c functions.h $(CC) -c factorial.c hello.o: hello.c functions.h $(CC) -c hello.c The general syntax of a Makefile Target Rule is: target [target...] : [dependent...] [ command...] Items in brackets are optional, ellipsis is one or more. Note tab to preface each command is required. A simple example is where you define a rule to make your target hello from three other files. hello: main.o factorial.o hello.o $(CC) main.o factorial.o hello.o -o hello

Standard targets People have come to expect certain targets in Makefiles. You should always browse first, but it's reasonable to expect that the targets all (or just make), install, and clean will be found. make all - should compile everything so that you can do local testing before installing things. make install - should install things in the right places. But watch out that things are installed in the right place for your system. make clean - should clean things up. Get rid of the executables, any temporary files, object files, etc.

Running a Makefile Running Makefile from command prompt: If you have prepared your Makefile with a name as "Makefile" then simply run make at command prompt and it will run your Makefile file. If you have given any other name to your Makefile then use the following command make -f your-makefile-name: Review the sample bb project makefile again.

General tarball/make example tar -vxzf <gzipped-tar-file> cd <dist-dir>./configure make make install make clean

Debugging gdb (gnu debugger) Use the -g command line parameter to cc, gcc, or CC: cc -g -c foo.c gdb [<program> [corefile> <pid>]] gcc g c o hello hello.c gdb./hello See also strace (Linux), truss (Solaris)

gdb commands General Commands: file [<file>] run [<args>] attach <pid> kill quit help [<topic>] Stepping and Continuing: c[ontinue] s[tep] n[ext] finish Useful breakpoint commands: b[reak] [<where>] [r]watch <expr> info break[points] clear [<where>] d[elete] [<nums>] Commands for looking around: list [<where>] search <regexp> backtrace [<n>] info [<what>] p[rint] [<expr>] Commands for altering data and control path: set <name> <expr> return [<expr>] jump <where> selects <file> as the program to debug runs selected program with arguments <args> attach gdb to a running process <pid> kills the process being debugged quits the gdb program accesses the internal help documentation continue execution (after a stop) step one line, entering called functions step one line, without entering functions finish the function and print the return value sets breakpoints. <where> sets a watchpoint, which will break when <expr> is written to [or read] prints out a listing of all breakpoints clears a breakpoint at <where> deletes breakpoints by number prints out source code at <where> searches source code for <regexp> prints a backtrace <n> levels deep prints out info on <what> (like local variables or function args) prints out the evaluation of <expr> sets variables or arguments returns <expr> from current function jumps execution to <where>