Linux Study Notes 1-basic knowledge of Linux and introduction to C Programming in Linux

Source: Internet
Author: User
Tags time and date
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?

  1. /**
  2. Name hellword. c
  3. @ Author qinjuning
  4. */
  5. Int main ()
  6. {
  7. Printf ("Hello World" N ");
  8. Return 0;
  9. }

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?

  1. <Span style = "font-family: Microsoft yahei; font-size: 16px; "> </span> <PRE class =" CPP "name =" code ">/* Main. C */
  2. # Include <stdio. h>
  3. # Include "first_1.h"
  4. # Include "first_2.h"
  5. Int main ()
  6. {
  7. Printf_str ("this is my first Linux C ");
  8. Print_to_num (10 );
  9. Return 0;
  10. }

 

 

Two header files

[CPP]View plaincopyprint?

  1. <Span style = "font-size: 16px;">/* printf_1.h */
  2. # Ifnde _ first_h _
  3. # DEFINE _ first_h _
  4. Void printf_str (char * Str );
  5. # Endif
  6. /* Printf_2.h */
  7. # Ifnde _ second_h _
  8. # DEFINE _ first_h _
  9. Void print_to_num (INT num );
  10. # Endif
  11. </Span>

Source file for real function implementation

[CPP]View plaincopyprint?

  1. <Span style = "font-size: 16px;">/* printf_1.c */
  2. # Include <stdio. h>
  3. # Include "first_1.h"
  4. Void printf_str (char * Str)
  5. {
  6. Printf ("% S. I am com", STR );
  7. }
  8. /* Printf_2.c */
  9. # Include <stdio. h>
  10. # Include "first_2.h"
  11. Void print_to_num (INT value)
  12. {
  13. Int I = 0;
  14. For (; I <value; I ++)
  15. Printf ("cur value is % d ##\ N", I );
  16. } </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

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.