4 parsing undefined symbols (top)

Source: Internet
Author: User

1. The two-point search principle is useful for finding syntax errors in unknown locations.

2. Library, library missing issues
Missing Library common reminders: undefined reference to "function"
The library is divided into two types: static library, dynamic library
When compiling calls to the function code in the static library, those functions become part of the final executable file;
When it is a dynamic library, these functions are actually attached to the transfer code until it is actually executed.

On UNIX systems, the static library file name is appended with a suffix. A, i.e. archive; The dynamic library suffix is generally. So, which is shared object; In addition, the library name typically starts with Lib.
$ ar rc file.c-l88-l path//create static Library

$ gcc-fpic-c B.C//Generate target file B.O
$ gcc-shared-o lib88 B.O//Generate a dynamic library from the target file B.O compilation

1 linux ar command
2 gcc compiler parameter-fpic some problems
-L, which instructs GCC to tell LD to look up (and default directory) in a directory other than the current directory when looking for a function

The LDD command allows you to check which libraries are needed by the program and, if so, where the operating system can find them.

First the program looks in the default path, no lib88.so is found, the latter is not in the default path;
The way to solve this problem is to add path/pathname to the search path;

# setenv Ld_library_path=${ld_library_path}:/pathname
# Export Ld_library_path

use of libraries in open source software
When installing open source software, there is often a problem that the source code configuration file cannot find some libraries, and attempts to resolve the problem through ld_library_path may fail, the root of which is that the configuration file calls the program named Pkgconfig. This program receives information about the library from some of the original data files. The filename suffix is. PC, and the prefix is the name of the library. For example, LIBGCJ.PC contains the location of the library file libgcj.so*.
Pkgconfig search. The default directory for PC files depends on the location of the pkgconfig itself. For example, if the program location is/usr/lib, search for/usr/lib. If the desired library is/lib/local/lib, searching in this directory alone is not enough. To solve this problem, set the environment variable pkg_config_path.

3. Links
A command like GCC is actually not a simple compile command, it manages the stages of program construction, including preprocessing (-E), compilation (-s), assembly (-C), and linking (LD), which are often referred to as compiler drivers.
GCC Common commands
There are two steps to compiling and linking, and in a large project, it is a good idea to build executables in a modular manner. If this is not the case, modifying a source file will require all source code to be recompiled, while using a modular method you can recompile only those parts that are affected by code modifications.
(1)Resolving undefined symbols
   * Missing Connection parameters
For example, in the connection process, missing the connection parameter containing the function fun definition fun.o, and appear undefined reference to "fun";
*search for missing symbols
Find & grep can be used to locate files in Unix systems, such as:
  
The utility NM provides a list of symbols that parse whether the symbols are defined in the object, typically used in conjunction with grep, and can be placed at the beginning of the file by the-O option;
  
  
Where the second column of T: the symbol of the function is contained in the text portion of the object's file;
U: denotes the symbol to be parsed;
  
It is also possible to show this function in the math library by using the man, the man sqrtf, to add the link parameter-l**m** at compile time, so that its name in the library is lib**m**.so, minus the prefix, just the middle name.mSame.
  
   Link Order Issues
If the object file is directly used as the linker's parameter, all its symbols will be linked to the final executable file, resulting in a larger target file. To avoid this problem, you can use a library or an archive file instead of an object file. The linker picks up the object files that contain the currently undefined symbols from the library, and most of the linker handles the objects and libraries provided to it only once, by clicking on the algorithm:
(1) First load the object file, the object file contains the initialization code, the initialization code also calls the C program's main function, that is,The connector starts at least with the undefined symbol main, the compiler driver automatically supplies the object file that contains the initialization routines to the connector.
(2) The connector then continues to follow the specified object files and libraries in the order of the parameters. Connectors from each Librarypick up an object file that contains at least one unresolved symbol。 All symbols are picked out, regardless of whether the symbol marked as undefined in the connector is parsed. New undefined symbols may be generated when a new object file is loaded into the connector.
(3) If all symbols are resolved at the end of the link line, the link process succeeds.

$ ar-r-O LIBFACTORIAL.A factorial.o

$ gcc-o calc_factorial LIBFACTORIAL.A MAIN.O
Undefined reference to ' factorial '
Explanation: When the linker arrives at the LIBFACTORIAL.A library, only the undefined symbol in its list is main; In LIBFACTORIAL.A, no defined file is provided for the main symbol, and the symbol defined in the library is not selected because there is no unresolved symbol for it in the linker, and finally, the connector loads the object file MAIN.O, where the main symbol is defined, but main () The routine also calls factorial (), so it creates a new unresolved entry in the undefined list of symbols, the connector reaches the end of the parameter list, and there are unresolved items, so the program terminates.

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.