C++ file compilation does not report an error. Run the program to report an error: find, locate and solve the problem of undefined symbol

An error is reported during program execution, and the error content is as follows

XXXX:symbol lookup error:/home/....../libpdfium.so:undefined symbol:CRYPT_MD5Generate

Error analysis:

        This problem indicates that the symbol is not defined, and it is directly located in the third-party dynamic library libpdfium.so linked to the product, so I started from libpdfium.so.

Because of the source code of this third-party library, it is possible to find errors.

Error positioning:

        But this symbol undefined error is a headache, because in my original idea, shouldn't the symbol bit definition be reported directly during compilation? So to confirm, I recompile the third-party source code and compile No error was reported, a new so was generated, replaced and re-run, the result is still an undefined package symbol error. Searching on the Internet, I know that when the link is wrong, it will also cause the undefined error of the symbol in the later period (see the end for the reference content). In this block, you can use the ldd -r command to check whether the generated so contains undefined symbols.

        Looking at these undefined symbols, there are too many missing, and it shouldn't be. In my case, several static library files will be linked when generating libpdfium.so dynamic library. According to the information found on the Internet, confirm whether the linking static library sequence is correct. Directly search for the error string "CRYPT_MD5Generate" in the third-party source code, and found that there are two cpp files, one is declared and defined, and the other is called.

        And in this block, you can see that fpdf_parse_encrypt depends on the fx_crypt file below. Looking at the static library, fpdf_parse_encrypt is compiled into fpdfapi.a, and fx_crypt is compiled into the pdrm.a static library, so fpdfapi.a should depend on pdrm .a static library. Check the link sequence in the Makefile again and find that the sequence is reversed! ! ! The culprit has been found! It is the wrong sequence of links in the Makefile that leads to undefined symbols in the final so!

 

Here are several useful commands and reference materials that I used in the search process.

After compiling and generating the dynamic link library, when calling:

 
  1. # lichunhong @ lichunhong-ThinkPad-T470p in ~/Documents/src/effective_robotics_programming_with_ros-master/catkin_ws on git:lichunhong/dev x [18:54:05] C:127

  2. $ rosrun path_plan PathPlanSimulation

  3. /home/lichunhong/Documents/src/effective_robotics_programming_with_ros-master/catkin_ws/devel/lib/path_plan/PathPlanSimulation:

  4. symbol lookup error: /home/lichunhong/Documents/src/effective_robotics_programming_with_ros-master/catkin_ws/src/pathPlan/lib/libpathplan.so:

  5. undefined symbol: _ZN12ninebot_algo10AprAlgoLog9instance_E

即 symbol lookup error: libpathplan.so: undefined symbol: _ZN12ninebot_algo10AprAlgoLog9instance_E

When this kind of problem occurs, it is often a problem when linking. The following is a three-step solution

(1) Use the file command to view the architecture of the so library to see if it is consistent with the platform

As you can see, the current so library architecture is x86-64, which can be used under the GNU/Linux platform. Consistent platform and architecture

 
  1. # lichunhong @ lichunhong-ThinkPad-T470p in ~/Documents/src/motion_planner/bin on git:dev x [18:47:54]

  2. $ file libpathplan.so

  3. libpathplan.so: ELF 64-bit LSB shared object, x86-64, version 1 (GNU/Linux), dynamically linked,

  4. BuildID[sha1]=32ae641e73c547376df20ca94746fbf5507de415, not stripped

Next, you need to locate the specific information of undefined symbol

(2)  View the link status and error information of the so library through the ldd -r  xxx.so command

ldd command, you can check which libraries the corresponding executable file or library file depends on, but the executable file or library file requirements are the same as the compiler type of the operating system, that is, the computer is an X86 GCC compiler, then the ARM cannot be viewed through the ldd command The executable file or library file compiled by the cross compiler.

If you want to view the dependencies of the ARM cross-compiled executable programs and library files on Linux hosts such as Ubuntu, you can use the following command:
readelf -d xxx.so | grep NEEDED
 

 
  1. # lichunhong @ lichunhong-ThinkPad-T470p in ~/Documents/src/effective_robotics_programming_with_ros-master/catkin_ws/src/pathPlan/lib on git:lichunhong/dev x [18:57:19]

  2. $ ldd -r libpathplan.so

  3. linux-vdso.so.1 => (0x00007ffec1bd8000)

  4. libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f186cc0a000)

  5. libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f186c901000)

  6. libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f186c6eb000)

  7. libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f186c321000)

  8. /lib64/ld-linux-x86-64.so.2 (0x00007f186d27a000)

  9. undefined symbol: pthread_create (./libpathplan.so)

  10. undefined symbol: _ZN12ninebot_algo10AprAlgoLog9instance_E (./libpathplan.so)

  11. undefined symbol: _ZN2cv3maxERKNS_3MatES2_ (./libpathplan.so)

  12. undefined symbol: _ZN12ninebot_algo10AprAlgoLog8WriteLogE10LEVEL_TYPEPKcS3_z (./libpathplan.so)

  13. undefined symbol: _ZN2cv6dilateERKNS_11_InputArrayERKNS_12_OutputArrayES2_NS_6Point_IiEEiiRKNS_7Scalar_IdEE (./libpathplan.so)

  14. undefined symbol: _ZN2cvgtERKNS_3MatEd (./libpathplan.so)

  15. undefined symbol: _ZN2cv8fastFreeEPv (./libpathplan.so)

  16. undefined symbol: _ZN2cv3Mat5setToERKNS_11_InputArrayES3_ (./libpathplan.so)

  17. undefined symbol: _ZN12ninebot_algo10AprAlgoLog9instance_E (./libpathplan.so)

You can see that there are many undefined symbols, including the mentioned _ZN12ninebot_algo10AprAlgoLog9instance_E error

(3) Use c++filt symbol to locate the error in that C++ file

From the undefined symbol above, through c++filt <symbol>, you can locate most of the opencv problems

 
  1. # lichunhong @ lichunhong-ThinkPad-T470p in ~/Documents/src/effective_robotics_programming_with_ros-master/catkin_ws/src/pathPlan/lib on git:lichunhong/dev x [19:04:26] C:1

  2. $ c++filt _ZN2cv7waitKeyEi

  3. cv::waitKey(int)

  4.  
  5. # lichunhong @ lichunhong-ThinkPad-T470p in ~/Documents/src/effective_robotics_programming_with_ros-master/catkin_ws/src/pathPlan/lib on git:lichunhong/dev x [19:04:31]

  6. $ c++filt _ZN2cv3maxERKNS_3MatES2_

  7. cv::max(cv::Mat const&, cv::Mat const&)

 

Guess you like

Origin blog.csdn.net/weixin_42096901/article/details/108818459