Undefined reference to error Solution

Source: Internet
Author: User

 

Chen yunwen

 

During program compilation in Linux, "undefined reference error" is often reported,

 

Here, we will summarize some possible causes and solutions to our friends:

 

For undefined reference error, refer to the Linux gcc link rules first:

 

The search order is as follows:

 

-L the specified path, search from left to right

The path specified by the Environment Variable LIBRARY_PATH. Use ":" To split and search from left to right.

The path sequence specified by/etc/ld. so. conf

/Lib and/usr/lib (64-bit/lib64 and/usr/lib64)

Search sequence of dynamic library calls:

 

The path specified by the-rpath parameter of ld, which is written to the code

Path specified by the ld script

Path specified by LD_LIBRARY_PATH

Path specified by/etc/ld. so. conf

/Lib and/usr/lib (64-bit/lib64 and/usr/lib64)

In general, we use the-L method to specify the search path and LD_LIBRARY_PATH to specify the link path when calling the dynamic link library.

 

In addition, if the first one is found, the system will return the result, and the following will not be searched. for example,-L. /A-L. /B-lx has libx in. a B contains libx. a And libx. so, this will be used in. /A libx. a does not follow the dynamic library priority principle, because. /A is first found, and there is no dynamic inventory with the same name in

 

For dynamic link libraries, the actual symbols are located at runtime. in the compilation. in so, if the library required by the library is not associated with it, for example, libx. so you need to use uldict, but forget to compile libx. if-luldict is added to so, libx is compiled. so does not report an error, because libx. so is considered to be a library, which contains some symbols that do not know the specific implementation is legal, it can be specified at runtime or when another binary program is compiled.

 

If g ++-Lpath-lx is used for compilation, the linker will find that the required uldict symbol table cannot be found and an error is reported. However, if the program uses dlopen for loading, because the program runs at runtime, an error is reported during this process. another case is that an external interface has been declared and defined in the dynamic library, but the implementation is forgotten, and similar errors will occur at this time.

 

If such an error is reported at runtime, check whether the error is caused by the fact that some libraries are not linked or some interfaces are not implemented.

 

With the above foundation, it is not difficult to conclude that the cause of the undefined reference error may come from the following aspects:

 

1. The corresponding Library (. o/. a/. so) is not specified)

This error occurs when the entity defined in the library is used but the Library (-lXXX) is not specified or the library path (-LYYY) is not specified,

 

2. The Parameter order of the connected database is incorrect.

By default, the more basic the database is to be written after-l, whether it is static or dynamic

 

3 gcc/ld version mismatch

The compatibility problem of gcc/ld versions is caused by compatibility problems between gcc2 and gcc3 versions (in fact, gcc3.2 and 3.4 also have such problems to some extent) this error occurs when you use an earlier version of a machine on a later version. This problem is common in a 32-bit environment, in addition, the 64-bit database is accidentally used in the 32-bit environment or the 32-bit database is used in the 64-bit environment in turn.

 

4 C/C ++ dependencies and links

The use of gcc and g ++ compilation results must ensure that both sides of the extern "C" interface can be used, in our 64-bit environment, the gcc link g ++ Library also needs to be added with-lstdc ++. For details, see the previous section on mixed compilation.

 

5. errors during runtime

This problem is basically because the program uses the dlopen Method for loading. so,. so all the required databases are not linked. For details, refer to the preceding description of hybrid use of static and dynamic databases.

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.