Use MATH.H header file-L and-l parameters under Linux

Source: Internet
Author: User
Tags gtk

Encounter a problem is, C language use to sqrt, obviously already contains math.h file, but still hint undefined, so the Internet recruit answer:

Gcc-lm

The following transfers from http://bbs.csdn.net/topics/330105678

-l parameters and-L parameters

The-l parameter is used to specify the library to which the program is linked, and the-l parameter is followed by the library name, so what does the library name have to do with the real library file name? Take the math library, his library name is M, his library filename is libm.so, it is easy to see, the library file name of the head Lib and tail. So remove the library name.

Okay now we know how to get the name of the library, when we have to use a third-party to provide the library name libtest.so, then we just copy libtest.so to/usr/lib, compile with-ltest parameters, We will be able to use the Libtest.so library (of course, with the function in the Libtest.so library, we also need the header file with libtest.so)

/lib and/usr/lib and/usr/local/lib in the library directly with the-l parameter can be linked, but if the library file is not placed in these three directories, but in other directories, then we only use the-l parameter, the link will be error, the error message is probably: "/usr/bin /ld:cannot find-lxxx ", that is, the link program LD in those 3 directories can not find libxxx.so, then another parameter-L will come in handy, such as the common X11 library, it in the/usr/x11r6/lib directory, we compile with-l/ usr/x11r6/lib-lx11 parameter, the-l parameter follows the directory name where the library file resides. For example, we put libtest.so in the/AAA/BBB/CCC directory, the link parameter is-l/aaa/bbb/ccc-ltest

In addition, most libxxxx.so are just a link to RH9, for example, libm.so it links to/lib/libm.so.x,/lib/libm.so.6 and links to/lib/libm-2.3.2.so,

If there is no such link, or error, because LD will only find libxxxx.so, so if you want to use the XXXX library, and only libxxxx.so.x or libxxxx-x.x.x.so, do a link can be ln-s Libxxxx-x.x.x.so libxxxx.so

Manual to write the link parameter is always very troublesome, fortunately many library development packages provide a program to generate link parameters, the name is generally called xxxx-config, usually placed in the/usr/bin directory, such as

The gtk1.2 link parameter generator is gtk-config, Gtk-config--libs can get the following output "-l/usr/lib-l/usr/x11r6/lib-lgtk-lgdk-rdynamic

-lgmodule-lglib-ldl-lxi-lxext-lx11-lm ", This is the GTK link parameter required to compile a gtk1.2 program, xxx-config in addition to the--libs parameter is the--cflags used to generate the header file containing the directory, that is, the-i parameter, which we will talk about below. You can try the Gtk-config--libs--cflags and see the output.

Now the question is how to use these output results, the most stupid way is to copy and paste or copied, the clever way is to compile the command line to add the ' Xxxx-config--libs--cflags ', such as compiling a GTK program: GCC gtktest.c ' Gtk-config--libs--cflags ' It's almost done. Note ' is not a single quote, but the key to the left of the 1 key.

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.