C + + Learning-GCC compilation process View assembly code

Source: Internet
Author: User

      • Gcc
        • GCC parameters
        • Example

Gcc

GCC is very familiar, is a compiler, powerful, full name is: GUN gcc.

In peacetime everyone is simply called: GCC, Powerful, able to compile a lot of languages. Includes: c, C + +, Java, Ada, etc...

GCC parameters

GCC usually has a lot of parameters, in peacetime when I compile small programs, are directly gcc -o object source.c on it.

But one time I wanted to see the same thing. But the two codes that are written differently are different in the execution efficiency. So I checked the parameters of the assembly code in the GCC compilation process.

Parameters Description
-C Compile no links only, generate *.o files
-S Generating assembly code *.s files
-E Precompiled build *.i File
-G Debugging information is included in the executable program and can be debugged with GDB.
-O Export the output file to the specified file
-static Link Static link library
-library link Libraries named Library
Example

The use of the method is relatively simple:

    • Pre-compile first:
gcc -E hello.c -o hello.i

So we get a precompiled file.
There are some:

# 1 "Main.cpp"# 1 "<built-in>" 1# 1 "<built-in>" 3# 175 "<built-in>" 3# 1 "<command line>" 1# 1 "<built-in>" 2# 1 "Main.cpp" 2# 1 "/applications/xcode.app/contents/developer/toolchains/xcodedefault.xctoolchain/usr/bin/. /include/c++/v1/iostream " 1 3# Panax Notoginseng "/applications/xcode.app/contents/developer/toolchains/xcodedefault.xctoolchain/usr/bin/. /include/c++/v1/iostream " 3# 1 "/applications/xcode.app/contents/developer/toolchains/xcodedefault.xctoolchain/usr/bin/. /include/c++/v1/__config " 1 3#  - "/applications/xcode.app/contents/developer/toolchains/xcodedefault.xctoolchain/usr/bin/. /include/c++/v1/__config " 3# 226 "/applications/xcode.app/contents/developer/toolchains/xcodedefault.xctoolchain/usr/bin/. /include/c++/v1/__config " 3typedef __char16_t CHAR16_T;TYPEDEF __char32_t char32_t;# 349 "/applications/xcode.app/contents/developer/toolchains/xcodedefault.xctoolchain/usr/bin/. /include/c++/v1/__config " 3...

As such, it is to put some of the head files inside God horse.

    • You can then link:
gcc -S hello.i -o hello.s

Inside is the assembler code. As follows:

    . section__text,__text,regular,pure_instructions. section__text,__const. Align  4Lcpi0_0:    . Long   1127219200              # # 0x43300000    . Long   1160773632              # # 0x45300000    . Long   0                       # # 0x0    . Long   0                       # # 0x0lcpi0_1:    . Quad   4841369599423283200     # # Double 4.503600e+15    . Quad   4985484787499139072     # # Double 1.934281e+25    . section__text,__text,regular,pure_instructions. Globl_main. Align  4,0x90_main:                                  # # @main    . CFI_startproc# # Bb#0:Pushq%RBPLTMP2:    . CFI_def_cfa_offset -Ltmp3:    . CFI_offset%RBP,- -Movq%rsp,%RBP ...

Forgive me for the assembly compared to idiot, I can only read a little bit of assembly code, want to learn all dragged a long time TT

    • It is then compiled, and does not link to the birth .o file.
gcc -c hello.s -o hello.o

This time there is no way to open the view with a text editor. It is already a binary file. It's all open 0 1 , too.
I opened it with sublime Text 2 and it was a bunch of 16-binary code.

    • Then generate the executable file:
gcc hello.o -o hello

At this point, the link is successful and compiled into an executable file.

Just use ./hello it when you're done.

C + + Learning-GCC compilation process View assembly code

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.