我想编译一个OpenCV C/C++ (MSYS2)程序。这样做的GNU方法是(在MSYS2 MINGW64 Shell上运行良好):
g++ main.cc -o main `pkg-config --cflags --libs opencv4`Bash完美地将后台识别为子表达式,但是PowerShell没有:
> 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'我做了一些研究,在PowerShell上的等价物应该是
g++ main.cc -o main $(pkg-config --cflags --libs opencv4)尽管$(pkg-config --cflags --libs opencv4)是用PowerShell编写子表达式的正确方法,但回显命令显示它产生了正确的输出(在本例中,编译器包含和链接器标志是我需要的):
> 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 (...)它根本行不通:
> 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.因此,我需要使用详细的变通方法来编译我的文件:
> 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 (...)我的PowerShell版本:
> $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我的问题是:如何使这个特殊的子表达式在PowerShell中工作,这样我就不必在g++ main.cc -o main之后复制和粘贴pkg-config --cflags --libs opencv4的输出。
发布于 2022-11-09 01:56:15
与您的bash命令等效的是:
g++ main.cc -o main (-split (pkg-config --cflags --libs opencv4))这就是:
(pkg-config --cflags --libs opencv4)执行您的pkg-config调用,通过将它封装在(...) ( 分组运算符 )中,它的输出可以参与更大的表达式。-split的一元形式串分裂算子,将pkg-config调用的输出分割成一个空格分隔的标记数组,这相当于bash通过其名为https://www.gnu.org/software/bash/manual/html_node/Word-Splitting.html的壳膨胀隐式所做的工作的一部分。注意:
$(...),PowerShell的子表达式运算符在这种情况下不能工作,因为它的输出将作为一个整体传递给目标可执行文件(或者,对于输出多行的外部程序调用,每一行全部将作为参数传递)。bash $(...) `...`**),(或PowerShell** (...) 中的遗留表单`...`**),)中,需要(...)(而在分配任务时,甚至不需要它,例如$var = Get-Date);PowerShell中的$(...)的主要用例在D54内,即可扩展(双引号)字符串内部。更根本的是,PowerShell的 no 等效于 [_shell expansions**_](https://www.gnu.org/software/bash/manual/html_node/Shell-Expansions.html):- Notably, [_parameter_ expansions](https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html) (e.g. `${var//foo/bar}`) are _not_ supported, and require use of _expressions_ instead (e.g. `($var -replace 'foo', 'bar')`)- _**Unquoted**_ **arguments are** _**not**_ **generally subject to special interpretation**, except if they contain _metacharacters_, which, if they are to be used _verbatim_, for syntactic reasons then must either be individually ```-_escaped_ (```, the so-called _backtick_, is PowerShell's [escape character](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Special_Characters)), or require the whole argument to be enclosed in _quotes_.- _**It is up to each target command**_ **to interpret special characters such as an initial** **`~`** **as referring to the home directory, or to interpret arguments such as** **`*.txt`** **as** _**wildcard patterns**_ - whether such an argument was passed in quotes or not.- Variable references (e.g., `$var`) and simple expressions based on them (e.g, `$var.Property`), (grouping) expressions (e.g. (`(1 + 2)`), as well as subexpressions (`$(...)`) and array subexpressions (`@(...)`) can be used _as-is_ as arguments, even if what they evaluate to contains _spaces_ - in the case of calling _external programs_, on Windows, PowerShell will double-quote the result _on demand_, behind the scenes; as stated, _multiple_ outputs are _each_ passed as an argument. - See [this answer](https://stackoverflow.com/a/41254359/45375) for more information.https://stackoverflow.com/questions/74369079
复制相似问题