嵌入式开发板 aarch64 opencv库 交叉编译

在x86的ubuntu系统上为嵌入式开发板交叉编译opencv库
源码目录:/home/xxx/opencv-3.4.6

  • 操作步骤一

创建文件,写入交叉编译工具链信息
/home/xxx/opencv-3.4.6/arm_toochain.cmake

set(CMAKE_SYSTEM_NAME linux)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(tools /home/xxxxx/Documents/Disk2/Program/aarch64-xxxxx-linux/install/aarch64-xxxxx-linux)
set(CMAKE_C_COMPILER ${tools}/bin/aarch64-xxxxx-linux-gcc)
set(CMAKE_CXX_COMPILER ${tools}/bin/aarch64-xxxxx-linux-g++)
set(CMAKE_FIND_ROOT_PATH /home/xxxxx/Documents/Disk2/Program/aarch64-xxxxx-linux/install/aarch64-xxxxx-linux/lib)
  • 操作步骤二

cmake命令

cd /home/xxx/opencv-3.4.6/
mkdir build
mkdir build_install
cd build
cmake \
-DCMAKE_TOOLCHAIN_FILE=../arm_linux_setup.cmake \
-DCMAKE_INSTALL_PREFIX=/home/xxx/opencv-3.4.6/build_install \
-DBUILD_ZLIB=ON \
-DBUILD_opencv_python2=OFF \
-DBUILD_opencv_python3=OFF ..
  • 操作步骤三
make -j4
make install

出现错误解决办法
错误1

[ 45%] Linking CXX executable ../../bin/opencv_version
/home/liujt/Documents/Disk2/Program/aarch64-xxxxx-linux/install/aarch64-xxxxx-linux/host_bin/../lib/gcc/aarch64-linux-gnu/7.3.0/../../../../aarch64-linux-gnu/bin/ld: ../../lib/libopencv_core.so: undefined reference to `pthread_mutexattr_destroy'
/home/liujt/Documents/Disk2/Program/aarch64-xxxxx-linux/install/aarch64-xxxxx-linux/host_bin/../lib/gcc/aarch64-linux-gnu/7.3.0/../../../../aarch64-linux-gnu/bin/ld: ../../lib/libopencv_core.so: undefined reference to `pthread_key_create'
/home/liujt/Documents/Disk2/Program/aarch64-xxxxx-linux/install/aarch64-xxxxx-linux/host_bin/../lib/gcc/aarch64-linux-gnu/7.3.0/../../../../aarch64-linux-gnu/bin/ld: ../../lib/libopencv_core.so: undefined reference to `dladdr'
/home/liujt/Documents/Disk2/Program/aarch64-xxxxx-linux/install/aarch64-xxxxx-linux/host_bin/../lib/gcc/aarch64-linux-gnu/7.3.0/../../../../aarch64-linux-gnu/bin/ld: ../../lib/libopencv_core.so: undefined reference to `pthread_getspecific'
/home/liujt/Documents/Disk2/Program/aarch64-xxxxx-linux/install/aarch64-xxxxx-linux/host_bin/../lib/gcc/aarch64-linux-gnu/7.3.0/../../../../aarch64-linux-gnu/bin/ld: ../../lib/libopencv_core.so: undefined reference to `dlopen'
/home/liujt/Documents/Disk2/Program/aarch64-xxxxx-linux/install/aarch64-xxxxx-linux/host_bin/../lib/gcc/aarch64-linux-gnu/7.3.0/../../../../aarch64-linux-gnu/bin/ld: ../../lib/libopencv_core.so: undefined reference to `pthread_mutex_trylock'
/home/liujt/Documents/Disk2/Program/aarch64-xxxxx-linux/install/aarch64-xxxxx-linux/host_bin/../lib/gcc/aarch64-linux-gnu/7.3.0/../../../../aarch64-linux-gnu/bin/ld: ../../lib/libopencv_core.so: undefined reference to `pthread_key_delete'
/home/liujt/Documents/Disk2/Program/aarch64-xxxxx-linux/install/aarch64-xxxxx-linux/host_bin/../lib/gcc/aarch64-linux-gnu/7.3.0/../../../../aarch64-linux-gnu/bin/ld: ../../lib/libopencv_core.so: undefined reference to `dlclose'
/home/liujt/Documents/Disk2/Program/aarch64-xxxxx-linux/install/aarch64-xxxxx-linux/host_bin/../lib/gcc/aarch64-linux-gnu/7.3.0/../../../../aarch64-linux-gnu/bin/ld: ../../lib/libopencv_core.so: undefined reference to `dlsym'
/home/liujt/Documents/Disk2/Program/aarch64-xxxxx-linux/install/aarch64-xxxxx-linux/host_bin/../lib/gcc/aarch64-linux-gnu/7.3.0/../../../../aarch64-linux-gnu/bin/ld: ../../lib/libopencv_core.so: undefined reference to `pthread_mutexattr_settype'
/home/liujt/Documents/Disk2/Program/aarch64-xxxxx-linux/install/aarch64-xxxxx-linux/host_bin/../lib/gcc/aarch64-linux-gnu/7.3.0/../../../../aarch64-linux-gnu/bin/ld: ../../lib/libopencv_core.so: undefined reference to `pthread_setspecific'
/home/liujt/Documents/Disk2/Program/aarch64-xxxxx-linux/install/aarch64-xxxxx-linux/host_bin/../lib/gcc/aarch64-linux-gnu/7.3.0/../../../../aarch64-linux-gnu/bin/ld: ../../lib/libopencv_core.so: undefined reference to `pthread_mutexattr_init'
collect2: error: ld returned 1 exit status
apps/version/CMakeFiles/opencv_version.dir/build.make:95: recipe for target 'bin/opencv_version' failed
make[2]: *** [bin/opencv_version] Error 1
CMakeFiles/Makefile2:4699: recipe for target 'apps/version/CMakeFiles/opencv_version.dir/all' failed
make[1]: *** [apps/version/CMakeFiles/opencv_version.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....

解决办法

在/home/xxx/opencv-3.4.6/CMakeLists.txt 文件中加入flags信息
include(cmake/OpenCVMinDepVersions.cmake)

SET(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-pthread -ldl")  # 添加入这一行
SET(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} "-pthread -ldl")  # 添加入这一行

if(CMAKE_GENERATOR MATCHES Xcode AND XCODE_VERSION VERSION_GREATER 4.3)

错误2

[ 55%] Building CXX object modules/video/CMakeFiles/opencv_video.dir/src/tvl1flow.cpp.obj
/home/xxxxx/opencv-3.4.6/modules/imgcodecs/src/grfmt_jpeg.cpp: In member function 'virtual bool cv::JpegDecoder::readHeader()':
/home/xxxxx/opencv-3.4.6/modules/imgcodecs/src/grfmt_jpeg.cpp:224:58: error: invalid conversion from 'uchar* {aka unsigned char*}' to 'const JOCTET* {aka const char*}' [-fpermissive]
             state->source.pub.next_input_byte = m_buf.ptr();
                                                 ~~~~~~~~~^~
/home/xxxxx/opencv-3.4.6/modules/imgcodecs/src/grfmt_jpeg.cpp: In member function 'virtual bool cv::JpegDecoder::readData(cv::Mat&)':
/home/xxxxx/opencv-3.4.6/modules/imgcodecs/src/grfmt_jpeg.cpp:456:56: error: invalid conversion from 'JSAMPROW {aka char*}' to 'const uchar* {aka const unsigned char*}' [-fpermissive]
                         icvCvt_RGB2BGR_8u_C3R( buffer[0], 0, data, 0, cvSize(m_width,1) );
                                                ~~~~~~~~^
In file included from /home/xxxxx/opencv-3.4.6/modules/imgcodecs/src/grfmt_base.hpp:46:0,
                 from /home/xxxxx/opencv-3.4.6/modules/imgcodecs/src/grfmt_jpeg.hpp:46,
                 from /home/xxxxx/opencv-3.4.6/modules/imgcodecs/src/grfmt_jpeg.cpp:43:
/home/xxxxx/opencv-3.4.6/modules/imgcodecs/src/utils.hpp:93:6: note:   initializing argument 1 of 'void cv::icvCvt_BGR2RGB_8u_C3R(const uchar*, int, uchar*, int, cv::Size)'
 void icvCvt_BGR2RGB_8u_C3R( const uchar* bgr, int bgr_step,
      ^~~~~~~~~~~~~~~~~~~~~
/home/xxxxx/opencv-3.4.6/modules/imgcodecs/src/grfmt_jpeg.cpp:458:59: error: invalid conversion from 'JSAMPROW {aka char*}' to 'const uchar* {aka const unsigned char*}' [-fpermissive]
                         icvCvt_CMYK2BGR_8u_C4C3R( buffer[0], 0, data, 0, cvSize(m_width,1) );
                                                   ~~~~~~~~^
In file included from /home/xxxxx/opencv-3.4.6/modules/imgcodecs/src/grfmt_base.hpp:46:0,
                 from /home/xxxxx/opencv-3.4.6/modules/imgcodecs/src/grfmt_jpeg.hpp:46,
                 from /home/xxxxx/opencv-3.4.6/modules/imgcodecs/src/grfmt_jpeg.cpp:43:
/home/xxxxx/opencv-3.4.6/modules/imgcodecs/src/utils.hpp:116:6: note:   initializing argument 1 of 'void cv::icvCvt_CMYK2BGR_8u_C4C3R(const uchar*, int, uchar*, int, cv::Size)'
 void icvCvt_CMYK2BGR_8u_C4C3R( const uchar* cmyk, int cmyk_step,
      ^~~~~~~~~~~~~~~~~~~~~~~~
/home/xxxxx/opencv-3.4.6/modules/imgcodecs/src/grfmt_jpeg.cpp:465:60: error: invalid conversion from 'JSAMPROW {aka char*}' to 'const uchar* {aka const unsigned char*}' [-fpermissive]
                         icvCvt_CMYK2Gray_8u_C4C1R( buffer[0], 0, data, 0, cvSize(m_width,1) );
                                                    ~~~~~~~~^
In file included from /home/xxxxx/opencv-3.4.6/modules/imgcodecs/src/grfmt_base.hpp:46:0,
                 from /home/xxxxx/opencv-3.4.6/modules/imgcodecs/src/grfmt_jpeg.hpp:46,
                 from /home/xxxxx/opencv-3.4.6/modules/imgcodecs/src/grfmt_jpeg.cpp:43:
/home/xxxxx/opencv-3.4.6/modules/imgcodecs/src/utils.hpp:118:6: note:   initializing argument 1 of 'void cv::icvCvt_CMYK2Gray_8u_C4C1R(const uchar*, int, uchar*, int, cv::Size)'
 void icvCvt_CMYK2Gray_8u_C4C1R( const uchar* ycck, int ycck_step,
      ^~~~~~~~~~~~~~~~~~~~~~~~~
/home/xxxxx/opencv-3.4.6/modules/imgcodecs/src/grfmt_jpeg.cpp: In function 'boolean cv::empty_output_buffer(j_compress_ptr)':
/home/xxxxx/opencv-3.4.6/modules/imgcodecs/src/grfmt_jpeg.cpp:512:34: error: invalid conversion from '__gnu_cxx::__alloc_traits >::value_type* {aka unsigned char*}' to 'JOCTET* {aka char*}' [-fpermissive]
     dest->pub.next_output_byte = &(*dest->buf)[0];
/home/xxxxx/opencv-3.4.6/modules/imgcodecs/src/grfmt_jpeg.cpp: In member function 'virtual bool cv::JpegEncoder::write(const cv::Mat&, const std::vector&)':
/home/xxxxx/opencv-3.4.6/modules/imgcodecs/src/grfmt_jpeg.cpp:584:37: error: invalid conversion from '__gnu_cxx::__alloc_traits >::value_type* {aka unsigned char*}' to 'JOCTET* {aka char*}' [-fpermissive]
         dest.pub.next_output_byte = &out_buf[0];
/home/xxxxx/opencv-3.4.6/modules/imgcodecs/src/grfmt_jpeg.cpp:701:43: error: invalid conversion from 'uchar** {aka unsigned char**}' to 'JSAMPARRAY {aka char**}' [-fpermissive]
             jpeg_write_scanlines( &cinfo, &ptr, 1 );
                                           ^~~~
In file included from /home/xxxxx/opencv-3.4.6/modules/imgcodecs/src/grfmt_jpeg.cpp:75:0:
/home/xxxxx/opencv-3.4.6/3rdparty/libjpeg-turbo/src/jpeglib.h:953:20: note:   initializing argument 2 of 'JDIMENSION jpeg_write_scanlines(j_compress_ptr, JSAMPARRAY, JDIMENSION)'
 EXTERN(JDIMENSION) jpeg_write_scanlines(j_compress_ptr cinfo,
                    ^~~~~~~~~~~~~~~~~~~~
modules/imgcodecs/CMakeFiles/opencv_imgcodecs.dir/build.make:254: recipe for target 'modules/imgcodecs/CMakeFiles/opencv_imgcodecs.dir/src/grfmt_jpeg.cpp.obj' failed
make[2]: *** [modules/imgcodecs/CMakeFiles/opencv_imgcodecs.dir/src/grfmt_jpeg.cpp.obj] Error 1
CMakeFiles/Makefile2:2810: recipe for target 'modules/imgcodecs/CMakeFiles/opencv_imgcodecs.dir/all' failed
make[1]: *** [modules/imgcodecs/CMakeFiles/opencv_imgcodecs.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....

解决办法

在/home/xxx/opencv-3.4.6/CMakeLists.txt 文件中增加flags信息
include(cmake/OpenCVMinDepVersions.cmake)

SET(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-pthread -ldl -fpermissive")  # 这一行添加-fpermissive
SET(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} "-pthread -ldl") 

if(CMAKE_GENERATOR MATCHES Xcode AND XCODE_VERSION VERSION_GREATER 4.3)

你可能感兴趣的:(学习,c++,linux,arm)