2019-2020-1 20175223 "Information Security System Design Basics," the first week of learning summary

table of Contents


Learning content summary

  • The first week of points:
    • Points 1: vim basic operation of the editor;
    • Point 2: Regular expressions base;
    • Highlights 3: Linux C programming basics:
      • Compiler GCC;
      • Debugger GDB;
      • Makefile。
    • Point 4: The first textbook, chapter VII.

  • Textbook first chapter, "computer system roaming" points:
    • Point 1: context information is the bit +;
    • 2 points: a storage device in a hierarchical structure;
    • Point 3: important topic (concept):
      • Amdahl's law;
      • Concurrency and parallelism;
      • abstract.

  • Textbook Chapter VII "link" points:
    • Point 1: static linking two major tasks:
      • Symbol Resolution;
      • reset.
    • Point 2: the target file:
    • Point 3: the symbol and the symbol table;
    • Point 4: symbol resolution:
      • How linker resolves multiple global symbol defined;
      • (Unfinished)


Textbook learning and problem-solving process

1. command error is generated binaries.

: According to the PPT

wrong command.

In the following project structure:

Use gcc -c -I/include src/say_hello.cthe command error.

src/say_hello.c:2:11: 致命错误:say_hello.h:没有那个文件或目录
    2 | # include "say_hello.h"
      |           ^~~~~~~~~~~~~
编译中断。
  • Problem Cause Analysis:
    gcc command format error, -Ishould be 头文件所在目录separated.

  • Problem Solution:

-IAnd 头文件所在目录separated, and then to generate the export file includefolder, the following command:

gcc -c -I include src/say_hello.c -o include/say_hello.o

Can generate:

[yogile@yogile-pc gcc_test]$ tree
.
├── bin
├── include
│   ├── say_hello.h
│   └── say_hello.o
├── libs
├── makefile
├── Readme.md
└── src
    ├── main.c
    └── say_hello.c

4 directories, 6 files

2. The error in the final link.

Enter the command gcc src/main.c -I include/ -L libs/ -o bin/mainerror.

[yogile@yogile-pc gcc_test]$ gcc src/main.c -I include/ -L libs/ -o bin/main
src/main.c: 在函数‘main’中:
src/main.c:5:2: 警告:隐式声明函数‘say_hello’ [-Wimplicit-function-declaration]
    5 |  say_hello();
      |  ^~~~~~~~~
/usr/bin/ld: /tmp/ccKSn7Vo.o: in function `main':
main.c:(.text+0xa): undefined reference to `say_hello'
collect2: 错误:ld 返回 1
  • Problem Cause analysis:
    the occurrence of "undefined reference to"errors have a variety of reasons (Click to view) . Here it is due to the lack of a binary file (say_hello.o) link.

  • Problem Solution:

Method One:
The beginning of the command "src/main.c"instead "src/*.c"directly to all of the source files with the re-compiled into a binary file, and then compiled together.

gcc src/*.c -I include/ -L libs/ -o bin/main

Method two:
directly to the lack of a binary file to be compiled with the command, in src/main.c -Ithe middle of adding include/say_hello.o.

[yogile@yogile-pc gcc_test]$ gcc src/*.c -I include/ -L libs/ -o bin/main
[yogile@yogile-pc gcc_test]$ bin/main 
hello word

3. static library, dynamic library command compilation step.

  • Static library
gcc -c -I include/ src/say_hello.c -o include/say_hello.o

ar rcvs libs/libsay_h.a include/say_hello.o
# 屏幕输出提示:r - include/say_hello.o

gcc src/*.c -I include/ -L libs/ -o bin/main

# 运行可执行文件即可:
[yogile@yogile-pc gcc_test]$ bin/main
hello word
  • Dynamic library
gcc -fPIC -c -I include/ src/say_hello.c -o include_so/say_hello.o

gcc -shared -o include_so/libsay_h.so include_so/say_hello.o

gcc src/main.c include_so/say_hello.o -I include/ -L libs/ -o bin/main_so

# 运行可执行文件即可:
[yogile@yogile-pc gcc_test]$ bin/main_so 
hello word
  • Project structure created
Project structure

[yogile@yogile-pc gcc_test]$ tree
.
├── bin
│   ├── main
│   └── main_so
├── include
│   ├── say_hello.h
│   └── say_hello.o
├── include_so
│   └── say_hello.o
├── libs
│   ├── libsay_h.a
│   └── libsay_h.so
├── makefile
├── Readme.md
└── src
    ├── main.c
    └── say_hello.c

5 directories, 11 files
  

4. linker resolves multiple global symbols defined rules puzzled.

The strength of the symbol resolution process there are three rules:

  1. Strong does not allow multiple symbols of the same name;
  2. If a plurality of strength weak symbol with the same name and symbol, the symbol is selected intensity;
  3. If a plurality of weak symbols of the same name, is a take any.

Puzzled 1: 2 for rules, if the main.cfile is defined weak symbols x; in the module_2.cfile, define a strong symbol x=3in main.cthe display output xis how much?
Doubts 2: For Rule 3, which in the end will be a weak symbol selection?

  • Analysis replied:

Answer puzzled 1:
For Rule 2, both strong symbol in the module definition which, in the other module calls the same symbol, the linker will choose the strong symbol of this module, the test is as follows.


Test 1

main.c

# include <stdio.h>
# include "mod.h"

int x;

int main()
{
    printf("x_start = %d\n", x);
    x=1;
    mod_2();
    printf("x = %d\n", x);
    return 0;
}

module_2.c

# include <stdio.h>
# include "mod.h"

int x=3;

void mod_2()
{
    printf("    x_2 = %d\n", x);
}

编译运行结果为:

[yogile@yogile-pc 7.6.1]$ ./mod.out 
x_start = 3
    x_2 = 1
x = 1


Answer doubts 2:
For the third rule, if they were all weak symbols (ie: not initialized, compile-time initial value 0), according to information, the so-called "random selection" means the space does not change the maximum selected test as follows.


Test 2

main.c

# include <stdio.h>
# include "mod.h"

int x;

int main()
{
    printf("x_start = %d\n", x);
    mod_2();
    x=1;
    mod_2();
    printf("x = %d\n", x);
    return 0;
}

module_2.c

# include <stdio.h>
# include "mod.h"

int x;

void mod_2()
{
    printf("    x_2 = %d\n", x);
}

编译运行结果为:

[yogile@yogile-pc 7.6.1]$ ./mod.out 
x_start = 0
    x_2 = 0
    x_2 = 1
x = 1



[Managed code]

Changed statistics.sh(ssss.sh)the find . -name "*.java"change find . -name "*.c".
The corresponding warehouse link to this page: https: //gitee.com/Yogile/Cpt_System_Yogile

  • Code submission process shots:

  • Screenshot amount of code:



Learning progress bar

The number of lines of code (add / accumulate) Blog amount (add / accumulate) Learning time (add / accumulate) Important growth
aims 4000 Line 20 280 hours
the first week 66/66 1/1 24/24
  • Plan study time: 16 hours

  • The actual study time: 24 hours


Reference material

Guess you like

Origin www.cnblogs.com/Yogile/p/11551624.html