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); 
    cv::waitKey(0);
    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

enjoy!

Prepackaged Binaries

Or… you know… just install OpenCV with:

brew install https://dl.dropboxusercontent.com/u/25729295/opencv.rb

 

Hacks (necessary) to the CMakeLists.txt files

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

# if(HAVE_QUICKTIME)
#   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/cap_qtkit.mm)
#   list(APPEND HIGHGUI_LIBRARIES "-framework QTKit" "-framework QuartzCore" "-framework AppKit")
# endif()

# if(HAVE_INTELPERC)
#   list(APPEND highgui_srcs src/cap_intelperc.cpp)
#   ocv_include_directories(${INTELPERC_INCLUDE_DIR})
#   list(APPEND HIGHGUI_LIBRARIES ${INTELPERC_LIBRARIES})
# endif(HAVE_INTELPERC)

# if(IOS)
#   add_definitions(-DHAVE_IOS=1)
#   list(APPEND highgui_srcs src/ios_conversions.mm src/cap_ios_abstract_camera.mm src/cap_ios_photo_camera.mm src/cap_ios_video_camera.mm)
#   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:

set(highgui_srcs
    # src/cap.cpp
    src/cap_images.cpp
    src/cap_ffmpeg.cpp
    src/loadsave.cpp
    src/utils.cpp
    src/window.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:

# if(BUILD_ANDROID_PACKAGE)
#   ocv_module_disable(nonfree)
# endif()
# 
# set(the_description "Functionality with possible limitations on the use")
# ocv_warnings_disable(CMAKE_CXX_FLAGS -Wundef -Wshadow)
# if(ENABLE_DYNAMIC_CUDA)
#   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

 

Advertisements
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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s