I want to compile an OpenCV C/C++ (MSYS2) program on Windows using solely the CLI. The GNU Bash way to do this is (which runs fine on MSYS2 MINGW64 Shell):
g++ main.cc -o main `pkg-config --cflags --libs opencv4`
Bash perfectly recognizes the backticks as a subexpression, however PowerShell doesn't:
> g++ main.cc -o main `pkg-config --cflags --libs opencv4`
g++.exe: error: unrecognized command-line option '--cflags'
g++.exe: error: unrecognized command-line option '--libs'
I've done some research, and the equivalent on PowerShell would be
g++ main.cc -o main $(pkg-config --cflags --libs opencv4)
Despite $(pkg-config --cflags --libs opencv4)
being the right way to write a subexpression in PowerShell as an echo command shows it produces the right output (in this case compiler include and linker flags I need):
> echo $(pkg-config --cflags --libs opencv4)
-IC:/msys64/mingw64/bin/../include/opencv4 -LC:/msys64/mingw64/bin/../lib -lopencv_gapi -lopencv_stitching -lopencv_alphamat -lopencv_aruco -lopencv_barcode -lopencv_bgsegm -lopencv_ccalib -lopencv_cvv -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm (...)
It simply won't work:
> g++ main.cc -o main $(pkg-config --cflags --libs opencv4)
main.cc:1:10: fatal error: opencv2/imgcodecs.hpp: No such file or directory
1 | #include <opencv2/imgcodecs.hpp>
| ^~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
Therefore, I need to use a verbose workaround to compile my file:
> g++ main.cc -o main -IC:/msys64/mingw64/bin/../include/opencv4 -LC:/msys64/mingw64/bin/../lib -lopencv_gapi -lopencv_stitching -lopencv_alphamat -lopencv_aruco -lopencv_barcode -lopencv_bgsegm -lopencv_ccalib -lopencv_cvv -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hdf -lopencv_hfs -lopencv_img_hash (...)
My PowerShell version:
> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.22621.608
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.22621.608
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
My question is: how can I make this particular subexpression work in PowerShell so that I don't have to resort to the verbose fallback of copying and pasting the output of
pkg-config --cflags --libs opencv4
after g++ main.cc -o main
?