Compiling C++ Source Code with OpenCV Flag on Powershell

Hello. I want to write a Powershell script on Windows to speed up the code compilation and execution.

At Linux I used this to compile at Terminal.
g++ -g -Wall -o resizeimage resizeimage.cpp 'pkg-config --cflags --libs opencv4'

And to run it, simply I use
./resizeimage lena.png

What is the Windows way to tell compiler to use OpenCV libraries?

Edit: I’d like to use g++ but if there are easier ways to do that with other compilers I am okay with those too.

which compiler do you want to use ?

Sorry I forgot the at that. I use g++ but if there are easier ways to do that with other compilers I am okay with those too.

well, you need to install / build the opencv libs first.
(if you want to use prebuilt libs, you’re bound to VS anyway)

something like:

cl myprog.cpp /I /path/to/opencv/includes /L /path/to/opencv/libs opencv_world452.lib

I actually built OpenCV by using CMake and I use OpenCV on Windows, compile and run my codes by using Visual Studio 2017 Community Edition but I want to make compilation and run at CMD/Powershell.

1 Like

also: cmake example

cl myprog.cpp /I /path/to/opencv/includes /L /path/to/opencv/libs opencv_world452.lib

So do I have to write something like this?

cl myprog.cpp /I D:\OpenCV_with_CUDA\build\install\include\opencv2 /L D:\OpenCV_with_CUDA\build\install\x64\vc15\lib opencv_world452.lib

Or do I need to add specific path for the all headers and libs that I use in my C++ source file. Something like this.

cl myprog.cpp /I D:\OpenCV_with_CUDA\build\install\include\opencv2\core D:\OpenCV_with_CUDA\build\install\include\opencv2\core\cuda /L D:\OpenCV_with_CUDA\build\install\x64\vc15\lib\opencv_cudaarithm450.lib D:\OpenCV_with_CUDA\build\install\x64\vc15\lib\opencv_core450.lib D:\OpenCV_with_CUDA\build\install\x64\vc15\lib\opencv_core450d.lib opencv_world452.lib

By the way I don’t have opencv_world452.lib in my build folders.

/I D:\OpenCV_with_CUDA\build\install\include

is the correct include path (w/o opencv2)

yea, that was just an example (if you build from src, you will have “single module” libs)

linker path should be:

/L D:\OpenCV_with_CUDA\build\install\x64\vc15\lib

then you do not need the complete path to specific libs any more,

 opencv_core450.lib opencv_imgproc450.lib opencv_highgui450.lib etc

note, that the libs ending with 450d are debug libs, and should not be mixed with release ones !
(specify /DEBUG on the cmdline for debug, similar to -g with gcc)

it is very much recommended to write a CMakeLists.txt for your project (according to the tutorial/example above), use cmake to generate a Makefile or visual studio project/solution or whatever you need, and then build from that.

as far as I’m aware, Visual Studio supports building of projects/solutions from the command line.

you can call MSVC’s cl.exe directly but you should not because it’s error-prone and needlessly complicated to reproduce.

In the end this is what I am trying to achieve but in Windows’ Powershell.


    g++ -g -Wall  -o resizeimage resizeimage.cpp `pkg-config --cflags --libs opencv4`
    ./resizeimage snow.jpg

    echo -e

    g++ -g -Wall -o parallelcode3 parallelcode3.cpp `pkg-config --cflags --libs opencv4` -fopenmp

    imgName=("snow.jpg" "snow2.jpg" "snow3.jpg" "snow4.jpg" "snow5.jpg" "snow6.jpg")

    for i in "${imgName[@]}"
       echo Image Name: $i
       ./parallelcode3  $i
       echo -e

And then just making a call
to automatically run executables with given arguments.

Did you have any progress?