背景
最近在研究图像识别相关代码——OpenCV,为了便于调试,就要在Mac上搭建一个调试c++程序的调试环境。我这跑通了,分享给大家。
环境
Mac OS 10.14.5
xcode-select v2354
Visual Studio code 1.35.1
OpenCV 3.4.5
Clang 1001.0.46.4
CMake 3.14.5
步骤说明
编译环境准备
安装OpenCV
安装VScode 插件
VScode 项目配置
OpenCV HelloWorld
环境准备
Mac 自带有C/C++环境,就不再赘述。
Xcode Command Line Tools是必须的,可通过一下命令行触发安装程序,或者下载安装。
命令安装 xcode-select
sudo xcode-select --install
下载 xcode-select 安装
选择下载文件注意这里提示的版本号!
命令行安装速度很慢,且失败率很高,建议通过下载dmg文件安装
CMake 准备
编译OpenCV时要用到cmake。同样可以通过homebrew或者下载安装。
下载方式安装的CMake,cmake可执行程序在/Applications/CMake.app/Contents/bin/cmake.
如果你用的homebrew方式安装opencv那么CMake就不是必须的.
pkg-config 安装
编译引用有opencv库的c/c++代码需要附带很多参数来告知include path,libs等,用pkg-config就能精简编译参数。
brew install pkg-config
安装OpenCV
有两种方式安装。
brew install
## 一个命令安装好opencv3
brew install opencv3
安装好之后最好配置上这几个环境变量。(一般命令行会提示你)
export PATH="/usr/local/opt/opencv@3/bin:$PATH"
export LDFLAGS="-L/usr/local/opt/opencv@3/lib"
export CPPFLAGS="-I/usr/local/opt/opencv@3/include"
export PKG_CONFIG_PATH="/usr/local/opt/opencv@3/lib/pkgconfig"
用homebrew安装很方便,就是安装时间很长(我用了一个大白天)。额外还会安装python-opencv。
编译安装
点击下载源代码。这里我选择的是Sources 3.4.5
解压后,进入目录
cd
mkdir release
cd release
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local
make
sudo make install
这里创建的release目录会放cmake命令产生的成果。CMAKE_INSTALL_PREFIX配置的目录会存make命令产生的成果。
到这里如果都没报错就算安装成功了。
一般都不会出问题,在虚拟环境不一定能成功。
写个C++ Hello World
先写一个只引用了标准库的C++代码试试看。
/// ./main.cpp
#include
#include
int main(int argc, const char * argv[]) {
std::cout << "Hello, World!\n";
return 0;
}
结果如下。
没问题就继续尝试在代码引入OpenCV库。
pkg-config 环境配置
安装好后,配置一个环境变量就行了。
PKG_CONFIG_PATH="~/opencv解压目录/release/unix-install"
配置好之后在执行命令:
pkg-config opencv --libs --cflags opencv
显示内容大致如下,这块的内容就是opencv.pc里面的配置。
该环境变量就是为了让pkg-config能够找到opencv.pc这个文件。
写个OpenCV Demo
好,重点来了,大部分人会卡在这里
写个OpenCV HellowWorld来试试,打开一个空的VScode项目,创建
文件test.cpp
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include
#include
#include
using namespace std;
using namespace cv;
Mat src, dst, src_gray;
char* window_name = "Demo";
/**
* @主函数
*/
int main( int argc, char** argv )
{
/// 读取一副图片,不改变图片本身的颜色类型(该读取方式为DOS运行模式)
src = imread(argv[1], 1 );
/// 将图片转换成灰度图片
cvtColor(src, src_gray, CV_RGB2GRAY);
namedWindow(window_name, CV_WINDOW_AUTOSIZE);
imshow(window_name, src_gray);
while(true) {
int c;
c = waitKey(20);
if (27 == (char) c) {
break;
}
}
}
进入到命令行环境
## 编译测试程序
g++ `pkg-config opencv --libs --cflags opencv` ./test.cpp -o ./test.o
## 运行测试程序
./test.o ~/Downloads/opencv-logo.png
pkg-config 这条命令是为编译opencv测试代码引入类库。
能看到如下运行结果,恭喜你成功了。
可参考该链接最后: 编译自测程序
实际调试代码不停编译运行会敲很多命令,我们尝试把这些工作交给VScode。先为VScode安装一些插件
C/C++
C++ Intellisense
C++ Clang Command Adapter
在根目录下创建一个.vocode文件夹,再创建三个文件。
.vscode/launch.json
.vscode/tasks.json
.vscode/c_cpp_properties.json
文件.vscode/tasks.json
{
"tasks": [
{
"type": "shell",
"label": "g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.o",
"`pkg-config",
"--libs",
"--cflags",
"opencv`"
],
"options": {
"cwd": "/usr/bin"
},
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": []
}
],
"version": "2.0.0"
}
文件.vscode/launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++ build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.o",
"args": [
"~/Downloads/opencv-logo.png"
],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [
{"name": "PKG_CONFIG_PATH", "value": "opencv解压目录/release/unix-install"}, // 这是opencv解压码后创建的release目录下的unix-install, 要保证该目录下下有opencv.pc文件
{"name": "DYLD_LIBRARY_PATH", "value": "/usr/local/opencv/build/lib"} // 这个是你在编译时,opencv make时`CMAKE_INSTALL_PREFIX`指定的目录
// {"DYLD_LIBRARY_PATH": "/usr/local/opencv/build/lib"} // 这样的配置是错误的!
],
"externalConsole": true, // 这个开关打开表示,新开一个命令行窗口并运行程序。
"MIMode": "lldb",
"preLaunchTask": "g++ build active file" // 这一行的内容要和上一个文件`.vscode/tasks.json`的`tasks`->`label`字段的内容要一模一样!
}
]
}
文件.vscode/c_cpp_properties.json
{
"configurations": [
{
"name": "Mac",
"includePath": [
"/usr/local/include",
"/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include",
"/usr/local/opencv/include", // 这个是你在编译时,opencv make时`CMAKE_INSTALL_PREFIX`指定的目录
"${workspaceFolder}"
],
"browse": {
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": "",
"path": [
"/usr/local/include",
"/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include",
"/usr/local/opencv/include", // 这个是你在编译时,opencv make时`CMAKE_INSTALL_PREFIX`指定的目录
"${workspaceFolder}"
]
},
"intelliSenseMode": "clang-x64",
"macFrameworkPath": [
"/System/Library/Frameworks",
"/Library/Frameworks"
],
"compilerPath": "/usr/bin/g++",
"cStandard": "c11",
"cppStandard": "c++17"
}
],
"version": 4
}
点击VScode菜单栏Debug > Start Debugging运行结果如下:
为了便于调试,我把launch.json的externalConsole设置为true,这样会在vscode之外新开一个终端运行程序,可能会弹框要授权,同意即可。
常见问题
pkg-config 配置不正确导致的报错
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found
test.cpp:1:10: fatal error: 'opencv2/imgproc/imgproc.hpp' file not found
#include "opencv2/imgproc/imgproc.hpp"
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
[Finished in 0.4s with exit code 1]
解决方案:
运行一下命令添加一条path
export PKG_CONFIG_PATH="/opencv解压目录/release/unix-install":"$PKG_CONFIG_PATH"
dyld 包依赖问题
dyld: Library not loaded: @rpath/libopencv_dnn.3.4.dylib
Referenced from: ~/App/test
Reason: image not found
bash: line 1: 18912 Abort trap: 6 ~/App/test
[Finished in 1.0s with exit code 134]
解决方案:
if you want to solve error on terminal you can use export DYLD_LIBRARY_PATH=your/lib:$DYLD_LIBRARY_PATH if you want to solve error on Xcode in build page , go to "Runpath Search Paths" add you lib path
运行一下命令添加一条path
export DYLD_LIBRARY_PATH=/usr/include/opencv/build/lib:$DYLD_LIBRARY_PATH
遇到问题,欢迎留言
原文链接。不支持转载!