OpenCV 2.4.9 compiling OSX with g++-4.8 and c++11 support

First configure your build environment for g++-4.8

export CC=/usr/local/bin/gcc-4.8
export CXX=/usr/local/bin/g++-4.8
export CMAKE_CXX_FLAGS=-std=c++11

Then, after applying the cmake hacks described at the end of this article, configure your opencv cmake project:

mkdir build && cd build
cmake -DWITH_QT=true -DBUILD_opencv_gpu=false -DBUILD_opencv_video=false -DBUILD_opencv_world=false -DBUILD_TESTS=false -DBUILD_PERF_TESTS=false ../opencv-2.4.9
make -j8
make install

Compiling the demo

stick the following in a opencv.cpp file:

#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
int main(int, char**){
    cv::String path = "test.png";
    cv::Mat img = cv::imread(path,CV_LOAD_IMAGE_COLOR);
    cv::imshow("test", img); 
    int b = 1;
    auto a = b;
    return 0;

and finally compile it with:

/usr/local/bin/g++-4.8 -I/usr/local/include -L/usr/local/lib -lopencv_core -lopencv_highgui -std=c++11 opencv.cpp -o opencv


Prepackaged Binaries

Or… you know… just install OpenCV with:

brew install


Hacks (necessary) to the CMakeLists.txt files

Comment out this stuff from the modules/highgui/CMakeLists.txt

#   list(APPEND highgui_srcs src/cap_qt.cpp)
#   list(APPEND HIGHGUI_LIBRARIES "-framework Carbon" "-framework QuickTime" "-framework CoreFoundation" "-framework QuartzCore")
# elseif(HAVE_QTKIT)
#   list(APPEND highgui_srcs src/
#   list(APPEND HIGHGUI_LIBRARIES "-framework QTKit" "-framework QuartzCore" "-framework AppKit")
# endif()

#   list(APPEND highgui_srcs src/cap_intelperc.cpp)
#   ocv_include_directories(${INTELPERC_INCLUDE_DIR})

# if(IOS)
#   add_definitions(-DHAVE_IOS=1)
#   list(APPEND highgui_srcs src/ src/ src/ src/
#   list(APPEND HIGHGUI_LIBRARIES "-framework Accelerate" "-framework AVFoundation" "-framework CoreGraphics" "-framework CoreImage" "-framework CoreMedia" "-framework CoreVideo" "-framework QuartzCore" "-framework AssetsLibrary")
# endif()

remove the cap.cpp from the compile list:

    # src/cap.cpp

as well as the tests (that would fail)

# ocv_add_accuracy_tests()
# ocv_add_perf_tests()

The nonfree module also seems to be problematic with g++, so get rid of that one too:

#   ocv_module_disable(nonfree)
# endif()
# set(the_description "Functionality with possible limitations on the use")
# ocv_warnings_disable(CMAKE_CXX_FLAGS -Wundef -Wshadow)
#   add_definitions(-DDYNAMIC_CUDA_SUPPORT)
#   ocv_define_module(nonfree EXCLUDE_CUDA opencv_imgproc opencv_features2d opencv_calib3d OPTIONAL opencv_ocl)
# else()
#   ocv_define_module(nonfree opencv_imgproc opencv_features2d opencv_calib3d OPTIONAL opencv_gpu opencv_ocl)
# endif()

Useful links


This entry was posted in C++, Mac OSX. Bookmark the permalink.

3 Responses to OpenCV 2.4.9 compiling OSX with g++-4.8 and c++11 support

  1. Lasse T says:

    Thank you so much for putting this information available. Commenting out the high gui CMakeLists.txt was critical to getting it compile, excellent work!

  2. Thanks much!

    I got this working with OpenCV 2.4.10 and GCC 4.9.2. I also had to make the following additional changes to the CMakeLists.txt in the root OpenCV directory:

    OCV_OPTION(WITH_OPENEXR “Include ILM support via OpenEXR” OFF)
    OCV_OPTION(WITH_OPENMP “Include OpenMP support” ON)
    OCV_OPTION(BUILD_OPENEXR “Build openexr from source” OFF )

    I originally was running into errors with “Undefined symbols for architecture x86_64 cv::imshow, cv::imread, cv::imwrite, etc,” because Homebrew installed a clang OpenCV whereas my project was using GCC. One solution would be to rebuild everything with clang but that was not appealing in my case because I wanted OpenMP support. So I instead used your solution to build a GCC version of OpenCV.

    • drtaglia says:

      I had the same issue as you… Needed OpenMP. Note however that there is also ClangOMP! I have pre-packaged binaries for osx somewhere. I had to abandon it in the end because CUDA doesn’t allow neither c++11 nor clang (newer version)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s