KEMBAR78
Linux programming | PDF
Programming in the Linux
Environment

ISO C and POSIX Standards
The Development tools
Using GCC
Creating and Using Libraries
Dynamic modules
Debugging with GDB
What are ISO C and POSIX
Standards and Why should we
     care about them ?
The Development tools available in
     the Linux Environment
 – Programming Languages Available
      ✔ C

      ✔ C++

      ✔ Java

      ✔ Python

      ✔ C# (CLR comes from the Mono Project)

      ✔ Perl

      ✔ Tcl/tk

      ✔ Vala

      ✔ Lua

      ✔ Ada

      ✔ PHP

      ✔ Ruby

      ✔ Haskell, Pascal, Fortran, Lisp,
The Development tools available
   in the Linux Environment
  – Tools for C Programming
       • GCC: GNU Compilers Collection
       • GDB: GNU Debugger
       • Emacs/Vi: The code editors
Using GCC
– Writing A “Hello World” Program
– Compiling and Running the program

 gcc -o helloworld helloworld.c

 ./helloworld
Using GCC
– Some Essential features of GCC
    • Including Additional Header Files

 gcc -I/home/abhinav/code/include
  include-test.c -o include-test

 ./include-test
Using GCC
– Some Essential features of GCC
    • Linking Libraries
    • Debugging symbols
    • Creating libraries
Creating and Using Libraries
  – Libraries are a collection of functions
  – libm, libpthread, libdbm, libc

    Types of Libraries:
  – Static Libraries: .a extension
  – Dynamic Libraries: .so extension
Creating and Using Libraries
 – Making object files:

          gcc -I./include -c new-helloworld.c print.c

 – Linking Object files

        • Gcc -o new-helloworld new-helloworld.o
           print.o

   -c : tells the compiler to only compile the
     file, but not to link them
   -o: to specify the name of the output file.
Creating and Using Libraries
  – Creating Static Library:

         • gcc    -o *.c

         • ar cr libcalc.a *.o

  – Linking against the static library:

    gcc -I./include -o calc-test calc-test.c -L.
      -lcalc
Creating and Using Libraries
  – Creating Shared Library:

         • gcc    -c -fPIC *.c

         • gcc -shared -fPIC -o libcalc.so *.o

  – Linking against the shared library:

    (Same as linking against a static library)

    gcc -I./include -o calc-test calc-test.c -L.
      -lcalc
Debugging with GDB
– Generating debugging symbols:
      • gcc -g -o

  Starting the debugger:
        • gdb <program name>

  Some common commands:
     • b line-number or function name: sets the break point
     • next : step over
     • step: step in
     • p <variable name> : prints the value of the variable
     • where: prints the stack trace
     • up: moves you up in the stack
Dynamically Loading Modules


 – Concept of Plugins/modules which are loaded by the program
     at run tim
 – No dynamic or statical linking of libraries

   void *handle = dlopen(“libcalc.so”, RTLD_LAZY);
   void (*test) () = dlsym(handle, “add”);
   (*test) ();
   dlclose (handle);
Some other productivity tools

        Diff: What are the changes made in the file
               Patch: merging the changes
  Version Control tools: Git, Bazaar, mercurial, SVN, CVS
Thank You
       --Abhinav Upadhyay
          Twitter: iamabhi9
Irc.freenode.net: abhinav-

Linux programming

  • 1.
    Programming in theLinux Environment ISO C and POSIX Standards The Development tools Using GCC Creating and Using Libraries Dynamic modules Debugging with GDB
  • 2.
    What are ISOC and POSIX Standards and Why should we care about them ?
  • 3.
    The Development toolsavailable in the Linux Environment – Programming Languages Available ✔ C ✔ C++ ✔ Java ✔ Python ✔ C# (CLR comes from the Mono Project) ✔ Perl ✔ Tcl/tk ✔ Vala ✔ Lua ✔ Ada ✔ PHP ✔ Ruby ✔ Haskell, Pascal, Fortran, Lisp,
  • 4.
    The Development toolsavailable in the Linux Environment – Tools for C Programming • GCC: GNU Compilers Collection • GDB: GNU Debugger • Emacs/Vi: The code editors
  • 5.
    Using GCC – WritingA “Hello World” Program – Compiling and Running the program gcc -o helloworld helloworld.c ./helloworld
  • 6.
    Using GCC – SomeEssential features of GCC • Including Additional Header Files gcc -I/home/abhinav/code/include include-test.c -o include-test ./include-test
  • 7.
    Using GCC – SomeEssential features of GCC • Linking Libraries • Debugging symbols • Creating libraries
  • 8.
    Creating and UsingLibraries – Libraries are a collection of functions – libm, libpthread, libdbm, libc Types of Libraries: – Static Libraries: .a extension – Dynamic Libraries: .so extension
  • 9.
    Creating and UsingLibraries – Making object files: gcc -I./include -c new-helloworld.c print.c – Linking Object files • Gcc -o new-helloworld new-helloworld.o print.o -c : tells the compiler to only compile the file, but not to link them -o: to specify the name of the output file.
  • 10.
    Creating and UsingLibraries – Creating Static Library: • gcc -o *.c • ar cr libcalc.a *.o – Linking against the static library: gcc -I./include -o calc-test calc-test.c -L. -lcalc
  • 11.
    Creating and UsingLibraries – Creating Shared Library: • gcc -c -fPIC *.c • gcc -shared -fPIC -o libcalc.so *.o – Linking against the shared library: (Same as linking against a static library) gcc -I./include -o calc-test calc-test.c -L. -lcalc
  • 12.
    Debugging with GDB –Generating debugging symbols: • gcc -g -o Starting the debugger: • gdb <program name> Some common commands: • b line-number or function name: sets the break point • next : step over • step: step in • p <variable name> : prints the value of the variable • where: prints the stack trace • up: moves you up in the stack
  • 13.
    Dynamically Loading Modules – Concept of Plugins/modules which are loaded by the program at run tim – No dynamic or statical linking of libraries void *handle = dlopen(“libcalc.so”, RTLD_LAZY); void (*test) () = dlsym(handle, “add”); (*test) (); dlclose (handle);
  • 14.
    Some other productivitytools Diff: What are the changes made in the file Patch: merging the changes Version Control tools: Git, Bazaar, mercurial, SVN, CVS
  • 15.
    Thank You --Abhinav Upadhyay Twitter: iamabhi9 Irc.freenode.net: abhinav-