背景
最近在搞iOS App实现车道识别,一直在iPhone真机上跑,调参数很不方便,效率低下。还是把macOS环境搞好,方便调参数,本文记录搭建macOS上openCV的C++开发环境。
步骤
CMake工具安装
使用brew安装CMake,brew安装就不啰嗦了,macOS基础技能。
$ brew install cmake
源代码编译安装OpenCV
1、clone源码
$ mkdir opencv
$ cd opencv
$ git clone https://github.com/opencv/opencv.git
$ cd opencv
$ git checkout tags/4.5.5
$ cd ..
$ git clone https://github.com/opencv/opencv_contrib.git
$ cd opencv_contrib
$ git checkout tags/4.5.5
$ cd ..
2、编译安装
准备编译
mkdir install build_opencv
$ cd build_opencv
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=../install \
-D INSTALL_C_EXAMPLES=ON \
-D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules \
-D BUILD_EXAMPLES=ON ../opencv
编译、安装
$ export CPUS=$(sysctl -n hw.physicalcpu)
$ make -j $CPUS
$ make install
3、验证安装
$ cd ..
$ mkdir simple_demo && cd simple_demo
$ touch main.cpp
$ touch CMakeLists.txt
main.cpp内容如下:
// main.cpp
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
using std::cout;
using std::endl;
int main(int argc, char** argv) {
if (argc != 2) {
cout << "Expecting a image file to be passed to program" << endl;
return -1;
}
cv::Mat img = cv::imread(argv[1]);
if (img.empty()) {
cout << "Not a valid image file" << endl;
return -1;
}
cv::namedWindow("Simple Demo", cv::WINDOW_AUTOSIZE);
cv::imshow("Simple Demo", img);
cv::waitKey(0);
cv::destroyAllWindows();
return 0;
}
注意下面的OPENCV_DIR
变量需要设置为自己的安装路径
# CMakeLists.txt
# Older versions of CMake are likely to work just fine but, since
# I don't know where to cut off I just use the version I'm using
cmake_minimum_required(VERSION "3.17")
# name of this example project
project(simple-demo)
# set OpenCV_DIR variable equal to the path to the cmake
# files within the previously installed opencv program
set(OpenCV_DIR /Users/ganquan/Development/opencv/install/lib/cmake/opencv4)
# Tell compiler to use C++ 14 features which is needed because
# Clang version is often behind in the XCode installation
set(CMAKE_CXX_STANDARD 14)
# configure the necessary common CMake environment variables
# needed to include and link the OpenCV program into this
# demo project, namely OpenCV_INCLUDE_DIRS and OpenCV_LIBS
find_package( OpenCV REQUIRED )
# tell the build to include the headers from OpenCV
include_directories( ${OpenCV_INCLUDE_DIRS} )
# specify the executable target to be built
add_executable(simple-demo main.cpp)
# tell it to link the executable target against OpenCV
target_link_libraries(simple-demo ${OpenCV_LIBS} )
编译:
$ mkdir build && cd build
$ cmake ..
$ make
验证运行:
$ ./simple-demo /path/to/test.jpg
如果一切正常的话,会有一个窗口,显示test.jpg。
VSCode安装配置
- 删除build目录中全部内容,后面会用vscode中的CMake插件来生成
$ rm -irf *
- 安装VSCode、安装C/C++ IntelliSense插件、安装CMakeTools插件
-
用vscode打开simple_demo目录,这时会让设置编译器,选择Clang
- simple_demo目录下创建vscode的配置
mkdir .vscode
,然后创建settings.json
文件配置CMake作为工程配置工具
// settings.json
{
"C_Cpp.default.configurationProvider": "vector-of-bool.cmake-tools"
}
- 一切正常的话,现在就可以编译了。编译后,依旧可以通过命令验证程序运行
$ ./simple-demo /path/to/test.jpg
- 配置调试工具
在.vscode
目录下,创建launch.json
文件,配置如下,注意"type"
的值。
要注意,如果你使用的mac是X86_64的芯片,那么"type"
配置cppdbg
就可以了。
而我使用的是M1芯片的mac,由于cppdbg
不能调试,所以需要配置为lldb
。
原因是,cppdbg
这个微软出的调试工具,还在不支持的Apple Silicon ARM64的调试。lldb支持,所以换用lldb即可。相关信息可以看这个issue
// launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) Debugger",
"type": "lldb",
"request": "launch",
"program": "build/simple-demo",
"args": ["/path/to/test.jpg"],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "lldb"
}
]
}
现在就可以愉快的使用开发调试了。
参考
[1] 安装配置opencv
[2]cppdbg调试出错
[3]cppdbg尚不支持Apple Silicon ARM64调试