在jetson nano中编译旧版本的usb_cam
- 1. 问题背景
- 1.1 jetson nano的软件环境版本
- 2.问题现象
- 3.问题原因及解决办法
1. 问题背景
最近给jetson nano重装了系统之后,在编译今年人工智能ROS小车比赛用的工程包的时候,遇到一个问题迟迟没有解决,花费了一段时间才弄清楚原因和找到解决办法,现在记录如下
1.1 jetson nano的软件环境版本
这里可以直接使用jtop
进行查看,没有jtop的话,先安装一下,安装方法如下:
Jetson Nano上安装jtop的步骤如下:
- 安装pip3和依赖项:
打开终端,运行以下命令安装pip3(如果尚未安装):
sudo apt-get install python3-pip python3-dev -y
安装其他依赖项,这些对于jtop的运行是必要的:
sudo apt-get install git cmake
sudo apt-get install libhdf5-serial-dev hdf5-tools
sudo apt-get install libatlas-base-dev gfortran
- 安装jtop:
使用pip3安装jetson-stats,它包含了jtop:
sudo -H pip3 install jetson-stats
安装完成后,可能需要重启jetson_stats服务:
sudo systemctl restart jetson_stats.service
重启系统以确保所有更改都已生效(可选):
reboot
- 运行jtop:
在终端中输入以下命令来启动jtop:
sudo jtop
如果安装后遇到无法运行jtop,并且一直提示重启jtop服务的问题,可以尝试再次使用pip3重新安装jetson-stats。
另外,如果你需要配置CUDA的环境变量,可以运行以下命令来编辑
~/.bashrc
文件(假设你的CUDA已经正确安装):
- 查看cuda的bin目录下是否有nvcc:
ls /usr/local/cuda/bin
- 如果存在nvcc,编辑~/.bashrc文件:
sudo vim ~/.bashrc
在文件末尾添加以下两行(注意在vim中,按I进入插入模式,按Esc退出插入模式,然后按:进入命令模式,输入wq并按Enter保存并退出):
export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
保存并关闭文件后,运行以下命令使更改生效:
source ~/.bashrc
以上步骤应该能帮助你在Jetson Nano上成功安装并运行jtop。如果仍然遇到问题,请检查你的网络连接、软件源设置以及任何相关的错误消息,以便进一步诊断问题。
最终显示信息如下,注意OpenCV的版本,最好使用3.2.0
的,如果是4.1.1
的话,可能会出现冲突。
这里可以直接卸载掉,然后使用ros里面的opencv
其他安装opencv的方式可以参考我的另外一篇博文:
我和jetson-Nano的故事(10)——安装OpenCV3.2.0
2.问题现象
在使用catkin_make
编译usb_cam编译时,报错信息如下
[ 75%] Linking CXX executable /home/agilex/idrive_ws/devel/lib/usb_cam/usb_cam_node
/home/agilex/idrive_ws/devel/lib/libusb_cam.so: undefined reference to `av_packet_from_data'
/home/agilex/idrive_ws/devel/lib/libusb_cam.so: undefined reference to `sws_scale'
/home/agilex/idrive_ws/devel/lib/libusb_cam.so: undefined reference to `av_frame_alloc'
/home/agilex/idrive_ws/devel/lib/libusb_cam.so: undefined reference to `avcodec_close'
/home/agilex/idrive_ws/devel/lib/libusb_cam.so: undefined reference to `av_frame_get_buffer'
/home/agilex/idrive_ws/devel/lib/libusb_cam.so: undefined reference to `av_new_packet'
/home/agilex/idrive_ws/devel/lib/libusb_cam.so: undefined reference to `avcodec_alloc_context3'
/home/agilex/idrive_ws/devel/lib/libusb_cam.so: undefined reference to `av_log_set_level'
/home/agilex/idrive_ws/devel/lib/libusb_cam.so: undefined reference to `avcodec_free_context'
/home/agilex/idrive_ws/devel/lib/libusb_cam.so: undefined reference to `av_free'
/home/agilex/idrive_ws/devel/lib/libusb_cam.so: undefined reference to `avcodec_open2'
/home/agilex/idrive_ws/devel/lib/libusb_cam.so: undefined reference to `avcodec_send_packet'
/home/agilex/idrive_ws/devel/lib/libusb_cam.so: undefined reference to `sws_getContext'
/home/agilex/idrive_ws/devel/lib/libusb_cam.so: undefined reference to `av_image_get_buffer_size'
/home/agilex/idrive_ws/devel/lib/libusb_cam.so: undefined reference to `av_parser_close'
/home/agilex/idrive_ws/devel/lib/libusb_cam.so: undefined reference to `av_parser_init'
/home/agilex/idrive_ws/devel/lib/libusb_cam.so: undefined reference to `avcodec_register_all'
/home/agilex/idrive_ws/devel/lib/libusb_cam.so: undefined reference to `sws_freeContext'
/home/agilex/idrive_ws/devel/lib/libusb_cam.so: undefined reference to `avcodec_receive_frame'
/home/agilex/idrive_ws/devel/lib/libusb_cam.so: undefined reference to `av_image_copy_to_buffer'
/home/agilex/idrive_ws/devel/lib/libusb_cam.so: undefined reference to `avcodec_find_decoder'
3.问题原因及解决办法
出现这些链接错误是因为在编译过程中没有正确链接 FFmpeg
库。为了正确链接这些库,你需要确保 CMakeLists.txt
文件中包含了必要的库和头文件路径。
以下是详细步骤,确保 FFmpeg 库被正确找到并链接:
-
安装 FFmpeg 库:
首先,确保系统上安装了 FFmpeg 库。sudo apt-get update sudo apt-get install ffmpeg libavcodec-dev libavformat-dev libavutil-dev libswscale-dev
-
更新 CMakeLists.txt 文件:
编辑你的 ROS 包的CMakeLists.txt
文件,确保正确链接所有必需的 FFmpeg 库。cmake_minimum_required(VERSION 3.0.2) project(usb_cam)find_package(catkin REQUIRED COMPONENTSroscppstd_msgs# Add other required components here )find_package(PkgConfig REQUIRED) pkg_check_modules(AV REQUIRED libavcodec libavformat libavutil libswscale)catkin_package(CATKIN_DEPENDS roscpp std_msgsDEPENDS AV )include_directories(${catkin_INCLUDE_DIRS}${AV_INCLUDE_DIRS} )add_executable(${PROJECT_NAME}_node src/your_node.cpp)target_link_libraries(${PROJECT_NAME}_node${catkin_LIBRARIES}${AV_LIBRARIES} )
确保
pkg_check_modules
找到所有必要的 FFmpeg 库(libavcodec
,libavformat
,libavutil
,libswscale
)。 -
清理和重新编译:
在工作空间的根目录下,清理并重新编译:catkin_make clean catkin_make
-
完整的 CMakeLists.txt 示例:
这是一个完整的CMakeLists.txt
示例,它应该能够帮助你正确地配置项目:
cmake_minimum_required(VERSION 2.8.12)
project(usb_cam)## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS cv_bridge image_transport roscpp std_msgs std_srvs sensor_msgs camera_info_manager)# Find OpenCV
find_package(OpenCV REQUIRED)## pkg-config libraries
find_package(PkgConfig REQUIRED)
pkg_check_modules(avcodec libavcodec REQUIRED)
pkg_check_modules(avutil libavutil REQUIRED)
pkg_check_modules(swscale libswscale REQUIRED)
pkg_check_modules(AV REQUIRED libavcodec libavformat libavutil libswscale)###################################################
## Declare things to be passed to other projects ##
##################################################### LIBRARIES: libraries you create in this project that dependent projects also need
## CATKIN_DEPENDS: catkin_packages dependent projects also need
## DEPENDS: system dependencies of this project that dependent projects also need
catkin_package(
INCLUDE_DIRS include
LIBRARIES ${PROJECT_NAME}
DEPENDS AV
)###########
## Build ##
###########include_directories(include
${catkin_INCLUDE_DIRS}
${avcodec_INCLUDE_DIRS}
${swscale_INCLUDE_DIRS}
${avutil_INCLUDE_DIRS}
${OpenCV_INCLUDE_DIRS}
${AV_INCLUDE_DIRS}
)## Build the USB camera library
add_library(${PROJECT_NAME} src/usb_cam.cpp)
target_link_libraries(${PROJECT_NAME}
${avcodec_LINK_LIBRARIES}
${avutil_LINK_LIBRARIES}
${swscale_LINK_LIBRARIES}
${catkin_LIBRARIES}
${OpenCV_LIBS}
)## Declare a cpp executable
add_executable(${PROJECT_NAME}_node nodes/usb_cam_node.cpp)
target_link_libraries(${PROJECT_NAME}_node
${PROJECT_NAME}
${avcodec_LINK_LIBRARIES}
${avutil_LINK_LIBRARIES}
${swscale_LINK_LIBRARIES}
${catkin_LIBRARIES}
${OpenCV_LIBS}
${AV_LIBRARIES}
)#############
## Install ##
############### Mark executables and/or libraries for installation
install(TARGETS ${PROJECT_NAME}_node ${PROJECT_NAME}
RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
)## Copy launch files
install(DIRECTORY launch/
DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}/launch
FILES_MATCHING PATTERN "*.launch"
)install(DIRECTORY include/${PROJECT_NAME}/DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp"
)
-
确保头文件路径和库路径正确:
确保所有必要的头文件路径和库路径都在include_directories
和target_link_libraries
中正确配置。如果你的库安装在非标准路径中,可以通过手动设置路径来确保编译器能找到它们。 -
调试链接问题:
如果问题仍然存在,可以使用以下命令查看链接器的详细输出,以便进一步调试:VERBOSE=1 catkin_make
通过这些步骤,你应该能够解决链接错误并成功编译项目。