Configuring Sublime to work with OpenCV on Mac

Standard

Sorry for the shilling, but here’s my upcoming project:

https://plusplusone.herokuapp.com

Please register your email address if you’re interested in it.


So, I just bought Sublime Text from http://www.sublimetext.com, which is supposed to be a very good cross platform text editor. I’ve been programming OpenCV applications using XCode, but rarely using editor or even compiling it from command line.

However, after tinkering a bit, I’ve managed to create a this built system for Sublime, which should allow you to compile and run OpenCV / C++ application from it. Just go to Tools > Build System > New Build System

My OpenCV was installed using HomeBrew, which has pkg-config information for opencv already created inside /usr/Local/Cellar/opencv/2.4.4/lib/pkgconfig/, thus you can utilize pkg-config to generate the include and library flags for g++ from it.


{
 "cmd": ["g++", "-Wall", "-Wextra", "${file}", "-o", "${file_path}/${file_base_name}",
 "-I/usr/local/Cellar/opencv/2.4.4/include/opencv",
 "-I/usr/local/Cellar/opencv/2.4.4/include",
 "/usr/local/Cellar/opencv/2.4.4/lib/libopencv_calib3d.dylib",
 "/usr/local/Cellar/opencv/2.4.4/lib/libopencv_contrib.dylib",
 "/usr/local/Cellar/opencv/2.4.4/lib/libopencv_core.dylib",
 "/usr/local/Cellar/opencv/2.4.4/lib/libopencv_features2d.dylib",
 "/usr/local/Cellar/opencv/2.4.4/lib/libopencv_flann.dylib",
 "/usr/local/Cellar/opencv/2.4.4/lib/libopencv_gpu.dylib",
 "/usr/local/Cellar/opencv/2.4.4/lib/libopencv_highgui.dylib",
 "/usr/local/Cellar/opencv/2.4.4/lib/libopencv_imgproc.dylib",
 "/usr/local/Cellar/opencv/2.4.4/lib/libopencv_legacy.dylib",
 "/usr/local/Cellar/opencv/2.4.4/lib/libopencv_ml.dylib",
 "/usr/local/Cellar/opencv/2.4.4/lib/libopencv_nonfree.dylib",
 "/usr/local/Cellar/opencv/2.4.4/lib/libopencv_objdetect.dylib",
 "/usr/local/Cellar/opencv/2.4.4/lib/libopencv_ocl.dylib",
 "/usr/local/Cellar/opencv/2.4.4/lib/libopencv_photo.dylib",
 "/usr/local/Cellar/opencv/2.4.4/lib/libopencv_stitching.dylib",
 "/usr/local/Cellar/opencv/2.4.4/lib/libopencv_ts.dylib",
 "/usr/local/Cellar/opencv/2.4.4/lib/libopencv_video.dylib",
 "/usr/local/Cellar/opencv/2.4.4/lib/libopencv_videostab.dylib"],
 "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
 "working_dir": "${file_path}",
 "selector": "source.c, source.c++",

"variants":
 [{
"name": "Run",
"cmd": ["bash", "-c", "g++ '${file}' -o '${file_path}/${file_base_name}' `/usr/bin/pkg-config --cflags --libs   /usr/Local/Cellar/opencv/2.4.4/lib/pkgconfig/opencv.pc` && '${file_path}/${file_base_name}' "]
}]
}

Advertisements

12 thoughts on “Configuring Sublime to work with OpenCV on Mac

  1. Peter.Lee

    Hi, I have tried your method, but it did not work.
    And the Sublime Text2 shows:
    ———————————————–
    bash: /usr/bin/pkg-config: No such file or directory
    Undefined symbols for architecture x86_64:
    “_cvLoadImage”, referenced from:
    _main in ccaVa2ML.o
    “_cvNamedWindow”, referenced from:
    _main in ccaVa2ML.o
    “_cvShowImage”, referenced from:
    _main in ccaVa2ML.o
    “_cvWaitKey”, referenced from:
    _main in ccaVa2ML.o
    ld: symbol(s) not found for architecture x86_64
    collect2: ld returned 1 exit status
    [Finished in 3.6s with exit code 1]
    ————————————————-
    And I find that my Mac(Snow Leopard 10.6.8) has no “pkg-config” under “/usr/bin”.
    How should I do?
    ————————————————-
    Thank you very much……

  2. Hi Peter,

    I can’t remember clearly whether I installed pkg-config using MacPorts or Homebrew, but you could either of these:
    1. sudo port install pkgconfig
    to install pkgconfig using MacPorts, or
    2. sudo brew install pkg-config
    to install using Homebrew

    Anyway, what pkg-config does is to provide a simpler way to get information about the installed library, running
    /usr/bin/pkg-config –cflags –libs /usr/Local/Cellar/opencv/2.4.4/lib/pkgconfig/opencv.pc
    on my Mac gives me

    -I/usr/local/Cellar/opencv/2.4.4/include/opencv -I/usr/local/Cellar/opencv/2.4.4/include /usr/local/Cellar/opencv/2.4.4/lib/libopencv_calib3d.dylib /usr/local/Cellar/opencv/2.4.4/lib/libopencv_contrib.dylib /usr/local/Cellar/opencv/2.4.4/lib/libopencv_core.dylib /usr/local/Cellar/opencv/2.4.4/lib/libopencv_features2d.dylib /usr/local/Cellar/opencv/2.4.4/lib/libopencv_flann.dylib /usr/local/Cellar/opencv/2.4.4/lib/libopencv_gpu.dylib /usr/local/Cellar/opencv/2.4.4/lib/libopencv_highgui.dylib /usr/local/Cellar/opencv/2.4.4/lib/libopencv_imgproc.dylib /usr/local/Cellar/opencv/2.4.4/lib/libopencv_legacy.dylib /usr/local/Cellar/opencv/2.4.4/lib/libopencv_ml.dylib /usr/local/Cellar/opencv/2.4.4/lib/libopencv_nonfree.dylib /usr/local/Cellar/opencv/2.4.4/lib/libopencv_objdetect.dylib /usr/local/Cellar/opencv/2.4.4/lib/libopencv_ocl.dylib /usr/local/Cellar/opencv/2.4.4/lib/libopencv_photo.dylib /usr/local/Cellar/opencv/2.4.4/lib/libopencv_stitching.dylib /usr/local/Cellar/opencv/2.4.4/lib/libopencv_ts.dylib /usr/local/Cellar/opencv/2.4.4/lib/libopencv_video.dylib /usr/local/Cellar/opencv/2.4.4/lib/libopencv_videostab.dylib

    Try to install pkg-config and see how it goes.

    • Peter.Lee

      But it’s so weird. The homebrew shows: “Error: pkg-config-0.27.1 already installed”. And I really have not the “/usr/bin/pkg-config”. Is it possible that this guy can be under some other folders?
      Can I not use the pkg-config to use the OpenCV Library under Sublime Text2 ?

      • I guess you could just type down all the includes for header and library files instead of using pkg-config command, it’s the same thing. But you might come across issue where it hits the max characters limit in shell command ( I believe I encountered problems like that before ). Just try out manually type down all the include headers and libraries.

  3. Peter.Lee

    You know my mother language is not the English, and have not understood the sentence “type down all the includes for header and library files” clearly. Do you mean that I just type down the whole things and do not hope the “Auto Complete” to help me?

    • Yes, instead of `/usr/bin/pkg-config –cflags –libs /usr/Local/Cellar/opencv/2.4.4/lib/pkgconfig/opencv.pc`

      replace it with something similar like this:
      -I/usr/local/Cellar/opencv/2.4.4/include/opencv -I/usr/local/Cellar/opencv/2.4.4/include /usr/local/Cellar/opencv/2.4.4/lib/libopencv_calib3d.dylib /usr/local/Cellar/opencv/2.4.4/lib/libopencv_contrib.dylib /usr/local/Cellar/opencv/2.4.4/lib/libopencv_core.dylib /usr/local/Cellar/opencv/2.4.4/lib/libopencv_features2d.dylib /usr/local/Cellar/opencv/2.4.4/lib/libopencv_flann.dylib /usr/local/Cellar/opencv/2.4.4/lib/libopencv_gpu.dylib /usr/local/Cellar/opencv/2.4.4/lib/libopencv_highgui.dylib /usr/local/Cellar/opencv/2.4.4/lib/libopencv_imgproc.dylib /usr/local/Cellar/opencv/2.4.4/lib/libopencv_legacy.dylib /usr/local/Cellar/opencv/2.4.4/lib/libopencv_ml.dylib /usr/local/Cellar/opencv/2.4.4/lib/libopencv_nonfree.dylib /usr/local/Cellar/opencv/2.4.4/lib/libopencv_objdetect.dylib /usr/local/Cellar/opencv/2.4.4/lib/libopencv_ocl.dylib /usr/local/Cellar/opencv/2.4.4/lib/libopencv_photo.dylib /usr/local/Cellar/opencv/2.4.4/lib/libopencv_stitching.dylib /usr/local/Cellar/opencv/2.4.4/lib/libopencv_ts.dylib /usr/local/Cellar/opencv/2.4.4/lib/libopencv_video.dylib /usr/local/Cellar/opencv/2.4.4/lib/libopencv_videostab.dylib

      I guess it should work. But if it doesn’t, maybe try other IDE (XCode, Eclipse), Sublime is not really a good IDE for C++ programming.

  4. Alon

    Thanks for sharing this!

    I’m trying to run it on mac os mavericks, I used macport to install opencv originally, so my files located at /opt/local/lib/ and /opt/local/include/, I replaced them in the file with the paths you had.

    at first I got the following error
    bash: /usr/bin/pkg-config: No such file or directory
    so I removed it from the end of the cmd path and left only the /opt/local/lib/pkgconfig/opencv.pc though that gives me an bash: /opt/local/lib/pkgconfig/opencv.pc: Permission denied error.

    any idea how to modify the file?

    Thanks in advance!

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