在 Windows 上的当前目录中,我有以下脚本文件 simple_script.sh:
#!/bin/bash
echo "hi from simple script"
Run Code Online (Sandbox Code Playgroud)
我希望通过 powershell 命令行在 wsl 上运行此脚本。
使用该wsl
命令,我找不到告诉 wsl 调用脚本代码的方法。
以下命令有效(我认为)
wsl bash -c "echo hi from simple script"
Run Code Online (Sandbox Code Playgroud)
但是,当尝试将脚本内容加载到变量中并运行时,它无法按预期工作:
$simple_script = Get-Content ./simple_script.sh
wsl bash -c $simple_script
Run Code Online (Sandbox Code Playgroud)
失败:
bash: -c: option requires an argument
Run Code Online (Sandbox Code Playgroud)
我尝试了几种变体。Get-Content
与标志一起使用-Raw
似乎会打印字符串中的第一个单词(但不是整个字符串)。不包含“”字符的命令有时似乎有效。但我还没有找到一致的方法。
类似的问题似乎不能直接与 wsl 一起工作,并且似乎不能运行驻留在 Windows 文件系统上的脚本文件。
从版本 4.6.1开始,ccache 支持使用 msvc 进行编译。
在我的 Windows 环境中,我安装了 ccache,并可以通过命令行使用它。我尝试通过以下方式将 ccache 集成到我的 cmake 项目中:
根 CMakeLists.txt:
find_program(CCACHE_FOUND ccache)
if(CCACHE_FOUND)
message("CCACHE is found")
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ccache) # Less useful to do it for linking, see edit2
else(CCACHE_FOUND)
message("CCACHE is NOT found")
endif(CCACHE_FOUND)
Run Code Online (Sandbox Code Playgroud)
这是我在 CMakePresets.json 中的 cmake 配置:
{
"name": ",
"hidden": false,
"generator": "Visual Studio 17 2022",
"toolset": {
"value": "host=x64",
"strategy": "external"
},
"architecture": {
"value": "x64",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_C_COMPILER": "cl.exe", …
Run Code Online (Sandbox Code Playgroud) 我有一个 python 环境(在 Windows 10 上),它使用 OpenCVVideoCapture
类连接到多个 USB 摄像头。
据我所知,除了类构造函数/方法device
中的参数之外,没有其他方法可以识别 OpenCV 中的特定相机。VideoCapture
open
问题是设备参数会根据实际连接的摄像头数量和 USB 端口而变化。
我希望能够识别特定的相机并找到其“设备索引”或“相机索引”,无论连接了多少个相机以及连接到哪个 USB 端口。
有人可以建议一种实现该功能的方法吗?python 代码更好,但 C++ 也可以。
我想检查某种类型是否可以与std::format
.
这是我天真的尝试:
template<typename Object>
concept formattable = requires(const Object & obj)
{
std::format("{}", obj);
};
Run Code Online (Sandbox Code Playgroud)
但这是行不通的。它基本上对所有类型都返回 true。即使那些不能与 std::format 一起使用的。
static_assert(!formattable<std::vector<int>>); // should return false
为什么不起作用?
我通常使用catch2的工作流程是拥有一个包含所有测试用例和测试“运行程序”的控制台应用程序。
例如:
file1.cpp、file2.cpp 包含测试:
#include <catch2/catch.hpp>
TEST_CASE("test", "[test]")
{
CHECK(true);
}
Run Code Online (Sandbox Code Playgroud)
单个文件 main.cpp 将包含运行程序代码:
#define CATCH_CONFIG_RUNNER
#include <catch2/catch.hpp>
int main(int argc, char* argv[])
{
return Catch::Session().run(argc, argv); // run tests
}
Run Code Online (Sandbox Code Playgroud)
为了减少编译时间,我尝试将包含测试的所有文件(file1、file2 等)移动到单独的静态库项目(Visual Studio)。
但这样做之后,catch 找不到任何测试用例:
catch main started..
Filters: [test]
===============================================================================
No tests ran
Run Code Online (Sandbox Code Playgroud)
我尝试将运行程序代码放入静态库中的函数中,但这没有帮助。
问题:
以下代码片段无法在最新版本的 MSVC (Visual Studio 2022 17.2.2) 上编译。相同的代码片段似乎在以前的编译器版本上运行得很好。
#include <iostream>
#include <format>
template <typename First, typename... Args>
inline auto format1(First&& first, Args&&... args) -> decltype(std::format(first, std::forward<Args>(args)...))
{
return std::format(std::forward<First>(first), std::forward<Args>(args)...);
}
int main()
{
std::cout << format1("hi {} {}", 123, 456);
}
Run Code Online (Sandbox Code Playgroud)
编译器发出以下错误:
1>ConsoleApplication3.cpp(10,24): message : 失败是由于在其生命周期之外读取变量引起的 1>ConsoleApplication3.cpp(10,24): message : 请参阅“first”的用法 1>ConsoleApplication3.cpp( 14): message : 请参阅正在编译的函数模板实例化 'std::string format<const char(&)[9],int,int>(First,int &&,int &&)' 的引用 1>
with 1> [ 1 > 首先=const char (&)[9] 1> ]
似乎以某种方式将字符串文字转发到 std::format 会使编译器认为它们在其生命周期之外使用。我尝试更改该函数以接受const First& first
以及各种其他变体,但错误仍然存在。
据我了解,当 …
vcpkg在Linux上默认使用GCC安装包。我看到官方文档提到了自定义三元组方法,但没有提到clang工具链。
如何指定clang和libc++作为vcpkg的默认工具链?
我有一个 2D 点的 2D numpy 数组:
np.random.seed(0)
a = np.random.rand(3, 4, 2) # each value is a 2D point
Run Code Online (Sandbox Code Playgroud)
我想按每个点的范数对每一行进行排序
norms = np.linalg.norm(a, axis=2) # shape(3, 4)
indices = np.argsort(norms, axis=0) # indices of each sorted row
Run Code Online (Sandbox Code Playgroud)
现在我想创建一个与 具有相同形状和值的数组a
。每行 2D 点将按其范数排序。
我怎样才能做到这一点?
我尝试了 np.take 和 np.take_along_axis 的变体,但没有成功。
例如:
np.take(a, indices, axis=1) # shape (3,3,4,2)
Run Code Online (Sandbox Code Playgroud)
此采样a
3 次,每行一次indices
。我只想取样a
一次。中的每一行都有indices
应从相应行中采样的列。
我正在尝试使用 C++ 概念来编写一个类型特征,该特征将根据其模板参数是否为基本类型而生成不同的类型:
template<typename T>
concept fundamental = std::is_fundamental_v<T>;
template<typename T>
concept non_fundamental = !std::is_fundamental_v<T>;
Run Code Online (Sandbox Code Playgroud)
以下代码按预期工作:
void Print(fundamental auto value)
{
std::cout << "fundamental\n";
}
void Print(non_fundamental auto value)
{
std::cout << "non fundamental\n";
}
int main()
{
Print(1); // prints "fundamental"
Print(std::string("str")); // prints "non fundamental"
}
Run Code Online (Sandbox Code Playgroud)
对类型特征应用相同的想法是行不通的。
template<fundamental T>
struct SomeTypeTrait
{
using type = T;
};
template<non_fundamental T>
struct SomeTypeTrait
{
using type = std::shared_ptr<T>;
};
using ExpectedToBeDouble = SomeTypeTrait<double>::type;
using ExpectedToBeSharedPtrOfString = SomeTypeTrait<std::string>::type; // fails to …
Run Code Online (Sandbox Code Playgroud) 我有一个使用 vcpkg 来管理其依赖项的 cmake 项目。vcpkg 用于“清单模式”。这意味着我的依赖项是在项目根目录中的 vcpkg.json 中指定的:
{
"name": "myproject",
"version-string": "1.0.0",
"builtin-baseline": "232704deb708fc866905af170b63c1a9cb821dbc",
"dependencies": [
{
"name" : "imgui",
"default-features": true,
"features" : ["docking-experimental"]
},
"magnum",
{
"name" : "magnum-integration",
"default-features": false,
"features" : ["imgui"]
}
]
}
Run Code Online (Sandbox Code Playgroud)
该"builtin-baseline"
字段包含 git SHA-1,标识我自己私人维护的 vcpkg 存储库中的提交。
例如,magnum
依赖项配置为使用最新的“基线”版本。这意味着如果您转到 vcpkg 的安装位置,则会有一个文件 versions/baseline.json,其中确定了基线。
vcpkg 有一个(复杂且不直观的)机制来将某些依赖项固定到旧版本。但是,我找不到如何修改 vcpkg 安装的结构化方法,因此它将从 git 存储库安装不同的版本。vcpkg“覆盖端口”功能在清单模式下不起作用。
理想情况下,vcpkg 允许我做一些简单的事情,例如:
"magnum",
{
"git-commit" : "dagfaghsfdg",
"name" : "magnum-integration",
"default-features": false,
"features" : ["imgui"]
}
Run Code Online (Sandbox Code Playgroud)
那么如何配置 vcpkg 以使用某个依赖项的 git 提交(在清单模式下)?
c++ ×6
c++20 ×3
c++-concepts ×2
python ×2
vcpkg ×2
visual-c++ ×2
arrays ×1
bash ×1
catch2 ×1
ccache ×1
clang ×1
cmake ×1
libc++ ×1
numpy ×1
opencv ×1
powershell ×1
templates ×1
type-traits ×1
unit-testing ×1
usb ×1
windows ×1
windows-subsystem-for-linux ×1
wsl-2 ×1