Reputation:
A boolean determining if it's 64-bit is perfect but an integer representing the amount of bits would also be fine.
I want to capture some information about the PC's architecture for statistics purposes.
Upvotes: 2
Views: 2146
Reputation: 1
You can use this:
#[cfg(target_os="your operating system here")]
// code here
This literally just checks if the operating system the program was compiled on is the recommended one. Then I guess you can do this to check the bits of the operating system:
#[cfg(all(unix, target_pointer_width = "32"))]
// code here
Where unix is the operating system (just a placeholder, should support Windows etc.), and 32 is the OS bitness.
Upvotes: -1
Reputation: 89006
In the best case your program is already compiled for the correct architecture/target. This means that you already know at compile time whether or not the program is being compiled for a 32bit or 64bit target. You can check that by using the cfg()
attribute or the cfg!()
macro:
fn is_compiled_for_64_bit() -> bool {
cfg!(target_pointer_width = "64")
}
#[cfg(target_pointer_width = "32")]
fn foo() {
println!("foo compiled for 32 bit");
}
#[cfg(target_pointer_width = "64")]
fn foo() {
println!("foo compiled for 64 bit");
}
But in case you want to ship only 32-bit binaries to your users, your program is then executed either natively by the user's 32 bit hardware or in compatibility mode by the user's 64 bit hardware. To find our whether your program actually runs on a 32 bit architecture or just in a 32 bit compatibility mode is more difficult and depends on your operating system. I'm not aware of any easy cross platform way to do that. I would advise you to compile separately for each architecture you're targeting anyway.
Upvotes: 1