How to link OpenCV in CPP with Premake5

321 Views Asked by At

I'd like to link the 'OpenCV' library with a 'Premake5'.

Since installing 'libopencv-dev' as 'apt install' on Ubuntu 18.04.

I have written and executed a simple C++ program, but it does not work.

How do I link the library? Help me...

[Here My CPP ... ]

#include "opencv2/opencv.hpp" 
#include "opencv2/highgui/highgui.hpp"

int main()
{
    cvNameWindow("Output", 1);
    cv::Mat output = cv::Mat::zeros(120,350,CV8UC3);
    putText(output, "Hello World", cvPoint(15, 70), CV_FONT_HERSHEY_PLAIN, 3, cvScalar(0,255,0), 4);
    cv::imshow("Output", output);
    cv::waitKey(0);

    return 0;
}

[And this my premake5.lua script]

workspace "HelloPremake"
    configurations {"Debug", "Release"}
    location "build"

project "HelloWorld"
    kind "WindowedApp"
    language "C++"
    configuration "gmake"

    libdirs {os.findlib("libopencv-dev")}
    files {"**.h", "**.cpp"}

    filter { "configurations:Debug" }
        defines { "DEBUG" }
        symbols "On"

    filter { "configurations:Release" }

    defines { "NDEBUG" }
    symbols "On"

Put the main.cpp and preMake5.lua in the same folder and run './premake5 gmake'.

Then build through 'cd build && make', but you cannot link OpenCV properly.

1

There are 1 best solutions below

0
On

i fixed this.

[The cpp code must be:]

#include "opencv2/opencv.hpp" 
#include "opencv2/highgui/highgui.hpp"

int main(int argc, char** argv)
{
    //create a gui window:
    cv::namedWindow("Output",1);
    
    //initialize a 120X350 matrix of black pixels:
    cv::Mat output = cv::Mat::zeros( 120, 350, CV_8UC3 );
    
    //write text on the matrix:
    cv::putText(output,
            "Hello World :)",
            cvPoint(15,70),
            cv::FONT_HERSHEY_PLAIN,
            3,
            cvScalar(255,255,0),
            4);
    
    //display the image:
    imshow("Output", output);
    
    //wait for the user to press any key:
    cv::waitKey(0);
    
    return 0;
}

[The premake5.lua code must be:]

workspace "HelloPremake"
    configurations {"Debug", "Release"}
    location "build"

project "HelloWorld"
    kind "WindowedApp"
    language "C++"
    configuration "gmake"
        links{"opencv_core", "opencv_imgproc", "opencv_highgui"}
        files {"**.h", "**.cpp", "**.hpp"}

    filter { "configurations:Debug" }
        defines { "DEBUG" }
        symbols "On"

    filter { "configurations:Release" }

    defines { "NDEBUG" }
    symbols "On"