我该如何解决这个“... .so: undefined reference to '...”

How do i solve this"... .so: undefined reference to '..."

对于与我类似的案例,我已经在互联网上浏览了几个小时,但我已经尝试了所有解决方案,但我的代码仍然无法正常工作。我正在使用一个名为“vdo_slam”的库,该库已构建并可在 /usr/local/include/vdo_slam 中找到。在我的项目 CMakeList.txt 中,我可以 find_package(vdo_slam REQUIRED) 没有错误。我遇到的唯一问题是在“catkin_build”的末尾,我得到了几个这样的“未定义的引用......”,如下所示。我已经尝试了下面列出的几种解决方案。所有这些未定义的引用都在“vdo_slam”包中声明和定义。感谢任何帮助。

我尝试过的已知解决方案:

  1. 我找了类和错误中提到的函数,看看是否有here中提到的纯虚拟析构函数,但它们都定义得很牢固。

  2. 我尝试使用“readelf”命令进行调试,如下所示,参考 this。但是我没有在错误中实际包含这些函数的“libvdo_slam.so”。我想当我 运行 这个“readelf”命令时我需要有“libvdo_slam.so”,对吗?我该如何添加?

来自“catkin_build”的错误消息:

/home/tranks/testing_ws/devel/lib/libmy_realtime_vdo_slam.so: undefined reference to `typeinfo for VDO_SLAM::Visualizer2D'
/home/tranks/testing_ws/devel/lib/libmy_realtime_vdo_slam.so: undefined reference to `vtable for VDO_SLAM::Visualizer2D'
/home/tranks/testing_ws/devel/lib/libmy_realtime_vdo_slam.so: undefined referealtime_vdo_slam/CMakeFiles/ros_vdoslam_node.dir/all' failed
make[3]: *** [my_realtime_vdo_slam/CMakeFiles/ros_vdoslam_node.dir/all] Error 2
Makefile:140: recipe for target 'all' failed
make: *** [all] Error 2
Invoking "make -j12 -l12" failede/tranks/testing_ws/devel/lib/libmy_realtime_vdo_slam.so: undefined reference to `VDO_SLAM::BoundingBox::BoundingBox(double, double, double, double)'
/home/tranks/testing_ws/devel/lib/libmy_realtime_vdo_slam.so: undefined reference to `VDO_SLAM::Scene::Scene()'
/home/tranks/testing_ws/devel/lib/libmy_realtime_vdo_slam.so: undefined reference to `VDO_SLAM::Visualizer2D::spinOnce(std::shared_ptr<VDO_SLAM::Scene>&)'
/home/tranks/testing_ws/devel/lib/libmy_realtime_vdo_slam.so: undefined reference to `VDO_SLAM::Scene::add_scene_object(std::shared_ptr<VDO_SLAM::SceneObject>&)'
/home/tranks/testing_ws/devel/lib/libmy_realtime_vdo_slam.so: undefined reference to `VDO_SLAM::Visualizer2D::Visualizer2D(std::shared_ptr<VDO_SLAM::VisualizerParams>&)'
collect2: error: ld returned 1 exit status
my_realtime_vdo_slam/CMakeFiles/ros_vdoslam_node.dir/build.make:226: recipe for target '/home/tranks/testing_ws/devel/lib/my_realtime_vdo_slam/ros_vdoslam_node' failed
make[3]: *** [/home/tranks/testing_ws/devel/lib/my_realtime_vdo_slam/ros_vdoslam_node] Error 1
CMakeFiles/Makefile2:6593: recipe for target 'my_realtime_vdo_slam/CMakeFiles/ros_vdoslam_node.dir/all' failed
make[3]: *** [my_realtime_vdo_slam/CMakeFiles/ros_vdoslam_node.dir/all] Error 2
Makefile:140: recipe for target 'all' failed
make: *** [all] Error 2
Invoking "make -j12 -l12" failed
$ readelf --dynamic /home/tranks/testing_ws/devel/lib/libmy_realtime_vdo_slam.so

    Dynamic section at offset 0x161608 contains 42 entries:
      Tag        Type                         Name/Value
     0x0000000000000001 (NEEDED)             Shared library: [libimage_transport.so]
     0x0000000000000001 (NEEDED)             Shared library: [libcv_bridge.so]
     0x0000000000000001 (NEEDED)             Shared library: [libopencv_core.so.3.2]
     0x0000000000000001 (NEEDED)             Shared library: [libtf2_ros.so]
     0x0000000000000001 (NEEDED)             Shared library: [libtf2.so]
     0x0000000000000001 (NEEDED)             Shared library: [libroscpp.so]
     0x0000000000000001 (NEEDED)             Shared library: [libroscpp_serialization.so]
     0x0000000000000001 (NEEDED)             Shared library: [librosconsole.so]
     0x0000000000000001 (NEEDED)             Shared library: [librostime.so]
     0x0000000000000001 (NEEDED)             Shared library: [libboost_system.so.1.65.1]
     0x0000000000000001 (NEEDED)             Shared library: [libpthread.so.0]
     0x0000000000000001 (NEEDED)             Shared library: [libopencv_highgui.so.3.4]
     0x0000000000000001 (NEEDED)             Shared library: [libstdc++.so.6]
     0x0000000000000001 (NEEDED)             Shared library: [libm.so.6]
     0x0000000000000001 (NEEDED)             Shared library: [libgcc_s.so.1]
     0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
     0x0000000000000001 (NEEDED)             Shared library: [ld-linux-x86-64.so.2]
     0x000000000000000e (SONAME)             Library soname: [libmy_realtime_vdo_slam.so]
     0x000000000000001d (RUNPATH)            Library runpath: [/home/tranks/testing_ws/devel/lib:/opt/ros/melodic/lib:/usr/local/lib]
     0x000000000000000c (INIT)               0xd0bd0
     0x000000000000000d (FINI)               0x129494
     0x0000000000000019 (INIT_ARRAY)         0x35f580
     0x000000000000001b (INIT_ARRAYSZ)       56 (bytes)
     0x000000000000001a (FINI_ARRAY)         0x35f5b8
     0x000000000000001c (FINI_ARRAYSZ)       8 (bytes)
     0x000000006ffffef5 (GNU_HASH)           0x1f0
     0x0000000000000005 (STRTAB)             0x2be68
     0x0000000000000006 (SYMTAB)             0xa7d8
     0x000000000000000a (STRSZ)              545696 (bytes)
     0x000000000000000b (SYMENT)             24 (bytes)
     0x0000000000000003 (PLTGOT)             0x362000
     0x0000000000000002 (PLTRELSZ)           90336 (bytes)
     0x0000000000000014 (PLTREL)             RELA
     0x0000000000000017 (JMPREL)             0xbaaf0
     0x0000000000000007 (RELA)               0xb4058
     0x0000000000000008 (RELASZ)             27288 (bytes)
     0x0000000000000009 (RELAENT)            24 (bytes)
     0x000000006ffffffe (VERNEED)            0xb3e98
     0x000000006fffffff (VERNEEDNUM)         6
     0x000000006ffffff0 (VERSYM)             0xb1208
     0x000000006ffffff9 (RELACOUNT)          9
     0x0000000000000000 (NULL)               0x0

这是我的 CMakeList.txt:

cmake_minimum_required(VERSION 3.10)
project(my_realtime_vdo_slam)

find_package(catkin REQUIRED COMPONENTS
  cv_bridge
  flow_net
  geometry_msgs
  image_transport
  mask_rcnn
  message_generation
  midas_ros
  mono_depth_2
  nav_msgs
  python_service_starter
  roscpp
  sensor_msgs
  std_msgs
  tf
  tf2
  tf2_geometry_msgs
  tf2_ros
  tf2_sensor_msgs
  vision_msgs
  visualization_msgs
  nodelet
  message_filters
)

find_package(OpenCV)
find_package(vdo_slam REQUIRED)

## Generate messages in the 'msg' folder
add_message_files(
   FILES
   VdoSlamScene.msg
   VdoSlamMap.msg
   VdoSceneObject.msg
   VdoInput.msg
)

generate_messages(
  DEPENDENCIES 
    actionlib_msgs 
    geometry_msgs 
    std_msgs 
    sensor_msgs 
    mask_rcnn 
    vision_msgs
)


set(PROJECT_INCLUDE_DIRS
  include
  ${catkin_INCLUDE_DIRS}
  ${OpenCV_INCLUDE_DIRS}
  ${vdo_slam_INCLUDE_DIRS}
  )

include_directories(
  ${PROJECT_INCLUDE_DIRS}
)  


catkin_package(
  INCLUDE_DIRS ${PROJECT_INCLUDE_DIRS}
  LIBRARIES ${PROJECT_NAME} 
  CATKIN_DEPENDS 
    roscpp
    image_transport 
    python_service_starter 
    std_msgs 
    tf 
    tf2 
    tf2_geometry_msgs 
    tf2_ros 
    tf2_sensor_msgs 
    nav_msgs 
    sensor_msgs 
    geometry_msgs 
    cv_bridge 
    flow_net 
    mask_rcnn 
    mono_depth_2 
    midas_ros 
    message_runtime 
    nodelet
    message_filters
  # DEPENDS system_lib
)

add_library(${PROJECT_NAME} SHARED 
  src/RosVdoSlam.cpp
  src/utils/RosUtils.cpp
  src/visualizer/RosVisualizer.cpp
  src/visualizer/RosAsyncManager.cpp
  src/tracking/HungarianSolver.cpp
  src/tracking/SemanticTracker.cpp
)
target_link_libraries(${PROJECT_NAME} PUBLIC
        ${catkin_LIBRARIES}
        ${OpenCV_LIBS}
        ${vdo_slam_LIBRARIES}
)
#if(catkin_EXPORTED_LIBRARIES)
#  add_dependencies(${PROJECT_NAME}
#         ${catkin_EXPORTED_LIBRARIES}
#         ${catkin_LIBRARIES}
#         ${OpenCV_LIBS}
 #     ${vdo_slam_LIB_DIRS})
# endif()

# target_include_directories(${PROJECT_NAME} SYSTEM PUBLIC
#     ${catkin_INCLUDE_DIRS}
#     ${OpenCV_INCLUDE_DIRS}
#     ${vdo_slam_INCLUDE_DIRS}
#     $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
#     $<INSTALL_INTERFACE:include>
# )


add_executable(ros_vdoslam_node 
  src/ros_vdoslam_node.cpp
)

target_link_libraries(ros_vdoslam_node 
  ${catkin_LIBRARIES}
  ${OpenCV_LIBS}
  ${vdo_slam_LIB_DIRS}
  ${PROJECT_NAME}
  )

if(catkin_EXPORTED_LIBRARIES)
  add_dependencies(ros_vdoslam_node
          ${catkin_EXPORTED_LIBRARIES}
          ${catkin_LIBRARIES}
          ${OpenCV_LIBS}
      ${vdo_slam_LIBRARIES})
endif()

尤里卡! 我找到了一个解决方案,尽管它不是一个理想的解决方案。如问题中所述,我注意到 libvdo_slam.so 在 运行 $ readelf <on_my_library.so> 时未被列为共享库。所以我在 CMakeList.txt 中手动添加了 -lvdo_slam 所以它看起来像:

target_link_libraries(ros_vdoslam_node  
  ${catkin_LIBRARIES}  
  ${OpenCV_LIBS}    
  ${PROJECT_NAME}  
  -lvdo_slam  
  )  `

此外,由于“vdo_slam”也使用了修改后的“g2o”库,因此必须执行相同的步骤在 VDO_SLAM 库的 CMakeList.txt 中完成(在类似的地方添加 -lvdo_slam_g2o)。

虽然现在构建成功了,但我还是想不通为什么会这样。根据我的理解,-lvdo_slam${vdo_slam_LIBS}是一样的,后者应该在使用find_package(vdo_slam REQUIRED)

之后才有效