Compiled by GCC

Source: Internet
Author: User
1. Analyze common helloworld programs
First, write a simple helloworld program as follows:
/* Hello. C */
Int main (INT argc, char * argv [])
{
Return 0
}
Compile the program:
Gcc-O hello. c
Equivalent compilation method:
Gcc-C hello. c
Gcc-O hello. My-nostartfiles hello. O/usr/lib/crt1.o/usr/lib/crti. O/usr/lib/crtn. o
In redflag workstation 5.0, the GCC compiler 3.4.3 is compiled with 3589 bytes in size, and the diff command is used to compare the files with the same size. This proves that GCC first compiles hello. c into Hello. O when compiling and linking the hello. c file, and then connects it with the crt1.o, crti. O, and crtn. ochains.

If you want to write a program without the main function, you need to implement crt1.o by yourself. The program entry is the symbol "_ start. The Code is as follows:
/* Nomain. C */
Void _ start (void)
{
_ Exit (0 );
}
Gcc-O nomain-nostartfiles hello. c

2. Compile and use the library
In addition to directly compiling code, the function library is also used. Databases can be static or dynamic databases. The static library is a file ending with. A, for example, libxxx. A. The dynamic library is a file ending with. So, for example, libxxx. So. After xxx or after. So and. A will follow the version number, for example: libc-2.3.4.so, or libuuid. so.1.2.
The code for adding a function library is as follows:
/* Test. H */
Int test1 (void );

/* Test. C */
# Include <stdio. h>
# Include "libtest. H"

Int test1 (void)
{
Printf ("test1 function is called./N ");
Return 0;
}
Use the following command to generate libtest. A when compiling a static Library:
Gcc-C test. c
Ar-r libtest. A test. o
Use the following command to generate libtest. So
Gcc-C-FPIC test. c
Gcc-shared test. O-o libtest. So
Or
Gcc-FPIC-shered test. C-o libtest. So
Use the following code to call the Library.
/* Calllib. C */
# Include "test. H"

Int main (INT argc, char * argv [])
{
Test1 ();
}
Compile as follows:
Gcc-O calllib. Static calllib. c libtest.
Or
Gcc-O calllib. Dynamic calllib. c libtest. So
Or
Gcc-O calllib. C-ltest
The premise is that libtest. A or libtest. So is in the current compiled directory.
The library that calllib. Static depends on is only libc and LD-Linux, but calllib. Dynamic has an additional libtest. So.

3. library files and header files
In the program, use # include <stdio. h> similar header file stdio. h In the header file path of the compiler, # include "ABC. stdio. the H file should be in the current directory. You can specify the directory of the header file by specifying the parameter-I <path> for the compiler. You can use # include <> to reference the file. For example, GCC-I./include hello. C will search for the header file from the include directory in the current directory.
Similarly, the library function called in the program also needs to specify the path and library during compilation. Use the-L <path> parameter to specify the directory of the library file and the-L <File> parameter to specify the included library file. For example, to use the libxxx. So library, the parameter is-LXXX.
Generally, after a library is compiled, it contains the library file and header file. If you want to use this library, you can specify the library file directory and the header file directory with the-I and-l parameters respectively, or copy them to the include and Lib directories of the compiler.

4. Reduce program volume
The Code is as follows:
/* Nomain. C */
Void _ start (void)
{
ASM ("movl, eax/N"
"Movl {FCKeditor}, EBX/N"
"Int {FCKeditor} X80"
);
}
Use the following method to minimize the volume of executable files (except for manual methods ).
Gcc-O nomain-S-O3 nomain. c
Objcopy-R. comment-R. Data
The size of the executable file is 352 bytes.

GCC can compile executable bodies in two formats: A. Out format and ELF format. The. O target file and. A static library file

Appendix.1 Common commands
LDD: displays executable files or library files dependent on.
Objdump: displays the internal information of the elf executable file.
-H: Display
-T: displays the symbol information.
-T: displays dynamic symbols. (For example, reference the function name in the Dynamic Link Library)
-R: displays the relocation entry information.
-R: displays the dynamic relocation entry information. (For example, the variable or function address in the Dynamic Link Library)
-S: displays the content of all sections.
-S: disassembly code segment.
Objcopy: Copy ELF File Content
-R: deletes a section.
-J: only copy the specified section.

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.