Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.8k views
in Technique[技术] by (71.8m points)

Rust 条件编译问题

我在写程序(纯Rust命令行应用)时,使用了Rust crate winreg-rs

对 windows 注册表有些写入操作并放在一个函数中

并有一个对 linux 上同一个的函数

extern crate winreg;
use winreg::enums::*;
use winreg::RegKey;


extern crate serde_json;
use serde_json::Value;
use std::fs::File;

fn main()  {
    chrome_native_config();
   }

#[allow(dead_code)]
#[cfg(target_os="windows")]
fn chrome_native_config(){
    let hklm = RegKey::predef(HKEY_CURRENT_USER);
    let (key,disp) = hklm.create_subkey("SOFTWARE\Google\Chrome\NativeMessagingHosts\chrome_nativeMessaging").unwrap();

    match disp {
        REG_CREATED_NEW_KEY => println!("A new key has been created"),
        REG_OPENED_EXISTING_KEY => println!("An existing key has been opened"),
    }
    key.set_value("", &"D:\threeday\chrome_nativeMessaging.json").unwrap();
    let value:String = key.get_value("").unwrap();
    if value != "D:\threeday\chrome_nativeMessaging.json".to_string() {
        key.set_value("", &"D:\threeday\chrome_nativeMessaging.json").unwrap();
    }
    let f = File::create("D:\threeday\chrome_nativeMessaging.json").unwrap();
    let config_str=r#"
    {
    "name":"com.chrome.nativemessaging",
    "description":"Chrome native messageing api example",
    "path":"D:\threeday\TestWeb.exe",
    "type":"stdio",
    "allowed_origins":[
        "chrome-extension://hgibimofjkchfnpmfhnigfhkkkahlmah/"
    ]
    }"#;
    let v:Value =serde_json::from_str(config_str).unwrap();
    serde_json::to_writer(&f, &v).expect("write json failed");
}

#[cfg(target_os="linux")]
fn chrome_native_config(){
    let f = File::create("~/.config/google-chrome/NativeMessingHosts/chrome_nativeMessaging.json").unwrap();
    let config_str=r#"
    {
    "name":"chrome_nativeMessaging",
    "description":"Chrome native messageing api example",
    "path":"~/.config/google-chrome/NativeMessingHosts/TestWeb",
    "type":"stdio",
    "allowed_origins":[
        "chrome-extension://fkdghlklbgmkokmgnoanmkedekgkckkp/"
    ]
    }"#;
    let v:Value =serde_json::from_str(config_str).unwrap();
    serde_json::to_writer(&f, &v).expect("write json failed");
}

但在 linux cargo build上的编译是
image.png

请问这个 Rust 条件编译怎么处理


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)


#[cfg(target_os="windows")]
extern crate winreg;
#[cfg(target_os="windows")]
use winreg::enums::*;
#[cfg(target_os="windows")]
use winreg::RegKey;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...