Tutorial: I wrote about the linux compilation process myself (I can't proceed, but I can give some advice on the red part)

Source: Internet
Author: User
For more information, see linux general technology-Linux programming and kernel. Compile and install programs from source code.
A: How to get the source code
What is source code? The XXX. c file that you edited in c language.
The general format of the open source software downloaded from the network is .tar.gz.
Example
So how can we find the source code from such an archive file?
Gzip-d rarlinux-3.6.0.tar.gz-d option indicates Decompression
We can see that rarlinux-3.6.0.tar.gzhas become rarlinux-3.6.0.tar.
Tar-xf rarlinux-3.6.0.tar-xf indicates releasing all files from the archive
Of course, the source code of C is not extracted from this example. I have not found a suitable example.

B: How make works
On the basis of the preceding steps, we enter the rarfile and run
Make> info.txt
Let's see what our make has done?
Cat info.txt
Mkdir-p/usr/local/bin-p indicates the upper-level directory to be created. If the directory exists, no error processing is performed.
Mkdir-p/usr/local/lib
Cp rar unrar/usr/local/bin
Cp rarfiles. lst/etc
Cp default. sfx/usr/local/lib
Let's take a look at what is in makfile in the rar folder?
Cat Makefile
PREFIX =/usr/local this is the shell variable. You can use echo $ PREFIX to check it.

Install:
Mkdir-p $ (PREFIX)/bin
Mkdir-p $ (PREFIX)/lib
Cp rar unrar $ (PREFIX)/bin
Cp rarfiles. lst/etc
Cp default. sfx $ (PREFIX)/lib
Yes, because make completes the content in the configuration file according to certain rules. The default name of this configuration file is makefile, which can also be changed. I don't need to write this configuration file, but I always need to understand it.
Measure the test taker's knowledge about the basic parameters of the make command.
-C dir: the path where make works. The default path is the path of course.
-F filename: the specified file is used as the configuration file. The default value is makefile.
-D: print the debug information.
In this example, we can know how make works, which is useful for running programs from the source code.

C: introduce all the tools that can be used for installation.
Gcc-this is the Compiler
Make-contains the make command that generates binary files from makefiles. Of course, there are other functions.
Glibc-important shared libraries, c libraries, and basic math libraries. There is no way to run the system without this connection.
Glibc-devel-contains the standard header files required to create executable files.
Binutils-contains the tools required for compiling programs, mainly Assembler and link programs.
Kernel-source-contains the kernel source code
Libc-contains libc5, and the above mentioned glibc is linc6
.
D: About the software package.
The software packages that you can find in linux have different formats, which is annoying, but they let you know in what environments these open-source projects are developed and compiled, and what they support.
Collect
Filename-4.2.3.i386.rpm, which means you can install with rpm, our favorite method.
Filename-4.2.3.tar.gz, which means to use gz compression, archive with tar, so what, that does not know
Filename-4.2.3.src.tar.gz, which indicates compression with gz, archive with tar, the content is source code
Filename-4.2.3.bin.SPARC.tar.gz, which is a binary code that indicates compression with gz and archive with tar that can run on the iSCSI workstation.
Filename-4.2.3.bin.ELF.static.tar.gz: a binary file consisting of executable FLF files with static connections, compressed with gz and archived with tar.
4.2.3 indicates version 4th, 2nd patches, and 3rd modifications.
Let's talk about it comprehensively.
Rpm. This suffix indicates that the file is a binary file used by fedora. This does not mean that the file is in binary format, but that it can be used by the fedora software manager. You can use the archive file manager to open the file.
Tar. This file is archived using tar and opened using the tar command.
Gz. and z. It is compressed by gzip.
Tgz. This extension can be easily combined with the above
Bz2. compressed with bzip2, you can use the bip2 command to open
Taz. And tz. This indicates that tar is used for compression, and the tar command is also used to open
Lsm. This is usually the text about the archive content, which can be downloaded together with the software package.
Deb. This is the same as rpm but used for Debian
If you are not sure about the format, you can use the file command to determine the format.
Now you can extract the source file from any soft package. We need to compile it.

E: Now let's take a look at the C compilation process ..
Pre-compile, compile and generate an assembly, compile and generate the target file, and generate executable files in the target file connection library. Everyone knows this process, but what exactly?
Complain: too many compilers are in place in one step. In fact, this is not a good thing for learning. At least I think it is troublesome to make simple things during the learning process, simplify troublesome things during work.
The example here is from Lorne
Pre-compiled gcc-E
Compile the gcc-S package with the gcc rpm package.
Compile
Binutils rpm package needs to be installed to connect to ld
1. Pre-compile
Compile c source code game. c
# Include

Int main ()
{
Printf ("Hello World! \ N ");
} The legendary helloworld
Gcc-E-o pregame game. c
This is a pregame file.
See what format he is.
File pregame
Pregame: ascii c program text
That is to say, the pre-compiled file is still the c source code. So what have the pre-editing translated?
Let's take a look at some things.
Cat pregame
You will find that pregame and game are similar. The difference is that pregame does not have # include The format is similar.
This is the role of pre-compilation. He adds the header file contained in game. c to the main function.
Gcc-o game. c generate an executable game program
Gcc-o pregame. c generates the executable program pregame
Note: the steps of as and ld are ignored.
Pregame uses. c as the file name.
It is found that the running results of game and pregame are the same, which also demonstrates the role of pre-compilation.
Summarize the functions of pre-Compilation: (these are summarized by lorne and are irrelevant to me)

1. Copy the "include" file to the source file to be compiled.
2. Replace "define" text with actual values.
3. Replace the macro at the place where the macro is called.


2. compile.
This process is used to generate an assembly language. This is a step in the compilation process. It is a step. Comrades ~~~~~~~~~~~~~~!!
Gcc-S-o aspregame pregame. c
Will claim an aspregame file to see what it is?
File aspregame
Aspregame: ASCII javaser program text
Hey, it's the assembler text.
If you are interested, take a look.
Cat aspregame
. File "pregame. c"
. Section. rodata
. LC0:
. String "Hello World! "
. Text
. Globl main
. Type main, @ function
Main:
Leal 4 (% esp), % ecx
Andl $-16, % esp
Pushl-4 (% ecx)
Pushl % ebp
Movl % esp, % ebp
Pushl % ecx
Subl $4, % esp
Movl $. LC0, (% esp)
Call puts
Addl $4, % esp
Popl % ecx
Popl % ebp
Leal-4 (% ecx), % esp
Ret
. Size main,.-main
. Ident "GCC: (GNU) 4.1.0 20060304 (Red Hat 4.1.0-3 )"
. Section. note. GNU-stack, "", @ progbits
The legendary assembly.
It is best to use. asm as the file name.
3. Generate the target file.
We can use gcc to complete this step, but we still use as. We always feel that one step is not very suitable for learning.
As-o ldaspregame aspregame
Will generate the ldaspregame File
Let's see what he is?
File ldaspregame
Ldaspregame: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped
The ELF format file is floating, which means that the address of this program can be modified to adapt to the initial address. In fact, it is an unconnected target file.
Note: We can convert any. c file into an assembly through gcc-S and convert it into a target file.
Use. o as the file name
4. connect to an executable file
Now let's generate an executable file.
Here we need to explain what is a dynamic link library, which is equivalent to a dll file in winxxx. Here it is a so file, and the ELF format supports dynamic link libraries.
Gcc-o exldaspregame ldaspregame
Okay. Claimed the executable file ldaspregame.
Any. c file can generate the target file, which needs to be connected for execution.
Here I have a question, which I hope experts can answer:
Originally, I wanted to use ld to connect files. ld ldaspregame
But the error message is:
Ld: warning: cannot find entry symbol _ start; defaulting to 08048094
Undefined reference to 'puts'
The database file to be connected is not set.
Ld/bin/libc. so.6 ldaspregame
Still Error
Error message:
Ld: warning: cannot find entry symbol _ start; defaulting to 08048094
What exactly is entry symbol_start? How does gcc complete this step?

[ This post was last edited by karon_fedora at, September 4 ,.]
Related Article

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.