The link sequence of the gcc Library

Source: Internet
Author: User

The link sequence of the gcc Library
Preface recently, a program developed on MIPS needs to use floating point operations. Write the bootloader and main function, and call the log floating point operation in the main function, including the math library. Then write the makefile and ld scripts. The gcc parameters use: CFLAGS =-c-march = 3081-msoft-float-fno-inline $(ENDIAN)-G0 ld parameters: LDFLAGS =-march = 3081-msoft-float-nostartfiles-lgcc-lm-lc-Wl,-Map, rlx_test.map database link sequence This article focuses on the gcc database link sequence. At the beginning, in the Link parameter section, my order was arranged as follows:-lc-lgcc-lm. The compile is normal, but when the ld encounters a problem, it always reports that the log function cannot find the errno variable. After careful analysis, I found that my program itself is faulty. The calculation expression can be summarized as: log (). If a is 0, the log function reports an error and assigns the error code to the global variable _ errno. The _ errno variable is declared in the errno. h header file, but defined in a file in libc. The gcc library Link order, the official explanation: https://gcc.gnu.org/onlinedocs/gcc-5.2.0/gcc/Link-Options.html#Link-Options gcc-l interpretation:-l library Search the library named library when linking. (The second alter-native with the library as a separate argument is only for POSIX compliance and is not recommended .) it makes a difference where in the command you write this option; the linker searches and processes libraries and Object files in the order they are specified. thus, foo. o-lz bar. o searches library z after file foo. o but before bar. o. if bar. o refers to functions in z, those functions may not be loaded. in other words, if the order of your library links is: foo. o-lz bar. o. Then, the gcc linker first searches for the database foo, then the z library, and then the bar library. This poses a problem. If the library bar calls the functions in library z, but the linker searches for library z first, no library bar calls library z at this time, so we won't pick out the function bodies in library z for link. Instead, we only pick out the function bodies called by the foo library in library z. Return to our previous description. Because the global variable _ errno is defined in library libc, the log function in library libm accesses the global variable _ errno. This requires that libm be in front of libc when linking. Therefore, we should arrange the link sequence of the database in this way:-lm-lgcc-lc. The more people call the underlying database, the more they put it behind; the more you call other people's upper-layer databases, the more you put them in front. Library Description: here we will talk about what libm, libgcc, and libc are used for: libm, a library of mathematical operation functions, which contains various basic mathematical function implementations, such as sin, cos, square root, and log. Libgcc Library: the library that needs to be called to compile code with gcc. It contains some basic functions. For more information, see https://gcc.gnu.org/onlinedocs/gccint/libgcc.html. For example, some integer/floating point operations, exception handling, and some miscellaneous functions. Libc library is a standard library of c, which contains basic input and output functions, such as memcpy and string copy functions. From the usage of these libraries, we know why we need to call them in the order of-lm-lgcc-lc. Of course, when compiling the kernel and some libraries, we may see nostdlib. This option means that the standard library libc and libgcc are not called, instead, we need to implement these basic library functions by calling kernel functions. Description of some compilation and link parameters-march = 3081 indicates that the cpu architecture is MIPS 3081. -Msoft-float is applicable to CPUs without FPU units. It is required to compile floating point computing code, also called soft floating point. -Nostartfiles: you do not need to use the default bootload code. Instead, you need to write the boot code by yourself.

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.