Linux programming (i) Getting Started

Source: Internet
Author: User

1. Linux Programs

Linux applications behave as two special types of files: executables and script files .

Executable: is a program that calculates the ability to run directly, equivalent to a Windows. exe file.

Script file: A collection of sets of instructions. These directives are executed by another program (that is, the interpreter), which is equivalent to a. bat file, a. cmd file under Windows.

Use (:) to separate entries for the path variable. MS-DOS and Windows use semicolons (;).

Linux uses forward slashes (/) to separate directory names in filenames.

2. C language Compiler

Compile, link, and run the Hello program:

[Email protected]:~/mypro$ gcc-o Hello hello. C[email protected]-virtualbox:~/mypro$./!

The program name (hello) plus "./" is a program that instructs the shell to execute the given name under the current directory.

The-o name option tells the compiler where to place the executable program. If you do not, the compiler places the program in a file called a.out (assembler output, which is the assembly outputs).

3. Development system Guidance

3.1 Applications

(1)/usr/bin: The program provided by the system for normal use, including tools for program development, can be found in the directory/usr/bin.

(2)/usr/local/bin or/OPT: A program that the system administrator adds to a particular host or network.

(3) The system administrator prefers to use the/usr/local directory, as it separates the vendors from the applications that the vendor provides and subsequently adds files to the system itself.

3.2 Header Files

(1) For C language, the header file is in the/usr/include directory and its subdirectories. Header files that depend on a particular Linux version are usually found in/usr/include/sys and/usr/include/linux.

(2) When invoking the C language compiler, you can use the-I flag to include an include file that is saved in a subdirectory or in a nonstandard location.

gcc-i/usr/openwin/include fred.c

The previous example indicates that the header files contained in the FRED.C are also found in the/usr/openwin/include directory, not only in the standard location.

3.3 Library files

A library is a set of pre-compiled functions.

Standard system library files are typically stored in the/lib and/usr/lib directories.

. A: Traditional library of static functions.

. So: Shared function library.

-L: Flag indicates the library file to be searched by the compiler.

-L: The compiler adds a search path to the library.

3.4 Static Libraries

Static libraries, also known as Archive Files (archive).

Static Library Experiment:

(1) Create a source file for two functions, ADD.C and SUB.C:

Add.c
#include <stdio.h>

int add (int a, int b) {
return a + B;
}

// sub.c#include <stdio.h>int sub (intint  b) {        return A - b;}

(2) Compile these functions separately, producing the target files to be included in the library file.

[Email protected]:~/mypro$ gcc-C add.c sub.c[email protected]-virtualbox:~/mypro$ lsadd.c  ADD.O  hello.c  sub.c  sub.o

-C: Prevents the compiler from creating a complete program. (-c Compile and assemble, but does not link)

(3) Create a header file that will declare the functions in the library file.

// Lib.h int Add (intint); int Sub (intint);

(4) The Calling program (PROGRAM.C).

//PROGRAM.C#include <stdio.h> #include"Lib.h"intMain () {printf ("5 + 4 =%d\n", Add (5,4)); printf ("5-4 =%d\n", Sub (5,4)); return 0;}

(5) Compile and test the program.

[Email protected]:~/mypro$ gcc-c program.c[email protected]-virtualbox:~/mypro$ gcc-o program PROGRAM.O ADD.O Sub.o[email protected]-virtualbox:~/mypro$ lsadd.c  add.o  hello.c  lib.h  Program  program.c  program.o  sub.c  sub.o[email protected]-virtualbox:~/mypro$./5  495)41

(6) Create and use a library file.

Use the AR program to create an archive file and add the target file.

[Email protected]:~/mypro$ lsadd.c  add.o  hello.c  lib.h  program  program.c  PROGRAM.O  SUB.C  sub.o[email protected]-virtualbox:~/-- sub.o[email protected]- virtualbox:~/mypro$ lsadd.c  add.o  hello.c  libcalc.a  lib.h  program  PROGRAM.C  program.o  sub.c  sub.o

(7) To successfully use the library, you also need to generate a table of contents for the library of functions.

Randlib LIBCALC.A

Add the library file to the list of files in the compiler command line to create our program:

[Email protected]:~/mypro$ lsadd.c  add.o  hello.c  libcalc.a  lib.h  program.c  PROGRAM.O  SUB.C  sub.o[email protected]-virtualbox:~/mypro$ gcc-o program PROGRAM.O libcalc.a[email protected ]-virtualbox:~/mypro$ lsadd.c  add.o  hello.c  libcalc.a  lib.h  Program PROGRAM.C  program.o  sub.c  sub.o[email protected]-virtualbox:~/mypro$./program   549541

NM command: View the functions contained in the target file, function library, or executable program.

3.5 Shared libraries

When a program uses a shared library, it is linked in such a way that it does not contain function code by itself, but rather refers to shared code that is accessible at run time.

Comparison of Linux and Windows files:

Project Unix Windows
Target Module . o . obj
static function library . A . lib
Program Program (a.out) Program.exe
Shared libraries . So . dll

Reference: The 3rd edition of Linux Programming

Linux programming (i) Getting Started

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.