Embedded introductory exercise_3
- Use gcc to generate .a static library and .so dynamic library
- The generation and use of static library .a and .so library files under Linux
- Independent exercises on the generation and use of static libraries
- Independent exercises on the generation and use of dynamic libraries
- GCC commonly used commands
- Common use of GCC assembly and Binutils and ELF files
- Comparison of the use of nasm command and the size of the executable file generated
- The main functions of curses, write out the names and functions of several basic functions
- Experience BBS in win10 system
- Install the curses library, and the installation location of the header files and library files
- Use gcc to compile and generate a terminal game and run it (snake game).
- to sum up
Use gcc to generate .a static library and .so dynamic library
Example 1: Write three files, including the function definition header file hello.h, the function content c file hello.c, and the main program c file main.c for testing.
The procedure is:
hello.h:
#ifndef HELLO_H
#define HELLO_H
void hello(const char*name);
#endif//HELLO_H
hello.c:
#include<stdio.h>
void hello(const char*name)
{
printf("Hello%s!\n",name); }
main.c:
include"hello.h"
int main()
{
hello("everyone");
return 0;
}
程序作用是调用hello输出Hello everyone
First, use the file editor to edit three files:
Second, both static libraries and dynamic libraries are created by .o files. Therefore, we must first compile the source program hello.c into an .o file through gcc:
3. Create a static library from the .o file, and type the following command at the system prompt to create the static library file libmyhello.a:
4. In the program Use the static library in:
5. Check whether the public function hello is really connected to the target file hello, delete the static library, and test whether the program runs normally: After
deleting the static library, the program runs normally, indicating that the internal function has been connected to the target code.
6. Create a dynamic library with hello.o, type the following command at the system prompt to get the dynamic library file libmyhello.so:
7. Use the dynamic library in the program, copy the file libmyhello.so to the directory /usr/lib, otherwise When calling the dynamic library, the system will not find this file, call and generate the hello2 executable file, and run:
Note: The dynamic library file is copied to the user's lib directory, and the dynamic library file must be kept in the original directory, otherwise the system will not find the dynamic library, resulting in compilation failure.
The generation and use of static library .a and .so library files under Linux
1. Preparation stage; create four files, respectively
A1.c:
#include <stdio.h>
void print1(int arg)
{
printf("A1 print arg:%d\n",arg); }
A2.c:
#include <stdio.h>
void print2(char *arg)
{
printf("A2 printf arg:%s\n", arg); }
A.h:
#ifndef A_H
#define A_H
void print1(int);
void print2(char *);
#endif
test.c:
#include <stdlib.h>
#include "A.h"
int main()
{
print1(1);
print2("test");
exit(0); }
2. Static library generation: The static library is generated by the .o file, so first compile the c file to become the .o file:
3. Next, generate a static library named libafile.a from two .o files.
4. Call the library The file is compiled and then executed.
5. The generation of the shared library .so file: the dynamic library is also generated based on the .o file, so first compile the c file to become the .o file:
6. Generate the dynamic library based on the .o file:
after generating the dynamic library The library must be copied to the user's lib, otherwise the system cannot find the library and cannot be called.
7. Use the .so library file to create an executable program:
Independent exercises on the generation and use of static libraries
Topic: In addition to the x2x function, expand and write a x2y function (function self-defined), the main function code will call x2x and x2y; write these 3 functions into 3 separate .c files, and use gcc to compile them to 3 One .o object file; use the ar tool to generate one .a static library file for the x2x and x2y object files, and then use gcc to link the object file of the main function with this static library file to generate the final executable program
. Write three main programs:
2. Compile files to generate .o files:
3. Generate static library files based on x2x.o and x2y.o:
Fourth, call the static file to connect mian.c to generate an executable file, and run:
Independent exercises on the generation and use of dynamic libraries
1. Perform dynamic library exercises on the basis of the previous static library exercises, and also generate dynamic libraries based on the .o file:
2. Copy the dynamic library to the user's lib directory:
3. Call the static file to connect mian.c to generate an executable file, and run:
GCC commonly used commands
1. The detailed compilation process of gcc, preprocessing:
2. Compiling into assembly code:
3. Assembling:
4. Linking and running:
5. Compiling multiple program files:
gcc -c test1.c -o test1.o
gcc -c test2.c -o test2.o
gcc test1.o test2.o -o test
Six, library file connection
gcc –c –I /usr/dev/mysql/include test.c –o test.o
gcc –L /usr/dev/mysql/lib –lmysqlclient test.o –o test
Seven, use static link library when compulsory linking
gcc –L /usr/dev/mysql/lib –static –lmysqlclient test.o –o test
Common use of GCC assembly and Binutils and ELF files
1. After preprocessing, some fields of the .i file:
2. Some fields in the .s file after compiling the file:
3. Use the as command in Binutils to assemble:
4. Link:
1. Use the dynamic library to link:
2. Use a static library for linking:
Through comparison, it is found that the generated ELF file has a large size gap due to the two different connection forms. It can be seen that dynamic is more space-saving than static.
5. View the information of each part of the ELF file:
6. Disassemble the ELF:
objdump -D hello
Comparison of the use of nasm command and the size of the executable file generated
Obviously comparing the size of the executable file, it is found that the executable file generated by compiling with the nasm command is much smaller.
The main functions of curses, write out the names and functions of several basic functions
Curses works on screens, windows and sub-windows. The screen is the total available display area of the device (for the terminal, all available character positions in the window), and the window is related to specific routines. Such as the basic stdscr window, etc.
关于屏幕的常用部分代码:
int addch(const chtype char_to_add);//当前位置添加字符
int addchstr(chtype *const string_to_add);//当前位置添加字符串
int printw(char *format, ...);//类似与printf
int refresh(void);//刷新物理屏幕
int box(WINDOW *win_ptr, chtype vertical, chtype horizontal);
//围绕窗口绘制方框,用vertical绘制垂直边,用horizontal绘制水平边
int insch(chtype char_to_insert); //插入一个字符(已有字符后移)
Experience BBS in win10 system
In win10 system, "Control Panel" -> "Programs" -> "Enable or Close Windows Functions", enable "telnet client" and "Windows Subsystem for Linux". Then open a cmd command line window and enter telnet bbs.newsmth.net on the command line
Install the curses library, and the installation location of the header files and library files
安装代码:
sudo apt-get install libncurses5-dev
Use gcc to compile and generate a terminal game and run it (snake game).
Write the Snake source code, gcc compiles and runs the executable file:
to sum up
This experiment is very important for getting started with a preliminary understanding of static libraries and dynamic libraries. In the creation and use of static libraries and dynamic libraries, I have a deep understanding of the difference between static libraries and dynamic libraries, and the difference between the working conditions of the two. The basic structure and working principle of library files have more understanding, especially the assembly process of executable programs. In the process of exploring, you can clearly feel that the difficulty lies in that the dynamic library needs to enable administrator permissions when copying, and the original directory There must also be a dynamic library under. In addition, I understand the ELF file format, assembly language format, and tools for working with gcc in compiling. There is also a distribution execution experiment at compile time of gcc, which gave me new insights into the working principle of gcc.