Document directory
- I. Linux operating system source
- Ii. Basic Linux knowledge
- Iii. Introduction to C Programming in Linux
This article is original and must be reproduced with the following source:Http://blog.csdn.net/qinjuning
Starting from today, I made up my mind to step into the Linux Hall step by step. When I picked up Linux again, I still remember how to learn Linux from the college age,
After a few days, the power will disappear under her confused coat. Now I am studying the Android system, and for many other purposes, I am determined
Good knowledge.
Source of materials for this study note:
1. laruence's Linux Private food _ basic learning (electronic files)
2. Two blog posts on the knowledge of C Programming GCC commands in Linux
C language programming in Linux-Basic KnowledgeAndLinux C programming practices
I. Linux operating system source
Everybody knows, omitting .....
Ii. Basic Linux knowledge
Note: I have not properly digested the book from laruence. Continue to work harder.
1. linnux command line Introduction
For example, the common command for listing files/folders in the current directory is LS-Al main.
Note: ls is a command.
-"Options A" indicates listing hidden files; "L" indicates listing details of files/files.
If main is parameter1, only files/folders starting with "Main" are listed.
Other common Commands include:
Ls lists all files/folders in the current directory
Data to list the current time and date
CD switching path
Of course, there are so many commands-there are too many commands and every usage is too large, we certainly cannot remember that, so the most powerful command is man.
Command. The command is used as follows:
Man command
You can see all the usage of this command.
2. Linux file attributes:
After entering Linux, type the following command LS-Al in any directory (this command means to list the information of all files and folders in detail), you can see
The following information is added:
Attribute description:
The information displayed in the first list is the most important, as shown in the figure below:
Attribute details:
Other attributes are relatively easy to understand.
3. Important directories in Linux
Some important directories include:
Iii. Introduction to C Programming in Linux
If you are familiar with programming in Windows, you may not get used to C Programming in Linux for the first time, because Linux does not provide integrated development environment IDE for us,
For example, VC and Visual Studio in windows. But C Programming in Linux can bring you different feelings. It can familiarize you with how the compiler generates
Steps of executable files.
Generally, the following two steps are required to generate an executable file:
1. Compile: Compile the source file to generate the target file.
2. Link: link the target file to generate an executable file.. (In windows, the name is generally ended with .exe;
In Linux, it is marked with the file property-X)
Finally, run the executable file to view the program output.
It is emphasized that, for some program compilation norms and methods, in general, whether it is C, C ++, or pas, the source file must be compiled into an intermediate
The code file is in windows, that is, the. OBJ file, and the. o file in UNIX, that is, the object file. This action is called compile ). Then
A large number of object files are merged into execution files. This action is called Link ).
To sum up, the source file will first generate the intermediate target file, and then the intermediate target file will generate the execution file. During compilation, the compiler only checks program syntax and Function
Number, whether the variable is declared. If the function is not declared, the compiler will give a warning, but the object file can be generated. When linking a program, the linker will
Find the implementation of the function in the object file. If it cannot be found, the Link error code (linker error) will be reported. In VC, this error is generally: link 2001
Error, which means that the linker cannot find the function implementation. You need to specify the object file of the function.
A preliminary understanding of the C compiler GCC command in Linux
The GCC compiler is the most important development tool on the Linux platform. It is the gnu c and C ++ compilers. Its basic usage is as follows:
GCC [Options] [filenames]
NOTE: For the convenience of the following expressions, it is assumed that there is a simple C source file, helloword. c
[CPP]View plaincopyprint?
- /**
- Name hellword. c
- @ Author qinjuning
- */
- Int main ()
- {
- Printf ("Hello World" N ");
- Return 0;
- }
The simplest compilation method is GCC hellowrold. C. It will generate the default file name a. Out for the target program, and directly execute the executable file to see the output.
For example, enter the following command in shell:
First step: GCC helloworld. C (enter the Enter key) Result: The a. out executable file is generated.
Second:./A. out result: execute this file to view the program output.
Options is the compilation option. GCC provides more than 100 compilation options, but only a few are frequently used. We will only introduce several common options.
-OMeaning: generate the target file and name it,
For example, if gcc-O helloworld. C is used, the generated executable file is hello, not a. Out.
-CMeaning: The-C option tells GCC to only compile the source code as the target code and skip the Assembly and connection steps. The default file name is [file_name]. O.
PS: Due to the header file and reference relationship, we also need to use the GCC-O command to compile the relevant target file to generate an executable file.
Assume that the following source code exists, including the master file and header file.
Main file: Main. c
[CPP]View plaincopyprint?
- <Span style = "font-family: Microsoft yahei; font-size: 16px; "> </span> <PRE class =" CPP "name =" code ">/* Main. C */
- # Include <stdio. h>
- # Include "first_1.h"
- # Include "first_2.h"
- Int main ()
- {
- Printf_str ("this is my first Linux C ");
- Print_to_num (10 );
- Return 0;
- }
Two header files
[CPP]View plaincopyprint?
- <Span style = "font-size: 16px;">/* printf_1.h */
- # Ifnde _ first_h _
- # DEFINE _ first_h _
- Void printf_str (char * Str );
- # Endif
- /* Printf_2.h */
- # Ifnde _ second_h _
- # DEFINE _ first_h _
- Void print_to_num (INT num );
- # Endif
- </Span>
Source file for real function implementation
[CPP]View plaincopyprint?
- <Span style = "font-size: 16px;">/* printf_1.c */
- # Include <stdio. h>
- # Include "first_1.h"
- Void printf_str (char * Str)
- {
- Printf ("% S. I am com", STR );
- }
- /* Printf_2.c */
- # Include <stdio. h>
- # Include "first_2.h"
- Void print_to_num (INT value)
- {
- Int I = 0;
- For (; I <value; I ++)
- Printf ("cur value is % d ##\ N", I );
- } </Span>
The compilation procedure is as follows:
1. Compile: generate the target file for different source files (xxx.0)
Gcc-C main. c generates the main. O target file
Gcc-C printf_1.c generates the printf_1.o target file
Gcc-C printf_2.c generates the printf_2.o target file
2. Link: link the target file and form an executable file.
Gcc-C main. O printf_1.0 printf_2.o can generate the executable file main
3. Enter./main to execute the file and view the program result.
For more GCC commands and debugging methods, refer to the following two blogs:
1,C language programming in Linux-Basic Knowledge
2,Linux C programming practices