c++opencvcoderunner

Including external libraries in CodeRunner 2 app?


I've always used Xcode to compile OpenCV based code in c++. The procedure in Xcode was quite simple, I just had to mention the paths and add the necessary lib files to the project. Theres this app called CodeRunner 2 for macOS. Theres no proper documentation on how to include external libraries to compile code in this app. Is it possible to link OpenCV headers and compile them in CodeRunner ? If yes, could someone post the steps?


Solution

  • You can run OpenCV in CodeRunner by setting up a new language. Go to Preferences -> Languages, right-click C++, and select Duplicate. Name the new language "C++ OpenCV". On the right side of the preferences window, click Settings then the Edit Script button. Look for this line (or something similar):

    xcrun clang++ -x c++ -lc++ -o "$out" "${files[@]}" "${@:1}"
    

    Add the clang++ command line parameters for OpenCV after "$out". Here's my version:

    xcrun clang++ -x c++ -lc++ -o "$out" -I/usr/local/opt/opencv3/include -L/usr/local/opt/opencv3/lib -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_imgcodecs -lopencv_videoio -lopencv_calib3d "${files[@]}" "${@:1}"
    

    Modify the -I and -L parameters to match your OpenCV install path. On this machine I used Homebrew to install OpenCV so it was installed in /usr/local/opt. On other machines I've compiled from source so OpenCV is installed in /usr/local/lib.

    Modify the -l parameters to include the libraries you typically use.

    After saving the compile script, go back to Preferences -> Languages and select the Templates button. You can set up a template for OpenCV programs. Here's mine:

    #include <iostream>
    
    #include "opencv2/core.hpp"
    #include "opencv2/imgproc.hpp"
    #include "opencv2/highgui.hpp"
    
    using namespace std;
    
    int main(int argc, char *argv[]) {
        cv::Mat image;
        // read an image
        if (argc < 2)
            image = cv::imread("img.jpg");
        else
            image = cv::imread(argv[1]);
    
        if (!image.data) {
            std::cout << "Image file not found\n";
            return 1;
        }
    
        // create image window named "asdfasdf"
        cv::namedWindow("asdfasdf");
        // show the image on window
        cv::imshow("asdfasdf", image);
        // wait for key
        cv::waitKey(0);
    
        return 0;
    }