5 DERECHA

linux 2 본문

Linux

linux 2

kay30 2023. 4. 10. 11:28

Assignment 

sudo apt install vim

: I missed that part,, the thing was that it only downloaded vi in linux ubuntu,,, so it is working in vi,, not vitm[vim command]p : pastey : copyv : selectdd : delete

 

 

way1
way2
way3
way4

★★★ sh -x homeworkd.sh a (you can see all command in script)

 

 

 

1. Understanding the Linux Development Environment

1) Build & Host & Target

1. Build: The build system, also known as the development system, is the machine where the software for the embedded Linux system is compiled, linked, and packaged. It typically includes a development environment with compilers, libraries, and tools needed to build the software for the target system.

2. Host: The host system, also known as the development host, is the machine where the build system runs. It is used to compile and build the software for the target system. The host system's operating system and architecture may be different from the target system's operating system and architecture.

3. Target: The target system, also known as the embedded system, is the actual hardware where the compiled software will be deployed and executed. It typically includes the processor, memory, storage, and other hardware components necessary to run the embedded Linux system. The target system's operating system and architecture may be different from the host system's operating system and architecture.

In summary, the build system is where the software for the embedded Linux system is compiled, the host system is where the build system runs, and the target system is the actual hardware where the compiled software is deployed and executed. These three components work together to develop and deploy software for embedded systems running Linux.

Source code could be same but binary file will be different.

2) Tool-Chain

A toolchain in software development refers to a series of tools that are used together to create software. It typically includes a set of compilers, linkers, assemblers, and other software development tools that are used to translate source code into executable code or other target formats.

The build process is the process of compiling, linking, and assembling source code into an executable program or library that can be run on a computer or embedded system. The build process typically consists of several stages, and the specific steps may vary depending on the programming language, platform, and build system being used.

Here's a general overview of the toolchain and build process:

1.Source code: Developers write the source code for their software using a programming language such as C, C++, Java, or Python.

2. Compiler: The source code is fed into a compiler, which translates the code into machine-readable instructions or an intermediate form such as object code.

3. Linker: The linker takes the object code produced by the compiler and combines it with libraries and other object code to create an executable program or library. It resolves references to symbols and addresses between different object files, ensuring that the final binary is complete and can be executed.

4. Assembler: If the software includes any assembly language code, an assembler is used to translate the assembly code into machine code that can be executed directly by the computer's processor.

5. Build system: A build system is a set of tools and scripts that automate the build process. It manages dependencies, compiles source code, and orchestrates the various stages of the build process. Popular build systems include make, CMake, and Gradle.

6. Build configuration: Developers may provide build configurations that specify various options and settings for the build process, such as compiler flags, optimization settings, and target platform specifications. These configurations help ensure that the build process produces the desired output.

7.Output: The output of the build process is an executable program or library that can be run on a computer or embedded system.

In summary, the toolchain and build process are critical parts of the software development lifecycle, allowing developers to translate source code into executable software. It involves various stages such as compilation, linking, assembling, and automation through build systems, resulting in an output that can be executed on a target platform.

3) GCC, Library, C-runtime

In software development, specifically in C/C++ programming, gcc is a popular compiler toolchain used to compile and build C/C++ source code into executable programs or libraries. The -c flag is used with gcc to generate object files (.o) from C source files (.c) without linking them, while the ld command is used to link object files into an executable program.

Here's a brief overview of the commands:

gcc -c *.c: This command invokes the gcc compiler with the -c flag, which tells gcc to compile the C source files (.c) into object files (.o) without performing the linking step. The *.c wildcard specifies that all C source files in the current directory should be compiled. The resulting object files contain machine code representations of the compiled source code, but they are not yet complete executables because they are not linked with libraries and other object files.

ld *.o: This command uses the ld linker to link the object files (*.o) generated by the gcc -c command. The *.o wildcard specifies that all object files in the current directory should be linked. The linker resolves references to symbols and addresses between the object files, and combines them with any libraries specified, to create an executable program or library.

Note that in practice, the gcc command is often used for both compilation and linking, as it includes the necessary linker functionality. The -c flag is used to stop the process after compilation, generating object files, without proceeding to the linking step. The ld command is typically used directly when more fine-grained control over the linking process is needed, such as when linking with specific libraries or configuring linker options.

In summary, gcc -c is used to compile C source files into object files without linking, and ld is used to link object files into an executable program or library. These commands are commonly used in C/C++ build processes to transform source code into executable software.

 

Let's walk you through ld !

ld *.o /usr/lib/x86_64-linux-gnu/crt1.o -lm -lc:

ld: This is the command to invoke the linker.

*.o: This wildcard specifies that all object files (.o) in the current directory should be linked. These are the object files generated by compiling C source files (.c) using a compiler like gcc.

/usr/lib/x86_64-linux-gnu/crt1.o: This is a specific object file that serves as the C runtime startup file for the Linux x86_64 platform. It contains the entry point and startup code required for a C program to execute correctly on Linux.

-lm: This is a linker flag that specifies the math library (libm) should be linked. The math library provides mathematical functions such as sin, cos, sqrt, etc., that are commonly used in C programs.

-lc: This is a linker flag that specifies the C library (libc) should be linked. The C library provides standard C functions and runtime support required for C programs.

In summary, the ld command is used to link object files (*.o) along with specific libraries such as the C runtime startup file (crt1.o), math library (libm), and C library (libc) to create an executable program or library. These libraries provide essential runtime support and functionality required for C programs to execute correctly.

 

4) LD Library

** 링커에서 해야하는 것들 c runtime, 관련된 library, dynamic linker

=> 사실은 gcc안에 링킹 과정까지 포함되어있다. 근데 나눠논 이유는 점진적 빌드(Incremental build)를 하기 위해서,,!

In the context of software development, a linker (such as ld) is a tool that is used during the building or linking phase of compiling a program to create an executable program or library from object files generated by a compiler.

A library, also known as a shared library or dynamic link library (DLL), is a collection of pre-compiled object files that are packaged together into a single file. Libraries contain compiled code and data that can be linked with other code at runtime, allowing software developers to reuse common functionality across multiple programs without duplicating code.

When linking a program, the linker may need to reference libraries in order to resolve references to symbols (e.g., functions or variables) used in the program but defined in external code. The linker searches for these symbols in the libraries specified during the linking process and resolves the references by linking them with the corresponding object files or library code.

There are two types of libraries commonly used in C/C++ programming:

1. Static libraries: These libraries are included in the executable program during the linking process, resulting in a self-contained executable. Static libraries are linked directly into the executable, and the resulting binary contains all the code from the libraries. Static libraries have the file extension .a on Unix-like systems and .lib on Windows.

2.Shared libraries: These libraries are loaded at runtime when the program is executed. Shared libraries are linked dynamically during runtime, allowing multiple programs to share the same library in memory, which can help reduce the size of the executable programs. Shared libraries have the file extension .so on Unix-like systems and .dll on Windows.

Linking with libraries is a way to leverage existing code and reuse common functionality, which can help reduce development effort and improve maintainability of software projects. The linker, such as ld, is responsible for resolving references to symbols in libraries and combining them with object files to create a complete executable program or library.

 

2. Makefile

A Makefile is a script that contains instructions for building and compiling software projects. It is used with the make utility, which is a build automation tool commonly used in software development to automate the process of building executable programs or libraries from source code.

A Makefile typically consists of rules, each of which defines how to build a target (e.g., an executable or a library) from dependencies (e.g., source files or other targets). Here's a breakdown of some key components of a Makefile:

1.Variables: Variables are used to store values that can be reused throughout the Makefile. They are defined with an assignment statement using the = or := operators. For example:

In this example, CC is a variable that stores the compiler command (e.g., gcc), and CFLAGS is a variable that stores compiler flags (e.g., -Wall -O2).

2. Targets: Targets are the desired output files to be built by the Makefile. They are defined with a target name followed by a colon (:). For example:


In this example, all is the target name, and program is the dependency that needs to be built before the all target.

3.Dependencies: Dependencies are the files or targets that are required to build a target. They are listed after the target name, separated by spaces. For example:

In this example, main.o, foo.o, and bar.o are the dependencies of the program target. If any of these dependencies are modified, the program target will be rebuilt.

4.Commands: Commands are the actions that are executed to build a target. They are indented with a tab character (\t) and are executed sequentially. For example:

In this example, the command $(CC) $(CFLAGS) -o program main.o foo.o bar.o is the action that is executed to build the program target. $(CC) and $(CFLAGS) are variables that are expanded to their respective values (gcc and -Wall -O2) during runtime.

Here's a simple example of a Makefile for building a C program with two source files, main.c and foo.c, and a header file foo.h:

In this example, the Makefile defines the CC and CFLAGS variables for the compiler command and flags, respectively. It also defines the all, program, main.o, and foo.o targets, along with their dependencies and commands to build them. The clean target is used to remove the built executable and object files.

1)  Suffix Makefile

A suffix Makefile is a type of Makefile that uses suffix rules to automatically generate target rules based on file extensions. It allows the Makefile to automatically determine the appropriate compiler and flags based on the file extension of the source files. Suffix rules are defined using the % character as a wildcard, and they are used to generate target rules for building object files from source files with specific extensions, such as .for C source files or .cpp for C++ source files.

make file Suffix rule

 

3. Build Rootfs

1) Kernel

The kernel is the core component of an operating system that manages system resources, provides an interface for user programs to interact with hardware, and ensures smooth operation of the computer system. It comes in different types, such as monolithic, microkernel, or hybrid, and plays a critical role in tasks like process management, memory management, device drivers, system call handling, and security enforcement.

2) Rootfs

Rootfs, short for root file system, is the foundational directory hierarchy of a Linux file system. It contains essential files, directories, and virtual file systems that are necessary for the proper functioning of the operating system. These directories, such as /bin, /sbin, /lib, /etc, /proc, /sys, /dev, and others, hold critical files, libraries, configuration files, and device files that are required for various system operations.

On the other hand, the init process, also known as the first process or PID 1, is the initial user space process that is launched by the Linux kernel during system boot. It plays a vital role in system initialization, as it is responsible for starting other processes, setting up system services, and managing system resources. The init process is typically configured through an initialization system, such as SysV init, Upstart, or systemd, which determines the order and manner in which other processes are started and managed during system boot.

In summary, rootfs is the foundational directory hierarchy of a Linux file system, containing essential files and directories, and the init process is the first user space process that is started by the kernel during system boot, responsible for initializing the system and managing other processes.

 

로드할때 

gcc 에서 linking 까지 같이 해주지만, linking 과정을 분리해서 보면,,,,

ld 라이브러리가 프로그램을 실행하기 위해서 중요하고, 프로그램을 링킹할때 같이 작업해야 제대로 실행이 된다.

ld 라이브러리를 제대로 못하면 "no serach" "그런 파일이 없습니다"라는 에러가 뜬다.

어쨌든 중요한건 링킹을 할때 ld라이브러리 까지 포함을 해줘야한다. crt이랑 dynamic lic랑 ld할때 같이 해줘야한다.

 

개발할때 개발한 c파일만 새로 빌드하게끔하는게 incremental build 인데 그럼 개발자가 모든 파일들을 뭐 다 알아야하냐? 이런식으로 물어본다면, build system이 있기 때문에 다 몰라도 된다.

build system 의 종류는 makefile, cmake, ninja가 있다.

 

 

make file의 구성은 다음과 같다. target, dependency, command.

one line is one process in make file.

 

.PHONY는 file이 아니라고 명시해주는거

 

makefile 확장자룰이 있다.

.c.o = .c를 만나면 .o를 만들어!

 

chp3. roofts 빌드하기

*커널

윈도우에서 프로그램이 커널에 직접적으로 접근하지 않기 때문에 커널에 대해 몰라도 됨

리눅스에서 커널은 kernel/git/torvalds/linux.git 에 tovrvalds 아저씨가 다 만들고 아직도 이 아저씨가 다 하고있음

*부트로더 = grub(그럽) 

ROOTFS : 운영체제에서 필요한 아주 기본적임 프로그램

- Init 프로세스 : 리눅스 커널이 가장 처음 띄우는 프로세스, PID 1번

    1) systemd : 가장 유명한 Init

    2) system V : 일일히 다 읽었다 (옛날) -> 실행하지 않아도 되는데 실행시켜버림 혹은 엄청 다른거 초기화 되길 기다려야함

 

https://bit.ly/3D9XCOz

#코멘토 #코멘토실무PT #실무PT후기

.

 

 

'Linux' 카테고리의 다른 글

linux 4  (0) 2023.04.28
linux 3  (0) 2023.04.28
Linux2(Shell script)  (1) 2023.04.05
linux1  (0) 2023.04.05
리눅스  (0) 2023.02.28