之前写了一篇文章:
Windows环境使用VS2017编译opencv-4.2.0和opencv_contrib-4.2.0源码,支持TBB
现在macOS也需要使用opencv,使用clang编译源码,与Windows编译的不同点有几个:
1、准备好编译器
先安装好Xcode和Command Tools
再Agree to Xcode license in Terminal: sudo xcodebuild -license
2、不需要使能TBB,opencv-4.2.0自带并默认使用了Apple的GCD并行库。
Grand Central Dispatch(GCD)是Apple开发的一个多核编程的较新的解决方法。
在Mac OSX 10.6首次推出,也可在iOS4及以上版本使用。
它主要用于优化应用程序以支持多核处理器以及其他对称多处理系统。
它是一个在线程池模式的基础上执行的并发任务。
使用GCD的好处:
GCD可用于多核的并行运算;
GCD会自动利用更多的 CPU 内核(比如双核、四核);
GCD会自动管理线程的生命周期(创建线程、调度任务、销毁线程),程序员只需要告诉 GCD 想要执行什么任务。
https://opensource.apple.com/source/libdispatch/
https://opensource.apple.com/tarballs/libdispatch/
3、cmake的Configure选择Unix Makefiles
cmake的install参数默认值是CMAKE_INSTALL_PREFIX=/usr/local,请用户根据需要,自行决定是否要更改路径。
4、cmake Generate之后,会生成Makefile文件,使用终端命令进行编译
sudo make clean
sudo make -j8
sudo make install
5、应用实践
以qt为例,建立demo,pro文件如下:
#------------------------------------------------- # # Project created by QtCreator 2019-08-29T10:43:54 # #------------------------------------------------- QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = qt_opencv_demo TEMPLATE = app # The following define makes your compiler emit warnings if you use # any feature of Qt which as been marked as deprecated (the exact warnings # depend on your compiler). Please consult the documentation of the # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS # You can also make your code fail to compile if you use deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += main.cpp\ Widget_op.cpp \ mylabel.cpp HEADERS += \ mylabel.h \ Widget_op.h \ stable.h FORMS += Widget_op.ui #指定生成的临时文件放置的目录 MOC_DIR = temp/moc RCC_DIR = temp/rcc UI_DIR = temp/ui OBJECTS_DIR = temp/obj #指定编译器选项和项目配置 #CONFIG += c++11 CONFIG += warn_on #告诉qmake要把编译器设置为输出警告信息的 #CONFIG += warn_off #不要警告输出 CONFIG += precompile_header #可以在项目中使用预编译头文件的支持 #预编译头文件路径 PRECOMPILED_HEADER = $$PWD/stable.h #disable C4819 warning win32:QMAKE_CXXFLAGS_WARN_ON += -wd4819 win32:QMAKE_CXXFLAGS += /FS #避免VC编译器关于fopen等应使用fopen_s的安全警告 win32:DEFINES += _CRT_SECURE_NO_WARNINGS #指定生成的应用程序放置的目录 win32:CONFIG(debug, debug|release){ contains(DEFINES, WIN64) { DESTDIR = ../_debug64 } else { DESTDIR = ../_debug32 } } else:win32:CONFIG(release, debug|release){ contains(DEFINES, WIN64) { DESTDIR = ../_release64 } else { DESTDIR = ../_release32 } } macx:CONFIG(debug, debug|release){ DESTDIR = ../bind } else:macx:CONFIG(release, debug|release){ DESTDIR = ../bin } message($$PWD) message($$DESTDIR) #Opencv Configuration isEmpty(MY_OPENCV_PATH) { win32:MY_OPENCV_PATH = "D:/My Resources/ImageProcessingLibrary/opencv/" #注意斜杆的方向;引号; macx:MY_OPENCV_PATH = "/Users/firecat/opencv4.2.0" message($$MY_OPENCV_PATH) } #头文件包含路径 win32 { INCLUDEPATH += $${MY_OPENCV_PATH}/include/ DEPENDPATH += $${MY_OPENCV_PATH}/include/ } macx { INCLUDEPATH += $${MY_OPENCV_PATH}/include/opencv4 DEPENDPATH += $${MY_OPENCV_PATH}/include/opencv4 } #库文件包含路径 win32 { contains(QT_ARCH, i386) { message("32-bit") #Windows x86 (32bit) specific build here MY_OPENCV_LIBS_PATH = $${MY_OPENCV_PATH}/Win32/vc15/lib } else { message("64-bit") #Windows x64 (64bit) specific build here MY_OPENCV_LIBS_PATH = $${MY_OPENCV_PATH}/x64/vc15/lib } message($$MY_OPENCV_LIBS_PATH) } macx { MY_OPENCV_LIBS_PATH = $${MY_OPENCV_PATH}/lib message($$MY_OPENCV_LIBS_PATH) } #依赖库的名称 win32:CONFIG(debug, debug|release): {#动态库 LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_calib3d420d LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_core420d LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_features2d420d LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_flann420d LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_highgui420d LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_imgcodecs420d LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_imgproc420d LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_videoio420d } else:win32:CONFIG(release, debug|release):{ LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_calib3d420 LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_core420 LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_features2d420 LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_flann420 LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_highgui420 LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_imgcodecs420 LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_imgproc420 LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_videoio420 } else:macx:{ LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_calib3d.4.2.0 LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_core.4.2.0 LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_features2d.4.2.0 LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_flann.4.2.0 LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_highgui.4.2.0 LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_imgcodecs.4.2.0 LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_imgproc.4.2.0 LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_videoio.4.2.0 } #pro文件的参考文档 #https://doc.qt.io/qt-5/qmake-variable-reference.html