首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Powershell子表达式不识别"pkg-config --c阻击--libs opencv4“命令。

Powershell子表达式不识别"pkg-config --c阻击--libs opencv4“命令。
EN

Stack Overflow用户
提问于 2022-11-09 01:49:28
回答 1查看 62关注 0票数 5

我想编译一个OpenCV C/C++ (MSYS2)程序。这样做的GNU方法是(在MSYS2 MINGW64 Shell上运行良好):

代码语言:javascript
复制
g++ main.cc -o main `pkg-config --cflags --libs opencv4`

Bash完美地将后台识别为子表达式,但是PowerShell没有:

代码语言:javascript
复制
> 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上的等价物应该是

代码语言:javascript
复制
g++ main.cc -o main $(pkg-config --cflags --libs opencv4)

尽管$(pkg-config --cflags --libs opencv4)是用PowerShell编写子表达式的正确方法,但回显命令显示它产生了正确的输出(在本例中,编译器包含和链接器标志是我需要的):

代码语言:javascript
复制
> 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 (...)

它根本行不通:

代码语言:javascript
复制
> 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.

因此,我需要使用详细的变通方法来编译我的文件:

代码语言:javascript
复制
> 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版本:

代码语言:javascript
复制
> $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的输出。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-09 01:56:15

与您的bash命令等效的是:

代码语言:javascript
复制
g++ main.cc -o main (-split (pkg-config --cflags --libs opencv4))

这就是:

注意:

  • 使用$(...),PowerShell的子表达式运算符在这种情况下不能工作,因为它的输出将作为一个整体传递给目标可执行文件(或者,对于输出多行的外部程序调用,每一行全部将作为参数传递)。
  • 一般情况下,在bash $(...) `...`**),(或PowerShell** (...) 中的遗留表单`...`**),)中,需要(...)(而在分配任务时,甚至不需要它,例如$var = Get-Date);PowerShell中的$(...)的主要用例在D54内,即可扩展(双引号)字符串内部。
  • 更根本的是,PowerShell的 no 等效于 [_shell expansions**_](https://www.gnu.org/software/bash/manual/html_node/Shell-Expansions.html):
代码语言:javascript
复制
- 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')`)
代码语言:javascript
复制
- _**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 `&grave;`-_escaped_ (`&grave;`, 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_.
代码语言:javascript
复制
- _**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.
代码语言:javascript
复制
- 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.
代码语言:javascript
复制
    - See [this answer](https://stackoverflow.com/a/41254359/45375) for more information.
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74369079

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档