Basic concepts of Linux Program Development

Source: Internet
Author: User
Tags constant definition
Basic concepts of Linux program development-general Linux technology-Linux programming and kernel information. The following is a detailed description. When setting the Linux system path, use a colon to separate each path name. For example:

PATH = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11"

There are two types of programs in linux, one is executable program, which is similar to the. bat file in Windows, and the other is a script.

The storage paths of commonly used programs in Linux are as follows:

/Bin. This path stores the programs required for system startup.

/Usr/bin. This path stores the standard programs you need.

/Usr/local/bin. This path stores locally installed programs.

Linux uses the slash "/" to separate path names, rather than the Windows Backslash "\".

In Linux, the C compiler uses GCC. for historical reasons, in POSIX-compatible operating systems, the C compiler is called cc, so there is also a cc command in Linux, it is a soft link to gcc.

Most development tools are located in the/usr/bin or/usr/local/bin directory.

Header file in the/usr/include directory. The header file contains the declaration of constant definition, system call, and library function call. This is the default path for storing system header files. During program compilation, the compiler automatically searches for this directory. The gcc compiler can also use the-I parameter to specify another header file path when compiling a program. For example:

Gcc-I/usr/local/myinclude test. c.
Library file. The Library is a set of compiled functions, which facilitates code reuse. It is stored in the/lib and/usr/lib directories by default. Library files can be classified into static files and shared files.

. A: static library file. Using static libraries will introduce all the library code into the program, occupying more disk space and memory space. Therefore, we recommend that you use shared libraries.

. So, shared library file. The program that uses the shared library does not contain the library code. The code in the shared library is called only when the program is running.

You can use the library file name containing the path or the library file specified with the-l parameter during compilation./usr/lib/libm. a is equivalent to-lm. For example:

Gcc-o hello. c/usr/lib/libm.
Or use the-l parameter.
Gcc-o hello. c-lm
If the library file we want to use is not in the default location, you can use the-L parameter to specify the path of the library file when compiling the program. The following example uses the libhello library file in the/usr/hello/lib directory:

Gcc-o hello-L/usr/hello/lib hello. c-lhello
Create and use static databases.

Create two functions respectively. The content of function a is as follows:

# Include

Void a (char * arg)
{
Printf ("function a, hello world % s \ n", arg );
}
The content of function B is as follows:

# Include

Void B (int arg)
{
Printf ("function B, hello world % d \ n", arg );
}
Then, two object files are generated.

Debian :~ /C # gcc-c a. c B. c
Debian :~ /C # ls *. o
A.o B .o
Finally, use the ar archive command to package the generated object file into a static library libhello..

Debian :~ /C # ar crv libhello. a. o B. o
R-a. o
R-B. o
Define a header file lib. h for our static library, which contains the definitions of these two functions.

/*
* This is a header file.
*/
Void a (char * arg );
Void B (int arg );
}}}
* Create the jims. c program. The content is as follows. {{{#! Cplusplus
# Include "lib. h"

Int main ()
{
A ("jims. yang ");
B (3 );
Exit (0 );
}
Compile the program using the static Link Library.

Debian :~ /C # gcc-c jims. c
Debian :~ /C # gcc-o jims. o libhello.
Debian :~ /C #./jims
Function a, hello world jims. yang
Function B, hello world 3
Debian :~ /C #
Gcc-o jims. o libhello. a can also be written as gcc-o jims. o-L.-lhello

Preprocessing: a command starting with "#" in a program is a preprocessing command, which is processed by the Preprocessing Program during syntax scanning and analysis. Preprocessing includes the following types:

Macro definition, defined using the # define command. For example, # define BUFFER 1024. Cancels the # undef command for macro definition. Macros can also contain parameters, such:

# Define BUF (x) x * 3
Contains the header file. You can use the # include command to insert the included file code to the current position. For example:

<# Include .
The included files can be enclosed in angle brackets or double quotation marks, for example:

# Include "stdio. h ".
The difference is that angle brackets are used to search for the file under the system's include directory (/usr/include), while double quotation marks indicate to search for the include file under the current directory. Each line can contain only one containing file. To contain multiple files, use multiple # include commands.

Conditional compilation in the following format:

Format 1: If an identifier is defined, compile program segment 1; otherwise, compile program Segment 2:
# Ifdef identifier
Procedure 1
# Else
Procedure 2
# Endif

Format 2: If an identifier is defined, compile program segment 2; otherwise, compile program segment 1, which is the opposite of the format:
# Ifndef identifier
Procedure 1
# Else
Procedure 2
# Endif

Format 3: If the constant expression is true, compile program segment 1; otherwise, compile program Segment 2:
# If constant expression
Procedure 1
# Else
Procedure 2
# Endif

It takes four steps to compile a program using gcc.

Pre-Processing. You can use the-E parameter to generate Pre-processed files.

Debian :~ /C # gcc-E hello. c-o hello. I
Compile (Compiling)

Assembly)

Link (Linking)

By default, GCC regards the. I file as the C language source code after preprocessing, so we can compile the. I file into the target file.

Debian :~ # Gcc-c hello. I-o hello. o }}}
Using the-pedantic option in GCC can help programmers find some codes that do not conform to the ANSI/iso c standard, but not all of them. From the programmer's perspective, the function library is actually a collection of header files (. h) and library files (. so or..

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.