Mac上C++中opencv的环境配置

Mac上C++中opencv的环境配置

安装opencv

brew install opencv

git clone -b 3.4 https://github.com/opencv/opencv.git
cd opencv
mkdir build
cd build
#注意下一步最后有两个点,markdown编辑出来可能显示有问题
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local …
make -j8
make install

完成!

使用该指令可查看是否成功安装opencv

pkg-config --cflags --libs opencv

若没有pkg-config可直接brew install pkg-config
关于pkg-config的介绍可参考:这篇博客
以上即完成opencv的安装!

opencv在c++中的使用

使用g++ 编译c++ :

g++ main.cpp -o main -L 库文件所在路径 -l 库文件名字

比如:用到,就可以

g++ main.cpp -o main -L /usr/local/include/opencv -l opencv_highgui

或直接

g++ $(pkg-config --cflags --libs opencv) main.cpp -o main

使用cmake编译c++:

新建CMakeLists.txt,输入以下内容:

cmake_minimum_required(VERSION 3.12)
project(main)

set(CMAKE_CXX_STANDARD 11)

find_package(OpenCV REQUIRED)

include_directories(${OpenCV_INCLUDE_DIRS})
set(CMAKE_CXX_STANDARD 11)

add_executable(main main.cpp)

target_link_libraries(main ${OpenCV_LIBS})

添加环境变量(应该不添加环境变量也可以)
vim ~/.bash_profile

export LIBRARY_PATH=/usr/local/lib/:$LIBRARY_PATH
export LD_LIBRARY_PATH=/usr/local/lib$LD_LIBRARY_PATH
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH
export C_INCLUDE_PATH=/usr/local/include:$C_INCLUDE_PATH
export CPLUS_INCLUDE_PATH=/usr/local/include:$CPLUS_INCLUDE_PATH

VS Code常见问题

问题1:

使用vscode编写c++时,能正常导入包编译运行,但是头文件有红色波浪线。

解决方法:
安装C/C++插件
command + shift + P,点击Edit Configurations(JSON)
工作目录会生成一个.vscode目录,找到c_cpp_properties.json文件

命令行输入:

gcc -v -E -x c++ -

得到:

#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/Library/Developer/CommandLineTools/usr/include/c++/v1
/Library/Developer/CommandLineTools/usr/lib/clang/10.0.1/include
/Library/Developer/CommandLineTools/usr/include
/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include
/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/System/Library/Frameworks (framework directory)
End of search list.

将得到的几个路径添加到c_cpp_properties.json中的"includePath"中(注意最后的/**):

"/usr/local/include/**"
"/Library/Developer/CommandLineTools/usr/include/c++/v1/**",
"/Library/Developer/CommandLineTools/usr/lib/clang/10.0.1/include/**",
"/Library/Developer/CommandLineTools/usr/include/**",
"/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/**",
"/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/**"

解决。

问题2

vscode下python能正常运行,但是编写调用包对应的函数时,有红色波浪线,显示找不到这个module

解决方法:

打开设置,右上角打开settings.json,在里边加入

"python.linting.pylintArgs": ["--generate-members"]

解决。

你可能感兴趣的:(c++,opencv,mac,cmake,vscode)