A summary of the use of GCC in Linux

Source: Internet
Author: User

Common parameters for 1.GCC:

I (uppercase i) parameter: Add header file directory to index directory; for example:-i/usr/local/include/

L parameter: add the link library directory to the index directory; for example:-l/usr/local/lib/

L (lowercase L): indicates that the link library file was added at compile time; for example:-ltest (no spaces between parameters and content) represents a link library file with a connection (library named Test), and the library name and the actual file name are different. The library name is the name of the. A or. So after the file name is removed from the beginning, such as the library name of the link library with the file name libmy.a is my

Mechanism of 2.GCC:Link Order:GCC link order is from right to left to resolve the link parameters, that is, GCC if linked to multiple files, and then from right to left link, so the first connected file to write to the far right, that is, the end of the command.gcc link library directory:GCC takes/usr/lib and/usr/lib64 as the default Library index directory, which can be controlled----1. Edit the/etc/ld.so.conf file and add the path to the directory where the library file is located 2. Run Ldconfig, the command rebuilds/ Etc/ld.so.cache file 3. Static Library Issues:To generate a static library:A static library is just a collection of object objects, and using the AR command, you can package the. o file as a static library.
Assuming that GCC has generated a.o, B.O, C.O, use the following command to generate the LIBMYLIB.A
#ar RCS LIBMYLIB.A A.O b.o C.O
4. Dynamic Library Issues:To generate a dynamic library:The generation of dynamic libraries is generated directly by GCC.
Suppose A.C, B.C two files, can be generated by the following command libmylib.so
#gcc-shared-o libmylib.so A.C B.C
5.GCC Usage Issues:-static option When prompted cannot FIND-LC:Reason analysis: gcc compile time called Libc.so's link library file, if the-static parameter is added, then GCC is looking for libc.a file, if not in the directory, then cannot FIND-LC Normally, installing GCC with Yum does not install a static library, so there is this error. Solution: Install gcc-corresponding static libraries, namely: Yum install Glibc-static
error: Undefined reference to ' sqrt ' when using-LM, which is the math library, under the-static option:Cause analysis: Because there are-static in the compilation options,-LM refers to LIBM.A

This involves the implementation of the symbolic hole.
Sam guesses: Because the compiler reads the file from left to right, when read-LM, because it is static, but at this time do not know which symbol to draw out. So we have to continue to deal with it, and then find it in MAIN.O.
*und* 00000000 sqrt, that is, the symbol needs to be filled in, the following file is not the implementation of this symbol. So it's an error.

But if you remove-static. This symbol is still undefined in test compiled by the compiler. However, the symbols will be found at run time from libm.so.
Solution: Since the compiler will only look backwards for symbols, it's easy to put the-LM back on the line.
Gcc-o test-static TEST.O-LM
Compilation succeeded.

A summary of the use of GCC in Linux

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.