我正试图在Windows上针对Rust库链接一个简单的C lib
我的lib是.h
extern "C" {
void say_hello(const char* s);
}
Run Code Online (Sandbox Code Playgroud)
的.cpp
#include <stdio.h>
void say_hello(const char* s) {
printf("hello world");
}
Run Code Online (Sandbox Code Playgroud)
我的Rust文件
#[link(name="CDbax", kind="static")]
extern "C" {
fn say_hello(s: *const libc::c_char) -> () ;
}
Run Code Online (Sandbox Code Playgroud)
通过给出其中一个数据符号的错误来链接失败
error: linking with `gcc` failed: exit code: 1
note: "gcc" "-Wl,--enable-long-section-names" "-fno-use-linker-plugin" "-Wl,--nxcompat" "-Wl,--large-address-aware" "-shared-libgcc" "-L" "C:\Program Files (x86)\Rust 1.2\bin\rustlib\i686-pc-windows-gnu\lib" "e:\Rust\DBTools\DBAnalytics\target\debug\DBAnalytics.o" "-o" "e:\Rust\DBTools\DBAnalytics\target\debug\DBAnalytics.dll" "e:\Rust\DBTools\DBAnalytics\target\debug\DBAnalytics.metadata.o" "C:\Program Files (x86)\Rust 1.2\bin\rustlib\i686-pc-windows-gnu\lib\libstd-11582ce5.rlib" "C:\Program Files (x86)\Rust 1.2\bin\rustlib\i686-pc-windows-gnu\lib\libcollections-11582ce5.rlib" "C:\Program Files (x86)\Rust 1.2\bin\rustlib\i686-pc-windows-gnu\lib\librustc_unicode-11582ce5.rlib" "C:\Program Files (x86)\Rust 1.2\bin\rustlib\i686-pc-windows-gnu\lib\librand-11582ce5.rlib" "C:\Program Files (x86)\Rust …Run Code Online (Sandbox Code Playgroud) 我不明白为什么这种类型的结构会出现错误
enum Cell <'a> {
Str(&'a str),
Double(&'a f32),
}
struct MyCellRep<'a> {
value: &'a Cell,
ptr: *const u8,
}
impl MyCellRep{
fn new_from_str(s: &str) {
MyCellRep { value: Cell::Str(&s), ptr: new_sCell(CString::new(&s)) }
}
fn new_from_double(d: &f32) {
MyCellRep { value: Cell::Double(&d), ptr: new_dCell(&d) }
}
}
Run Code Online (Sandbox Code Playgroud)
我收到错误
14:22 error: wrong number of lifetime parameters: expected 1, found 0 [E0107]
src\lib.rs:14 value : & 'a Cell ,
Run Code Online (Sandbox Code Playgroud)
所以我也尝试过
struct MyCellRep<'a> {
value: &'a Cell + 'a,
ptr: *const u8,
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用fmt :: Display打印枚举(或结构).虽然代码编译并获取显示方法,但它不会打印该值.
pub enum TestEnum<'a> {
Foo(&'a str),
Bar(f32)
}
impl<'b> fmt::Display for TestEnum <'b> {
fn fmt(&self, f : &mut fmt::Formatter) -> fmt::Result {
println!("Got this far");
match self{
&TestEnum::Foo(x) => write!(f,"{}",x),
&TestEnum::Bar(x) => write!(f,"{}",x),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_print() {
let cell = TestEnum::Str("foo");
println!("Printing");
println!("{}",cell); // No output here
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用{:?}和{}但无济于事.
这有效:
let fut = Arc::new(Mutex::new(Box::pin(async { 1 })));
let mut conn_futures = BTreeMap::new(); // implicitly typed
conn_futures.insert(123, fut);
if let Some(fut) = conn_futures.get_mut(&123) {
let fut = fut.clone();
self.pool.spawn(async move {
let mut fut = fut.try_lock().unwrap();
(&mut *fut).await;
});
};
Run Code Online (Sandbox Code Playgroud)
我如何在结构中写同样的东西;是什么类型的conn_futures?根据编译器的说法,它是BTreeMap<i32, impl Future>,但无法将其写入结构中:
struct Foo {
conn_futures: BTreeMap<i32, impl Future>, // impl not allow in this position
}
Run Code Online (Sandbox Code Playgroud)
我试过这个:
use futures::{executor::LocalPool, lock::Mutex, task::SpawnExt, Future}; // 0.3.1
use std::{collections::BTreeMap, pin::Pin, sync::Arc};
struct Foo {
conn_futures: BTreeMap<i32, Arc<Mutex<Pin<Box<dyn …Run Code Online (Sandbox Code Playgroud) 我的Rust测试代码
extern "C" {
fn test_int_only(n : libc::c_int);
fn test_int_and_str(s : CString , n : libc::c_int);
}
pub fn test1() {
unsafe {
test_int_only(0);
test_int_only(1);
test_int_only(2);
test_int_only(4);
test_int_only(-12);
}
}
pub fn test2() {
unsafe {
test_int_and_str(CString::new("Foo").unwrap(),0);
test_int_and_str(CString::new("Bar").unwrap(),1);
test_int_and_str(CString::new("Baz").unwrap(),2);
test_int_and_str(CString::new("Fub").unwrap(),4);
test_int_and_str(CString::new("Bub").unwrap(),-12);
}
}
Run Code Online (Sandbox Code Playgroud)
我的C代码
void test_int_only(int abc){
printf("%d\n", abc);
}
void test_int_and_str(const char* name,int abc) {
printf("%s %d\n", name, abc);
}
Run Code Online (Sandbox Code Playgroud)
测试test_int_only()时
1
2
4
-12
Run Code Online (Sandbox Code Playgroud)
测试test_int_and_str()时
Foo 4
Bar 4
Baz 4
Fub 4
Bub 4
Run Code Online (Sandbox Code Playgroud)
似乎第二个arg被解释为(在rust或c中)作为sizeof字符串,而不是从Rust代码传递的值.我猜它与调用约定或空终止无法正常工作有关.它是一个C dll,带有_cdecl(windows 32bit …
fn lines_from_file<F>(filename: F) -> Result<io::Lines<BufReader<File>>, io::Error>
where
F: std::convert::AsRef<std::path::Path>,
{
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}
fn main() {
let filename: &str = "input.pdl";
// This works fine
match lines_from_file(filename) {
Ok(lines) => {
for line in lines {
println!("{:?}", line);
},
}
Err(e) => println!("Error {:?}", e),
}
}
Run Code Online (Sandbox Code Playgroud)
我想改用它:
if let lines = Ok(lines_from_file(filename)) {
for line in lines {
println!("{:?}", line);
}
} else {
println!("Error {:?}" /*what goes here?*/,)
}
Run Code Online (Sandbox Code Playgroud)
但这给出了一个错误:
| if let lines = …Run Code Online (Sandbox Code Playgroud) 这似乎部分工作,但我无法获得要打印的字符串值
pub fn test() {
let mut buf: Vec<u16> = vec![0; 64];
let mut sz: DWORD = 0;
unsafe {
advapi32::GetUserNameW(buf.as_mut_ptr(), &mut sz);
}
let str1 = OsString::from_wide(&buf).into_string().unwrap();
println!("Here: {} {}", sz, str1);
}
Run Code Online (Sandbox Code Playgroud)
打印:
Here: 10
Run Code Online (Sandbox Code Playgroud)
当我希望它也打印
Here: 10 <username>
Run Code Online (Sandbox Code Playgroud)
作为测试,C版
TCHAR buf[100];
DWORD sz;
GetUserName(buf, &sz);
Run Code Online (Sandbox Code Playgroud)
似乎buf很好.
有没有办法在C++ 14中为循环编写声明式样式
for(int i = 0; i < 10; i+=2) {
// ... some code
}
Run Code Online (Sandbox Code Playgroud)
我发现最接近的是使用boost
for(auto i : irange(1,10,2)){
// .... some code
}
Run Code Online (Sandbox Code Playgroud)
是否有c ++ 14/17标准方法可以达到同样的效果?
我试图将std :: make_integer_sequence()作为一个可能的起点,但无法弄明白.