Posted on 11-08-2016
Filed Under (Technology) by bigbang

The library versions using in this article: OpenCV2.4.13, FFMpeg 2.8.7, CMake 3.5.

First of all, you need to install FFMpeg on Mac OSX first. The step is very easy just like doing it on other Unix/Linux platform.

Then download OpenCV 2.4.13 and unzip it to a folder. Enter this folder from terminal and here are basic steps for OpenCV 2.4.13 compilation under Mac:

mkdir build
cd build
cmake -G "Unix Makefiles" ..
make -j8
sudo make install

According to OpenCV version and FFMpeg dependency, you might meet some compilation errors.

Error 1: not generating corresponding makefile

Here is an example error message:

In file included from /Users/collins/Documents/temp/opencv-2.4.13/3rdparty/zlib/zlib.h:34:

/Users/collins/Documents/temp/opencv-2.4.13/release/3rdparty/zlib/zconf.h:426:14: fatal error:

      'sys/types.h' file not found

#    include <sys/types.h>      /* for off_t */

	     ^
1 error generated.

Try to generate correct makefile on Mac, you should use following parameters in cmake command:

cmake -G "Unix Makefiles" ..

So your final OpenCV project generating cmake parameters should be something like this:

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -G "Unix Makefiles" ..

Error 2: link CoreMedia, VideoToolBox frameworks or something else on Mac

An example error message is as following:

[ 33%] Building CXX object modules/highgui/CMakeFiles/opencv_highgui.dir/src/bitstrm.cpp.o

[ 33%] Linking CXX shared library ../../lib/libopencv_highgui.dylib

Undefined symbols for architecture x86_64:

  "_CMBlockBufferCreateWithMemoryBlock", referenced from:

      _videotoolbox_common_end_frame in libavcodec.a(videotoolbox.o)

  "_CMSampleBufferCreate", referenced from:

      _videotoolbox_common_end_frame in libavcodec.a(videotoolbox.o)

  "_CMVideoFormatDescriptionCreate", referenced from:

      _av_videotoolbox_default_init2 in libavcodec.a(videotoolbox.o)

  "_SSLClose", referenced from:

      _tls_open in libavformat.a(tls_securetransport.o)

      _tls_close in libavformat.a(tls_securetransport.o)
  "_SSLCopyPeerTrust", referenced from:

The reason of above error message is that some FFMpeg version under Mac is depended on VideoToolBox & CoreMedia Frameworks.

The solution is to modify and add following code into opencv-x.x.x/modules/highgui/CMakeLists.txt file:

find_library(CORE_MEDIA CoreMedia)
if (NOT CORE_MEDIA)
    message(FATAL_ERROR "CoreMedia not found")
endif()

find_library(VIDEO_TOOL_BOX VideoToolBox)
if (NOT VIDEO_TOOL_BOX)
    message(FATAL_ERROR "VideoToolBox not found")
endif()

find_library(SECURITY Security)
if (NOT SECURITY)
    message(FATAL_ERROR "Security not found")
endif()

target_link_libraries(${the_module} ${CORE_MEDIA} ${VIDEO_TOOL_BOX} ${SECURITY})

So at the compilation time, it will link VideoToolBox, CoreMedia, Security frameworks under System/Library/Frameworks folder.

Another solution is not to compile highgui library, you can do this such kind of thing like this:

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_opencv_highgui=OFF -G "Unix Makefiles" ..

Adding CoreMedia & VideoToolBox libraries is not enough, You might still get following errors:

ld: warning: directory not found for option '-L/usr/local/lib/Debug'
ld: warning: directory not found for option '-L/usr/local/Cellar/x264/r2601/lib/Debug'
ld: warning: directory not found for option '-L/usr/local/Cellar/libvpx/1.5.0/lib/Debug'
ld: warning: directory not found for option '-L/usr/local/Cellar/opus/1.1.1/lib/Debug'
ld: warning: directory not found for option '-L/usr/local/Cellar/freetype/2.6_1/lib/Debug'
ld: warning: directory not found for option '-L/usr/local/Cellar/fdk-aac/0.1.4/lib/Debug'
ld: warning: directory not found for option '-L/usr/local/Cellar/libass/0.13.1/lib/Debug'
Undefined symbols for architecture x86_64:
  "_SSLClose", referenced from:
      _tls_open in libavformat.a(tls_securetransport.o)
      _tls_close in libavformat.a(tls_securetransport.o)
  "_SSLCopyPeerTrust", referenced from:
      _tls_open in libavformat.a(tls_securetransport.o)
  "_SSLCreateContext", referenced from:
      _tls_open in libavformat.a(tls_securetransport.o)
  "_SSLHandshake", referenced from:
      _tls_open in libavformat.a(tls_securetransport.o)
  "_SSLRead", referenced from:
      _tls_read in libavformat.a(tls_securetransport.o)
  "_SSLSetCertificate", referenced from:

That’s the reason why I need to add security.framework to CMakeLists.txt file.

According to different OpenCV & FFMpeg version, you might try to link other frameworks this way by your self to make sure you can install OpenCV successfully.

(0) Comments    Read More   
Posted on 11-08-2016
Filed Under (Technology) by bigbang

While writing program on Mac, sometimes we need to link framework and libraries under OSX folder System/Library/Frameworks. There are so many frameworks like CoreMedia & VideoToolBox, which you will use in FFMpeg or something else someday.

In this situation, we have to use find_library as it includes some special handling for frameworks on OSX.

Also, don’t use link_directories, CMake use full paths to libraries and it’s not needed.

Here’s some simple example with CoreMedia:

find_library(CORE_MEDIA CoreMedia)
if (NOT CORE_MEDIA)
    message(FATAL_ERROR "CoreMedia not found")
endif()

add_executable(program ${program_SOURCES})
target_link_libraries(program ${CORE_MEDIA})
(0) Comments    Read More