rust中的條件編譯


1、示例:對不同的操作系統調用不同的代碼。

use libc;

#[cfg(target_os="linux")]
fn my_print(){
    unsafe{
        libc::printf("hello".as_ptr() as *const libc::c_char);
    }
}
#[cfg(target_os="windows")]
fn my_print(){
    unsafe{
        libc::puts("hello".as_ptr() as *const libc::c_char); 
    }
}

fn main() {
     my_print();
}

2、cfg宏

    if cfg!(target_os="windows"){
        println!("windows");
    }
    else{
        println!("not windows")
    }

if與else代碼分支的都會編譯。以下代碼無法通過編譯,因為在windows下生成時,libc中沒有printf函數:

    if cfg!(target_os="windows"){
        libc::printf("hello".as_ptr() as *const libc::c_char);
    }
    else{
        libc::puts("hello".as_ptr() as *const libc::c_char); 
    }

 

更多的內容可參考:https://www.cntofu.com/book/55/content/Conditional%20Compilation%20%E6%9D%A1%E4%BB%B6%E7%BC%96%E8%AF%91.md

Rust有一個特殊的屬性,#[cfg],它允許你基於一個傳遞給編譯器的標記編譯代碼。它有兩種形式:

#[cfg(foo)]
# fn foo() {}

#[cfg(bar = "baz")]
# fn bar() {}

它還有一些幫助選項:

#[cfg(any(unix, windows))]
# fn foo() {}

#[cfg(all(unix, target_pointer_width = "32"))]
# fn bar() {}

#[cfg(not(foo))]
# fn not_foo() {}

這些選項可以任意嵌套:

#[cfg(any(not(unix), all(target_os="macos", target_arch = "powerpc")))]
# fn foo() {}

至於如何啟用和禁用這些開關,如果你使用Cargo的話,它們可以在你Cargo.toml中的[features]部分設置:

[features] # no features by default default = [] # The “secure-password” feature depends on the bcrypt package. secure-password = ["bcrypt"] 

當你這么做的時候,Cargo傳遞給rustc一個標記:

--cfg feature="${feature_name}" 

這些cfg標記集合會決定哪些功能被啟用,並且因此,哪些代碼會被編譯。讓我們看看這些代碼:

#[cfg(feature = "foo")]
mod foo {
}

如果你用cargo build --features "foo"編譯,他會向rustc傳遞--cfg feature="foo"標記,並且輸出中將會包含mod foo。如果我們使用常規的cargo build編譯,則不會傳遞額外的標記,因此,(輸出)不會存在foo模塊。

cfg_attr

你也可以通過一個基於cfg變量的cfg_attr來設置另一個屬性:

#[cfg_attr(a, b)]
# fn foo() {}

如果a通過cfg屬性設置了的話這與#[b]相同,否則不起作用。

cfg!

cfg![語法擴展](Compiler Plugins 編譯器插件.md)也讓你可以在你的代碼中使用這類標記:

if cfg!(target_os = "macos") || cfg!(target_os = "ios") {
    println!("Think Different!");
}

這會在編譯時被替換為一個truefalse,依配置設定而定。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM