如何正确链接ROS Indigo下的opencv3(使用CMake)?
我无法链接自己与ros分开安装的opencv3。 我得到这个错误
../devel/lib/libirTest.so: undefined reference to `cv::ORB::create(int, float, int, int, int, int, int, int, int)'
../devel/lib/libirTest.so: undefined reference to `cv::calcOpticalFlowPyrLK(cv::_InputArray const&, cv::_InputArray const&, cv::_InputArray const&, cv::_InputOutputArray const&, cv::_OutputArray const&, cv::_OutputArray const&, cv::Size_<int>, int, cv::TermCriteria, int, double)'
但是,能够成功编译外部ROS; 也就是使用没有ROS的类似软件包....下面是在ROS下使用的CMake文件(这不起作用)
cmake_minimum_required(VERSION 2.8.3)
project(my_test_pkg)
SET(SRC ${CMAKE_CURRENT_SOURCE_DIR}/src)
find_package(catkin REQUIRED COMPONENTS
cv_bridge
image_transport
roscpp
rospy
std_msgs
message_generation
genmsg
)
find_package(nodelet REQUIRED)
################################################
## Declare ROS messages, services and actions ##
################################################
add_message_files(
FILES
velocity_trsl.msg
)
generate_messages(
DEPENDENCIES
std_msgs
)
catkin_package(
CATKIN_DEPENDS message_generation cv_bridge image_transport roscpp rospy std_msgs
)
###########
## Build ##
###########
include_directories(
${catkin_INCLUDE_DIRS}
)
#***********************
#****** GSL
#***********************
find_package( PkgConfig REQUIRED)
pkg_check_modules( gsl REQUIRED gsl )
#***********************
#****** Boost
#***********************
SET (BOOST_DIR "/home/polar/soft/lib/boost/boost_1_61_0")
FIND_PACKAGE(Boost 1.61.0 REQUIRED thread)
if (NOT Boost_FOUND)
message(FATAL_ERROR " Fatal error: Boost (version >= 1.55) required.")
else()
message(STATUS "Setting up BOOST")
message(STATUS " Includes - ${Boost_INCLUDE_DIRS}")
message(STATUS " Library - ${Boost_LIBRARY_DIRS}")
include_directories(${Boost_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS})
endif (NOT Boost_FOUND)
#***********************
#****** OpenCV
#***********************
find_package( OpenCV 3.1.0 REQUIRED )
find_package( OpenCV 3.1.0 REQUIRED PATHS /home/polar/soft/lib/opencv/opencv-3.1.0/cmake)
if (NOT OpenCV_FOUND)
message(FATAL_ERROR "*****!!!!! opencv NOT found.")
endif (NOT OpenCV_FOUND)
#****************************************
include_directories(${SRC}/calculus)
SET(MY_LIB
irTest
)
# ALL EXEC FILES...
SET(EXE_TESTER
tester1
)
add_executable(tester1 ${CMAKE_CURRENT_SOURCE_DIR}/exe-main-files-stdl/testdir/tester1.cpp )
SET(FILES_2_RUN
${EXE_TESTER}
)
#--------------------------
foreach(file2link ${FILES_2_RUN})
target_link_libraries(${file2link}
${catkin_LIBRARIES}
${Boost_LIBRARIES}
${gsl_LIBRARIES}
${OpenCV_LIBRARIES}
${OpenCV_LIBS}
${MY_LIB}
)
add_dependencies(${file2link} my_test_pkg_generate_messages_cpp)# newlly added for messages!!
endforeach(file2link)
add_subdirectory(src)
它应该是我使用的opencv版本和ROS使用的(v2.4)之间的冲突吗? 也许cv_bridge或image_transport正在创建这个问题......(???)我甚至删除了build /和devel /并试图再次编译,但总是出现同样的问题.....我从来没有花费这么长时间,而不是这个问题...
我能够在ROS靛蓝下将ROS项目与opencv 3.1联系起来。 请注意,opencv 2.4仍然是靛蓝下的默认版本,所以这变得棘手,因此YMMV。
首先,你需要安装ros-indigo-opencv3
软件包。 通过手动指定路径,源代码路径可能会有效,但我没有对其进行测试。
在您的CMakeLists.txt中,确保您包含${OpenCV_INCLUDE_DIRS}
并链接到${OpenCV_LIBRARIES}
:
find_package(OpenCV 3 REQUIRED)
include_directories(
${catkin_INCLUDE_DIRS}
${OpenCV_INCLUDE_DIRS}
)
add_library(your_node src/your_node.cpp)
target_link_libraries(your_node ${catkin_LIBRARIES} ${OpenCV_LIBRARIES} )
最后,因为ros opencv绑定仅为opencv 2.4编译,所以在编译之前需要将vision_opencv存储库(https://github.com/ros-perception/vision_opencv)克隆到工作区中。 这将确保绑定与opencv 3的链接。
我正在使用Opencv2.4.8
和opencv3.1
与ros indigo
。 该opencv3.1
从下家目录-源安装/home/xxx/opencv3_install
。
好处是我使用opencv2.4.8
旧版包不需要更改任何代码,因为默认的opencv版本是2.4.8
。
当opencv3.1
需要,在CMAKE_PREFIX_PATH
应设置为找到的lib。
set(CMAKE_PREFIX_PATH "/home/xxx/opencv3_install")
find_package(OpenCV 3.1 REQUIRED)
set(OpenCV_INCLUDE_DIRS "/home/xxx/opencv3_install/include")
此外, cv_bridge
需要在安装后重新编译opencv3.1
。
上一篇: How to correctly link opencv3 under ROS Indigo (using CMake)?