GCC series-initial understanding of LD

Source: Internet
Author: User

UseGccCompileCodeWe all know that there will be a compilation link process, andLDIt is the connector used for linking.

1 Source File hello. c
# Include & lt; stdio. H & gt;

Int main (INT argc, char * argv [])
{
Printf (& quot; Hello \ n & quot ;);
Return 0;
}

2 GCC Compilation
GCC hello. C-O hello
Then run./Hello
Output result hello

3 GCC compilation,LDConnection
Gcc-C hello. C-O hello. o
LD-O hello. o
The generated Hello file cannot be executed.

4. Solution
Since the hello generated in step 2 can be executed, let's take a look at what GCC has done <br/>
Modify as follows <br/>
GCC hello. C-O hello-V
At this time, the GCC operation steps will be printed and you will understand it.

5. Conclusion
Lflags + = -- Eh-frame-HDR-M elf_i386 -- hash-style = GNU-dynamic-linker/lib/ld-linux.so.2
Lflags + =/usr/lib/GCC/i386-redhat-linux/4.1.1 /.. /.. /.. /crt1.o/usr/lib/GCC/i386-redhat-linux/4.1.1 /.. /.. /.. /crti. o/usr/lib/GCC/i386-redhat-linux/4.1.1/crtbegin. O
Lflags + = -- no-as-needed/usr/lib/GCC/i386-redhat-linux/4.1.1/crtend. o/usr/lib/GCC/i386-redhat-linux/4.1.1 /.. /.. /.. /crtn. O
Gcc-C hello. C-O hello. o
LD $ (lflags)-O hello. o
Succeeded and running properly.

For a long time, I did not notice that a LD is hidden in the GCC compilation process.

How does LD work?

First, a connection script is required:

Enter LD-verb in the Linux Command Line to view the default connection script.

If you write the connection script, you only need to add it to clflag.
-Wl,-t my. LDS

You can use your own connection scriptProgram.

MoreArticleSee shanzhizi.

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.