如何在跨平台场景下通过Vala语言获取系统位数?
Hey there! Let's walk through how to determine system bitness in Vala, whether you're targeting a single platform or building cross-platform applications. We'll cover both compile-time checks and runtime detection, plus a unified cross-platform approach.
If you're building for a specific target architecture and don't need runtime flexibility, Vala lets you use compiler-defined preprocessor macros to check bitness at compile time. This is efficient since the check is resolved before your program runs.
void main() { #if X86_64 stdout.printf("Running on a 64-bit x86 system\n"); #elif ARM64 stdout.printf("Running on a 64-bit ARM system\n"); #elif I386 stdout.printf("Running on a 32-bit x86 system\n"); #elif ARM stdout.printf("Running on a 32-bit ARM system\n"); #else stdout.printf("Unknown architecture\n"); #endif }
- These macros are automatically set by the Vala compiler (gcc/clang) based on the target architecture.
- This method works best when you're compiling for a known platform and don't need to handle cross-environment scenarios (like 32-bit apps running on 64-bit systems).
If you need to detect the system's bitness at runtime (e.g., to handle 32-bit apps running on 64-bit hosts), you'll need to use platform-specific APIs wrapped in Vala.
2.1 Linux/BSD/Unix-like Systems
On Unix-like systems, you can use the sysconf function from the POSIX library to query the system's word size, or parse the output of uname to get the machine architecture.
Using sysconf (Process Bitness)
using Posix; public static string get_unix_process_bitness() { // _SC_LONG_BIT returns the size of a long integer in bits (matches process bitness) long bit_count = sysconf(_SC_LONG_BIT); return $"{bit_count}-bit"; } void main() { stdout.printf("Running process bitness: %s\n", get_unix_process_bitness()); }
- Compile with:
valac your-file.vala --pkg posix - Note: This returns the bitness of the running process, not the underlying system. If you're running a 32-bit app on a 64-bit system, it will return "32-bit".
Using uname (Underlying System Architecture)
To get the actual system architecture (not the process's), parse the machine field from uname:
using Posix; public static string get_unix_system_arch() { Utsname system_info; if (uname(out system_info) == 0) { string machine = system_info.machine; if (machine.contains("64")) { return "64-bit"; } else { return "32-bit"; } } return "Unknown"; } void main() { stdout.printf("System architecture: %s\n", get_unix_system_arch()); }
2.2 Windows Systems
On Windows, use the Win32 API functions GetSystemInfo (process bitness) or GetNativeSystemInfo (actual system bitness) to query the architecture.
First, define the necessary struct and external functions:
[CCode(cname = "SYSTEM_INFO", cprefix = "", has_type_id = false)] struct SystemInfo { public ushort wProcessorArchitecture; public ushort wReserved; public uint dwPageSize; public void* lpMinimumApplicationAddress; public void* lpMaximumApplicationAddress; public ulong dwActiveProcessorMask; public uint dwNumberOfProcessors; public uint dwProcessorType; public uint dwAllocationGranularity; public ushort wProcessorLevel; public ushort wProcessorRevision; } [CCode(cname = "GetNativeSystemInfo", library = "kernel32")] private extern static void get_native_system_info(out SystemInfo lpSystemInfo); public static string get_windows_system_bitness() { SystemInfo info; get_native_system_info(out info); switch (info.wProcessorArchitecture) { case 0: // PROCESSOR_ARCHITECTURE_INTEL (32-bit x86) return "32-bit"; case 9: // PROCESSOR_ARCHITECTURE_AMD64 (64-bit x86) case 12: // PROCESSOR_ARCHITECTURE_ARM64 (64-bit ARM) return "64-bit"; default: return "Unknown"; } } void main() { stdout.printf("System bitness: %s\n", get_windows_system_bitness()); }
GetNativeSystemInforeturns the underlying system's architecture, even if your app is running in 32-bit compatibility mode.- Compile with:
valac your-file.vala(Vala automatically links kernel32 on Windows targets)
Wrap the platform-specific logic into a single function using Vala's conditional compilation to make it cross-platform:
using Posix; // Include the Windows struct/function definitions here public static string get_cross_platform_bitness() { #if WINDOWS return get_windows_system_bitness(); #else return get_unix_system_arch(); #endif } void main() { stdout.printf("Cross-platform system bitness: %s\n", get_cross_platform_bitness()); }
- The
WINDOWSmacro is automatically defined when compiling for Windows targets, so the function will use the correct implementation based on the platform.
内容的提问来源于stack exchange,提问作者MrSyabro

