Linux Programming Reading pen 1

Source: Internet
Author: User

Introduction to Chapter I.

Linux applications behave as two special types of files: Executable + script files

/bin binary Files directory, the standard program used to store the boot system

/usr/bin user binaries directory, storing standard programs for users

/usr/local/bin local binary files directory, software installation program

/sbin and/usr/sbin root user, storage System Management Program

/OPT Storage of optional operating system components and third-party applications

First Linux program: Hello World Program

hello.c

#include <stdio.h>#include<stdlib.h>int  main () {  "Hello world\n"  );  Exit (0);          }

Gcc-o Hello hello.c

./hello can run print Hello World

Some important directories and files are introduced in Linux development:

1 applications

Generally in/usr/bin,/usr/local/bin,/opt

2 Header Files

Generally in the/usr/include

At compile time, you can use the-I flag to include header files that are stored in subdirectories or nonstandard locations, such as:gcc-i/usr/openwin/include fred.c

You can use the grep command to search for files that contain certain keywords, such as grep exit_ *.h to search for strings in all files ending in. h in the current directory Exit_

3 Library files

A library is a set of pre-compiled functions. Standard library files are typically located in/lib and/usr/lib

The library file name always begins with Lib, and the subsequent section indicates what library (c is the C language library and M stands for the math library). The last part of the file name starts with., and then gives the type of the library file:

. A represents a static library

. So stands for shared libraries

You can tell the compiler to search for the library file by giving the full library file pathname or by using the-l flag:

  Gcc-o Fred Fred.c /usr/lib/libm.a equivalent to

  Gcc-o Fred Fred.c -lm (-LM is shorthand, representing a library of libraries named LIBM.A in the standard library directory)

You can add a search path for the library to the compiler through the-l flag:

  gcc-o x11fred -l/usr/openwin/lib x11fred.c -lx11 This command compiles and links the program with the LIBX11 library in the/usr/openwin/lib directory x11fred

4 Static libraries

Also known as archive files, conventions generally end with. A

Experiment: Create your own static library, contain two functions, and then call one of the functions in one of the sample programs

Step 1: Create your own source files Fred.c and bill.c for each of the two functions, respectively:

fred.c#include<stdio.h>voidint  arg) {    "fred:we passed% d\n", Arg);} Bill.c#include<stdio.h>voidchar* Arg) {    "fred:we Passed%s\n", Arg);}

Step 2: Compile these functions separately to produce the target files to be included in the library file

The gcc- c bill.c fred.c -C option prevents the compiler from creating a complete program, although attempting to create a complete program will not succeed at this point because the main function is not defined in FRED.C and BILL.C

There will be two more files in the current directory BILL.O FRED.O

Step 3: Create a header file for the library file

Named Lib.h (random name), Lib.h will declare the function signature in the library file. It's a good idea to include lib.h in source files Fred.c and bill.c.

The contents of the Lib.h are as follows:

Bill of Void (char*);

void Fred (int);

Step 4: Write the source program that invokes the Bill function program.c

#include <stdio.h>#include"lib.h"  // header file containing the library!!!  int  main () {    "HelloWorld"  );    Exit (0);}

Step 5: Compile and test the PROGRAM.C

  Gcc-c PROGRAM.C

  Gcc-o program PROGRAM.O BILL.O

./program output: Bill:we passed Hello World

Step 6: Create and use a library file

Use the AR Archive command to create an archive file and add your target file:

    ar crv libfoo.a bill.o fred.o Archive Two target files to a static library named LIBFOO.A using AR command

Step 7: Generate a table of contents for the library (this step is generally not required, but it does not hurt), use the Ranlib command

ranlib libfoo.a

So far, your library has been created and can be used as follows:

gcc-o program program.o libfoo.a(or write gcc-o program program.c libfoo.a or write Gcc-o progra M PROGRAM.C- l-lfoo or written gcc-o program- L. program.c-lfoo,-L. Indicates the current directory! )

./program

Linux Programming Reading pen 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.