Chapter 4 compiling high-quality GNU/Linux software
1. Interaction with the running environment
Get parameters: getopt and getopt_long functions are used.
Header file # include <unistd. h> or <getopt. h>
Function prototype int getopt (INT argc, char * const argv [], const char * optstring );
Int getopt_long (INT argc, char * const argv [], const char * optstring, const struct option * longopts, int * longindex );
Parameter description: argc and argv are the number and content of parameters passed by main.
The optstring parameter is the option string, indicating which option can be processed by getopt () and which option requires a parameter,
If the option character is not followed by a colon, it indicates that the parameter option does not contain parameters, and the parameter is ignored during processing.
If the option character is followed by a colon ":", the parameter option contains parameters, and the string following the parameter option is the parameter of this option
If the option character is followed by a double colon ":", the parameter of this option is optional, depending on the specific command. (Not all Linux systems support this function)
Longopts is a structure array used to specify the length parameter corresponding to a short parameter. The following structure is used:
Struct option {
Const char * Name; // name of the long option
Int has_arg; // whether the parameter exists: 0 does not
Int * flag; // how to return the result of the long option, usually null
Int val; // the result to be returned. It can be understood as the corresponding short option.
}; Similar to this:
Const struct option long_options [] = {
{"Help", 0, null, 'H '},
{"Output", 1, null, 'O '},
{"Verbose", 0, null, 'V '},
{Null, 0, null, 0} // all fields of the last element in the array should be 0
};
Extern char * optarg;
Extern int optind, opterr, optopt;
The global variable optarg points to the parameter of the current option. If there is no parameter, It is null.
2. stdout is buffered.
In C ++, there is also a difference between cout and cerr. Note that the Endl operator will execute the refresh operation in addition to the output line break. If you do not want to perform the refresh operation (for example, for the sake of running efficiency), use the constant '\ n' to indicate the line break.
3. environment variables:
The environ array contains all environment variables and requires extern char ** environ;
Do not directly modify the environ variable. to modify the environment variable, use the setenv and unsetenv functions.
4. Temporary Files
5. Compile and use the library
Archive files
6. Shared Library
7. Dynamic Loading and Unloading
Chapter 3 threads
1. How to clean threads in C ++