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.