- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在使用需要 int argc, char **argv
的 C API(特别是 MPI_Init
)。我正在尝试使用以下代码生成等效的 argc, argv
:
let argc = std::env::args().len() as c_int;
let c_strs: ~[CString] = std::env:args().map(|s: & &str| s.to_c_str());
let mut argv: ~[*c_char] = c_strs.map(|c: &CString| c.with_ref(|ptr| ptr));
if null_terminate {
argv.push(std::ptr::null());
}
通过改编this discussion on Github .
它失败了:
error: expected type, found `~`
src/lib.rs:37 let c_strs: ~[CString] = std::env::args().map(|s: & &str| s.to_c_str());
^
我摆脱了 ~
然后它找不到 to_c_str()
并且不确定用什么替换 to_c_str
to_raw()
(例如)失败。
有谁知道将 Args
转换为对 C 更友好的格式的方法吗?
最佳答案
我的答案适用于当前稳定的 Rust (1.5),并且可能适用于 beta 和 nightly。
下面的 Rust 代码调用了用 C 实现的 foo(argc, argv)
函数。foo
的签名非常类似于 main
功能。
extern crate libc;
use libc::c_char;
use libc::c_int;
use std::ffi::CString;
#[link(name="foo")]
extern "C" {
fn foo(argc: c_int, argv: *const *const c_char);
}
fn main() {
// create a vector of zero terminated strings
let args = std::env::args().map(|arg| CString::new(arg).unwrap() ).collect::<Vec<CString>>();
// convert the strings to raw pointers
let c_args = args.iter().map(|arg| arg.as_ptr()).collect::<Vec<*const c_char>>();
unsafe {
// pass the pointer of the vector's internal buffer to a C function
foo(c_args.len() as c_int, c_args.as_ptr());
};
}
请注意,C 端只是借用 指向字符串的指针。如果要存储它们,请对它们使用 strdup()
。
我还在 CString
实例上使用了 unwrap()
。如果您的字符串包含 0 个字节,它将返回错误,请参阅 https://doc.rust-lang.org/stable/std/ffi/struct.CString.html#method.new .
证明:
我把这段代码放到了一个 cargo 项目中,并添加了 libc
作为依赖。foo()
函数如下所示:
#include <stdio.h>
void foo(int argc, char* argv[]) {
int i;
for (i = 0; i < argc; i++) {
printf("argv[%d]: %s\n", i, argv[i]);
}
}
我用以下代码编译了这段代码:
gcc foo.c -o libfoo.so -shared -fPIC
然后将 libfoo.so
复制到 target/debug/deps
下(只是为了在库搜索路径中)。然后我运行我的 cargo 项目:
$ cargo run the quick brown fox
Compiling args v0.1.0 (file:///home/tibi/Codes/Rust/argv/args)
Running `target/debug/args the quick brown fox`
argv[0]: target/debug/args
argv[1]: the
argv[2]: quick
argv[3]: brown
argv[4]: fox
关于c - 如何将 Rust `Args` 转换为 argc 和 argv C 等价物?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34379641/
我正在将 HP-Unix 下的大量 Oracle Pro*C 代码迁移到 Linux 环境。 程序中只定义了这样一个main方法: main _2a((argc,argv), int argc, ch
我正在使用库中的一个函数,该函数调用主程序中的 (argc,argv) 命令行函数直接传递给它;但是,我只想将一些参数传递给它,其余的在主代码中评估而不是函数。以下是带有评估参数的主要代码示例。 in
我在SO里游荡,看到了this question .然后我开始怀疑我是否可以溢出 argc。 标准规定 argv[argc] 必须是空指针,但如果 argc 溢出,则为 false。 (我 wrote
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
出于某种原因,如果我写“a+bi * c+di”而不是“a+bi + c+di”,我的 argc 参数从 4 更改为 6,但我不知道为什么。发生了什么事以及如何解决? 谢谢 #include #in
我有以下代码: void parse(char *commandLine) { int rc = 0; int argc = 0; char *cmdLine; cha
这是此问题的后续问题:Minimal example of Python interpreter embedding segfaults . 当我这样编译程序时: gcc -fno-diagnosti
根据this paper 和一些 stackoverflow 帖子,argc 在堆栈的顶部,argv 在它下面。我已经尝试了大约 3-4 种不同的方法: 将其弹出到初始化变量 (.data) - 通过
这个问题在这里已经有了答案: OpenCV argc and argv confusion (1 个回答) 关闭 9 年前。 我不明白,OpenCV 中用于加载图像的代码的功能是什么。 if(arg
我的教授和几个学生正在争论 argv 是否以 null 终止。我的 friend 写了一个小程序,它打印出 null 但另一个 child 说他可能只是在读入空白内存。有人可以解决这个讨论吗? 最佳答
所以我进行了 PHPUnit 测试,并在一个函数中找到了这段代码。 global $argv, $argc; echo $argc; print_r($argv); 我明白这些变量代表什么(从命令行传
我编写了一个打印输入的代码: int main(int argc, char *argv[]) { printf("%c", *argv[1]); return 0; } 当我输入
我将一个 main(int argc, char *argv[]) 函数重命名为普通子函数,例如 a(int argc, char *argv[]);并在我的 main() 中调用 a() 函数。然后
int main( const int argc , const char[] const argv) 如Effective C++ Item#3 指出“尽可能使用 const”,我开始思考“为什么不
我知道 argc 计算参数的数量,argv 接受参数,但它们是我可以确定我的参数是否来自 argv 以某种格式输入,如括号内和非括号内。比如./a.out "Hello Word"和./a.out H
我是 C++ 的新手,我正在尝试了解它的工作原理。所以我有下一个代码: int _tmain(int argc, _TCHAR* argv[]) { std::wcin.get();//for con
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
我正在尝试查找 C++ 程序中的段错误。我注意到在发生段错误时 argc 已被修改为非常大的数字。该数字恰好是我的应用程序地址空间中的一个地址。这让我相信有什么东西正在破坏我的主堆栈框架。问题是,如何
为什么会出现以下情况: #include int main(int argc, char *argv[]) { char array[3] = {'1', '2', '3'};
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我是一名优秀的程序员,十分优秀!