CMake交叉编译OpenCV

桌面环境:Windows Subsystem for Linux 4.4.0-17134-Microsoft

Cmake:Visual Studio Tools for Cmake 1.0

OpenCV版本:3.3.0

工具链版本:gcc6.4.0 g++6.4.0

 

示例代码:

#include 
#include 
#include 
#include "opencv2/opencv.hpp"
#include "opencv2/highgui.hpp"
using namespace cv;
using namespace std;

int main()
{
    string imgL_filename = "imL.bmp";
    Mat imgL = imread(imgL_filename);
    size_t cols = imgL.cols;
    size_t rows = imgL.rows;

    printf("rows = %zd, cols = %zd", rows, cols);
    fflush(stdout);

    return 0;
}

Cmake问题配置示例:

message("Process Project: ${CMAKE_CURRENT_SOURCE_DIR}")

file(GLOB_RECURSE SRC_LIST
    "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp"
)
ADD_EXECUTABLE(opencvTest ${SRC_LIST})

#指定工具链中OpenCV路径
SET(OpenCV_DIR ${SDK_DIR}aarch64-linux-gnu/sysroot/usr/share/OpenCV)
#查找OpenCV库文件
FIND_PACKAGE( OpenCV REQUIRED)
#输出找到的OpenCV库
message("OpenCV_LIBS ${OpenCV_LIBS}")
#连接OpenCV库
TARGET_LINK_LIBRARIES(opencvTest ${OpenCV_LIBS})

这里Cmake配置略去了工具链的配置,只列出了OpenCV相关的配置。

但编译的时候提示文件找不到。修改代码发现不使用OpenCV库就没问题。网上的方法一般是

-DENABLE_PRECOMPILED_HEADERS=OFF

但是这是针对编译OpenCV库的设置。我们现在是使用OpenCV库。根据cmake中message的输出,我们是找到了OpenCV的lib了。既然这样的话,我们就手动指定连接OpenCV库。修改Cmake配置如下:

message("Process Project: ${CMAKE_CURRENT_SOURCE_DIR}")

file(GLOB_RECURSE SRC_LIST
    "${CMAKE_CURRENT_SOURCE_DIR}/stereo_test.cpp"
)
ADD_EXECUTABLE(opencvTest ${SRC_LIST})

#指定库链接路径
LINK_DIRECTORIES(${SDK_DIR}aarch64-linux-gnu/sysroot/usr/lib)
#链接指定的库文件
TARGET_LINK_LIBRARIES(opencvTest libopencv_core.so libopencv_imgcodecs.so libopencv_highgui.so)

问题解决

你可能感兴趣的:(嵌入式)