linux programming

Upload: pham-minh-tri

Post on 07-Mar-2016

235 views

Category:

Documents


0 download

DESCRIPTION

it's

TRANSCRIPT

  • Tuesday, December 29, 2015

    Linux programming

    By Phu Le

    Partner: www.facebook.com/chaybaitap

  • Client-first/driven Adaptation Innovation & Improvement Teamwork Empowerment & Accountability Lead by Example Social Community

    Page 2Confidential

    Makefile

    What is a makefile?

    Static library and Shared library

    Assembler, Compiler, Linker and Loader

    system() function

    Contents

  • Client-first/driven Adaptation Innovation & Improvement Teamwork Empowerment & Accountability Lead by Example Social Community

    Page 3Confidential

    Overview: Development process

    Creation of source files (.c, .h, .cpp) Compilation (e.g. *.c *.o) and linking Running and testing programs

    Makefile: What is makefile?

  • Client-first/driven Adaptation Innovation & Improvement Teamwork Empowerment & Accountability Lead by Example Social Community

    Page 4Confidential

    d.hd.cppd.bj

    Object files contain

    machine code but not

    in executable form.

    Object files contain

    references (calls) to

    external functions that

    must be resolved.

    Linker programs are

    commonly invoked by

    (C/C++) compilers

    Separate Compilation Steps

    Step 1

    source files compiled

    to object files

    Step 2

    objects files

    linked to form

    executable

    file.exe

    a.h

    a.cpp

    a.obj

    b.h

    b.cpp

    b.obj

    c.h c.cpp c.obj

    Overview: Development process

  • Client-first/driven Adaptation Innovation & Improvement Teamwork Empowerment & Accountability Lead by Example Social Community

    Page 5Confidential

    Compilation (e.g. *.c *.o) and linking

    Compilers (e.g. gcc, g++)

    Automatic building tools (e.g. make)

    Running and testing programs

    Debuggers (e.g. gdb)

    Compiling with g++:

    g++ hello.cpp (compile hello.cpp, produce executable a.out)

    g++ -o hello hello.cpp other_fns.cpp (compile hello.cpp and

    other_fns.cpp, produce executable hello)

    Overview

  • Client-first/driven Adaptation Innovation & Improvement Teamwork Empowerment & Accountability Lead by Example Social Community

    Page 6Confidential

    From any source file, you can produce an object file to be

    linked in later to an executable

    g++ -c hello.cpp

    g++ -c other_fuxnctions.cpp

    g++ -o hello hello.o other_functions.o

    Overview

  • Client-first/driven Adaptation Innovation & Improvement Teamwork Empowerment & Accountability Lead by Example Social Community

    Page 7Confidential

    With medium to large software projects containing many

    files, its difficult to:

    Type commands to compile all the files correctly each

    time

    Keep track of which files have been changed

    Keep track of files dependencies on other files

    Problems

  • Client-first/driven Adaptation Innovation & Improvement Teamwork Empowerment & Accountability Lead by Example Social Community

    Page 8Confidential

    The make utility automates this process

    automatically builds executable programs from source

    code by reading makefiles.

    Make Utility

  • Client-first/driven Adaptation Innovation & Improvement Teamwork Empowerment & Accountability Lead by Example Social Community

    Page 9Confidential

    Makefiles:

    files consisting of rules and dependencies that specify how

    to build (or make) a target program.

    can make the compilation procedure much faster.

    The compilation is done using a single command

    Only the files that must be compiled are compiled

    Allows managing large programs with a lot of dependencies.

    Makefile: What is makefile?

  • Client-first/driven Adaptation Innovation & Improvement Teamwork Empowerment & Accountability Lead by Example Social Community

    Page 10Confidential

    PyCal: Makefile Example

  • Client-first/driven Adaptation Innovation & Improvement Teamwork Empowerment & Accountability Lead by Example Social Community

    Page 11Confidential

    PyCal: Makefile Example

  • Client-first/driven Adaptation Innovation & Improvement Teamwork Empowerment & Accountability Lead by Example Social Community

    Page 12Confidential

    Contains objects code.

    Linked with an end-user application.

    A part of that executable.

    /lib, /usr/lib or /usr/local/lib

    Filenames: start with lib, end with .a

    Static library

  • Client-first/driven Adaptation Innovation & Improvement Teamwork Empowerment & Accountability Lead by Example Social Community

    Page 13Confidential

    Static library

  • Client-first/driven Adaptation Innovation & Improvement Teamwork Empowerment & Accountability Lead by Example Social Community

    Page 14Confidential

    Build:

    $ ar

    $ ar --help

    Options: c, r, v, V, T, ...

    Example:

    $ ar -crv libExpress.a Postfix.o Prefix.o Values.o

    Static library

  • Client-first/driven Adaptation Innovation & Improvement Teamwork Empowerment & Accountability Lead by Example Social Community

    Page 15Confidential

    Intended to be shared objects files.

    into memory at load time or run time.

    Rather than being copied by linker when it creates executable.

    (static library)

    Filenames: start with lib, end with .so

    Shared Library

  • Client-first/driven Adaptation Innovation & Improvement Teamwork Empowerment & Accountability Lead by Example Social Community

    Page 16Confidential

    Shared library

  • Client-first/driven Adaptation Innovation & Improvement Teamwork Empowerment & Accountability Lead by Example Social Community

    Page 17Confidential

    Build:

    the -fPIC option to object code file.

    the -shared option to shared library file.

    Example:

    $ g++ -c -fPIC Express.cpp

    $ g++ -shared -o libExpress.so Express.o

    Shared Library

  • Client-first/driven Adaptation Innovation & Improvement Teamwork Empowerment & Accountability Lead by Example Social Community

    Page 18Confidential

    Use:

    LD_LIBRARY_PATH

    $ export LD_LIBRARY_PATH=$ LD_LIBRARY_PATH: YOUR_DIRECTORY

    /lib , /usr/lib, /usr/local/lib

    -Wl,-rpath option

    $ g++ main.o -L./LIB -lExpress -Wl,-rpath,./LIB

    Shared Library

  • Client-first/driven Adaptation Innovation & Improvement Teamwork Empowerment & Accountability Lead by Example Social Community

    Page 19Confidential

    $ nm to see more information of library

    $ nm libExpress.so

    $ nm libExpress.a

    $ nm -help

    Example:

    $ nm libExpress.so

    0000000000201058 B __bss_start

    0000000000201058 b completed.6972

    ...

    0000000000200df0 d _DYNAMIC

    ...

    0000000000000a7c t _GLOBAL__sub_I_functions.cpp

    0000000000000b53 t _GLOBAL__sub_I_sumfunctions.cpp

    w __gmon_start__

    Check Library

  • Client-first/driven Adaptation Innovation & Improvement Teamwork Empowerment & Accountability Lead by Example Social Community

    Page 20Confidential

    $ ldd check the required shared libraries for a program to run.

    $ ldd main

    Example:

    linux-vdso.so.1 => (0x00007fff829fe000)

    libExpress.so =>

    /usr/lib/libExpress.so(0x00007f2b68311000)

    ...

    Check Library

  • Client-first/driven Adaptation Innovation & Improvement Teamwork Empowerment & Accountability Lead by Example Social Community

    Page 21Confidential

    Static lib and Shared lib

  • Client-first/driven Adaptation Innovation & Improvement Teamwork Empowerment & Accountability Lead by Example Social Community

    Page 22Confidential

    Compiler, Assember, Linker and Loader

  • Client-first/driven Adaptation Innovation & Improvement Teamwork Empowerment & Accountability Lead by Example Social Community

    Page 23Confidential

    Compiler, Assember, Linker and Loader

  • Client-first/driven Adaptation Innovation & Improvement Teamwork Empowerment & Accountability Lead by Example Social Community

    Page 24Confidential

    Translates high-level language program into assembly

    language.

    .s (lower case) Assembler source files.

    Compiler

  • Client-first/driven Adaptation Innovation & Improvement Teamwork Empowerment & Accountability Lead by Example Social Community

    Page 25Confidential

    Converts assembly language programs into object files

    Object files contain a combination of machine instructions, data,

    and information needed to place instructions properly in

    memory.

    *.a, *.o source file output.

    Assember

  • Client-first/driven Adaptation Innovation & Improvement Teamwork Empowerment & Accountability Lead by Example Social Community

    Page 26Confidential

    Typically, assemblers make two passes over the assembly file

    First pass: reads each line and records labels in a symbol table.

    Second pass: use info in symbol table to produce actual machine

    code for each line.

    Assember

  • Client-first/driven Adaptation Innovation & Improvement Teamwork Empowerment & Accountability Lead by Example Social Community

    Page 27Confidential

    Tool that merges the object files produced by separate

    compilation or assembly and creates an executable file.

    *.out, *.exe source file output.

    Linker

  • Client-first/driven Adaptation Innovation & Improvement Teamwork Empowerment & Accountability Lead by Example Social Community

    Page 28Confidential

    Three tasks

    Searches the program to find library routines used by

    program, e.g. printf(), math routines,

    Determines the memory locations that code from each

    module will occupy and relocates its instructions by

    adjusting absolute references

    Resolves references among files.

    Linker

  • Client-first/driven Adaptation Innovation & Improvement Teamwork Empowerment & Accountability Lead by Example Social Community

    Page 29Confidential

    Part of the OS that brings an executable file residing on disk

    into memory and starts it running.

    Loader

  • Client-first/driven Adaptation Innovation & Improvement Teamwork Empowerment & Accountability Lead by Example Social Community

    Page 30Confidential

    Steps

    Read executable files header to determine the size of text and

    data segments

    Create a new address space for the program

    Copies instructions and data into address space

    Copies arguments passed to the program on the stack

    Initializes the machine registers including the stack ptr

    Jumps to a startup routine that copies the programs arguments

    from the stack to registers and calls the programs main routine

    Loader

  • Client-first/driven Adaptation Innovation & Improvement Teamwork Empowerment & Accountability Lead by Example Social Community

    Page 31Confidential

    Loader

  • Client-first/driven Adaptation Innovation & Improvement Teamwork Empowerment & Accountability Lead by Example Social Community

    Page 32Confidential

    Build:

    g++ hello.cpp o hello

    g++ -save-temps hello.cpp o hello

    Compiler, Assember, Linker and Loader

  • Client-first/driven Adaptation Innovation & Improvement Teamwork Empowerment & Accountability Lead by Example Social Community

    Page 33Confidential

    execute a shell command.

    #include

    int system(const char *command);

    The return value of system():

    If a child process could not be created, or its status could not be

    retrieved, the return value is -1.

    If a shell could not be executed in the child process, the return

    value is 127. (_exit(2)).

    -1: The return value of system(string).

    system() function

  • Client-first/driven Adaptation Innovation & Improvement Teamwork Empowerment & Accountability Lead by Example Social Community

    Page 34Confidential

    http://eclipsebook.in/c-cpp-development/building-code/build-

    eclipse-managed/

    http://www.fotech.org/forum/index.php?showtopic=36504

    http://vi.wikipedia.org/wiki/B%E1%BB%99_tr%C3%ACnh_d%E1%BB%8B

    ch_GNU

    http://www.adp-gmbh.ch/cpp/gcc/create_lib.html

    http://www.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make

    .html

    http://forum.gocit.vn/threads/gioi-thieu-gcc.158/

    http://vi.wikipedia.org/wiki/GNU_Debugger

    References

  • Client-first/driven Adaptation Innovation & Improvement Teamwork Empowerment & Accountability Lead by Example Social Community

    Page 35Confidential

    http://lttqstudy.wordpress.com/2011/08/22/ham-trong-cc/

    http://www.mingw.org/wiki/sampledll

    http://www.adp-gmbh.ch/cpp/gcc/create_lib.html

    http://www.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make

    .html

    http://ftp.gnu.org/old-gnu/Manuals/make-

    3.79.1/html_chapter/make_9.html

    http://web.mit.edu/gnu/doc/html/make_5.html

    References

  • Client-first/driven Adaptation Innovation & Improvement Teamwork Empowerment & Accountability Lead by Example Social Community

    Welcome Your Questions!

    Page 36Confidential