Beginning Linux Programming - Chapter 1: Getting Started

Unix, Linux, and GNU

  • Unix
    • The UNIX operating system was originally developed at Bell Laboratories, once part of the telecommunication giant AT&T
    • History
      • Unix is a trademark administered by The Open Group, and it refers to a computer operating system that conforms to a particular specification. This specification, known as The Single UNIX Specification, defines the names of, interfaces to, and behaviors of all mandatory UNIX operating system functions
      • The above specification is largely a superset of an earlier series of specifications, POSIX (Portable Operating System Interface) specifications, developed by the IEEE (Institute of Electrical and Electronic Engineers)
      • In the past, compatibility among different UNIX systems has been a problem
    • Philosophy
      • Simplicity - KISS - "Keep It Small and Simple"
      • Focus - it's often better to make a propgram perform one taks well than to throw in every feature along with the kitchen sink
      • Reusable Components - Make the core of your application available as a library
      • Filters - Many UNIX applications can transfrom their input and produce output, which can be combined with other UNIX programs
      • Open File Formats - The more successful and popular UNIX programs use configuration files and data files that are plain ASCII text or XML
      • Flexibility - avoid unnecessary restrictions
  • Linux
    • Linux is a freely distributed implementation of a UNIX-like kernel, the low-level core of an operating system
    • The intention is that the Linux kernel will not incorporate proprietary code but will contain nothing but freely distributable code
    • Linux distributions
      • Linux is actually just a kernel
      • You can obtain the sources for the kernel to compile and install it on a machien and then obtain and install many other freely distributed software programs to make a complete Linux installation
      • These installations are usually refered to as Linux systems, because they consist of much more htan just the kernel
  • The GNU Project and the Free Software Foundation
    • The Linux community supports the concept of free software, that is, sofware that is free from restrictions, subject to the GNU General Public License. Alghotugh there may be a cost involved in obtaining the software, it can thereafter be use in any way desired and is usually distributed in source form
    • The Free Software Foundation was set up by Richard Stallman, the author of GNU Emacs
    • Several examples of software from the GNU Project distributed under GPL
      • GCC: The GNU Compiler Collection, containing the GNU C compiler
      • G++: A C++ compiler
      • GDB: A source code-level debugger
      • GNU make: A version of UNIX make
      • Bison: A parser generator compatible with UNIX yacc
      • bash: A command shell

Progams and programming languages for Linux

  • Linux Programs
    • Linux applications are represented by two special types of files: executables and scripts. Executable files are programs that can be run directly by the computer. Scripts are collections of instructions for antoher program, an interpreter, to follow. These correspond to Windows .bat or .cmd files, or interpreted BASIC programs
    • Linux doesn't require executables or scripts to ahve a specific filename or any extension whatsoever
    • When you log in to a Linux system, you interact with a shell program that runs programs in the same way that hte Windows command prompt does. The directories to serach are stored in a shell variable, PATH
      • /bin: Binaries, prgrams used in booting the system
      • /usr/bin: User binaries, standard programs available to users
      • /usr/local/bin: Local binaries, programs specific to an installation
      • An administrator's login, such as root, may use a PATH varaible that includes directories where system administration programs are kept, such as /sbin and /usr/sbin
      • Optional operating system components and third-party applications may be installed in subdirectories of /opt
      • Linux, like UNIX, uses the colon character to seperate entries in the PATH variable
        • /usr/local/bin:/bin:/usr/bin:.:/home/neil/bin:/usr/X11R6/bin

           

      • Linux uses a forward slash (/) = to separate directory names in a filename

  • The C Compiler

    • On POSIX-compliant systems, the C compiler is called c89. Historically, the C compiler was simply called cc

  • Your First Linux C program

    • #include <stdio.h>
      #include <stdlib.h>
      
      int main()
      {
      	printf("Hello World\n");
      	exit(0);
      }
      
      /* to run the program
       gcc -o hello hello.c
       ./hello
      */

       

    • How it works

      • You invoked the GNU C compiler that translated the C source code into an executable file called hello

      • If PATH doesn't include a reference to your home directory, the shell won't be able to find shell 

      • If you forget the -o name option that tells the compiler where to place the executable, the compiler will place the program in a file called a.out

How to locate development resources

  • Applications
    • /usr/bin: applications supplied by the system for general use, including program development
    • /usr/local/bin or /opt: applications added by system administrators for a specific host computer or local network
    • Administrators favor /opt and /usr/local, because they keep vendor-supplied files and later additions separate from the applications upplied by the system
  • Header Files
    • For programming in C and other languages, you need header files to provide definitions of constants and declarations for system and library function calls
  • Library Files
    • Standard system libaries are sually stored in /lib and /usr/lib
    • Names
      • A library filename always starts with lib. The last part of the name starts with a dot, and specifies the type of the library
        • .a for traditional, static libraries
        • .so for shared libraries

Static and shared libraries

  • Static libraries
    • Static libraries, also known as archives, conventionally have names that end with .a
  • Create a static librariy
    • Suppose we have freq.c. and bill.c such that
      • #include <stdio.h>
        
        void fred(int arg)
        {
        	printf("fred: we passed %d\n", arg);
        }
        
        #include <stdio.h>
        
        void bill(char *arg)
        {
        	printf("bill: we passed %s\n", arg);
        }

         

      • We can compile these functions individually to produce object files ready for inclusion into a library. Doing this by invokin gthe C compiler with the -c option, which prevents the compiler from trying to create a complete program

      • gcc -c bill.c fred.c 
        ls *.o
        
        /* output
        
        bill.o fred.o
        
        */

         

      • It is a good ideato create a header file for your library. This will declare the functions in your library and should be included by all applications that want ot sue your library

      • void bill(char *);
        void freq(int);

        .

      • The calling program

      • #include <stdlib.h>
        #include "lib.h"
        
        int main()
        {
        	bill("Hello World");
        	exit(0);
        }

         

      • Compile the program and test it. For now, specify the object files explicity to the compiler, asking it to compile your file and link it with the previously compiled object module bill.o

      • gcc -c program.c
        gcc -o program program.o bill.o
        ./program
        
        /* output
        bill: we passed Hello World
        */

         

      • Create and use a library. Use the ar program to create th earchive and add your object files to it. The program is called ar because it creates archives, or collections, of individual files place together in one large file

      • ar crv libfoo.a bill.o fred.o
        
        /* output 
        a - bill.o
        a - fred.o
        */

         

      • The library is created and the two object files added. To use the library successfully, some systems, notably those derived from Berkeley UNIX, require that a table of contents be created for the library. Do this with the ranlib command

      • ranlib libfoo.a

         

      • Your library is now ready to use. You can add to the list of files to be used by the compiler to crate your program like this

      • gcc -o program program.o libfoo.a
        ./program
        
        /* output
        bill: we passed Hello World
        */

         

    • Shared Libraries

      • One disadvantage of static libraries is that when you run many applications at the same time and they all use functions from the same library, you may end up with many copies of hte same functions in memory and indeed many copies in the program files themselves. This can consume a large amount of valuable memory and disk space

      • Shared libraries are stored in the same places as static libraries, but shared libraries have a different filename suffix. On a typicl Linux system, the shared version of the standard math library is /lib/libm.so

      • When a program uses a shared library, it is linked in such a way that it doesn't contain function code itself, but references to shared code that will be made available at run time. When the resulting program is loaded into memory to be executed, the function references are resolved and calls are made to the shared library, which will be loaded into memory if needed

      • For Linux systems, hte program that takes care of loading shared libraries and resolving client program function references is called ld.so

      • You can see which shared libraries are requires by a program by running the utility ldd

Getting Help

  • man 
    • man gcc
  • info
    • info gcc
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章