如何通过Fortran获取操作系统版本、CPU架构及核心数等系统信息?
嘿,这个问题我刚好研究过!用Fortran获取这些系统信息确实需要分平台处理,毕竟不同操作系统的API和命令差异不小。下面我给你拆解每个需求的实现方式,代码都是可直接运行的:
1. 操作系统名称(预处理指令快速实现)
Fortran的主流编译器(GCC、Intel Fortran等)都支持预处理宏,可以直接识别常见操作系统,这是最省心的方式:
program get_os_name implicit none #ifdef _WIN32 print *, "Operating System: Windows" #elif __APPLE__ print *, "Operating System: macOS" #elif __linux__ print *, "Operating System: Linux" #else print *, "Operating System: Unknown" #endif end program get_os_name
编译时编译器会自动替换对应宏,直接输出系统名称。
2. 操作系统版本
这个就得调用系统API或者执行系统命令了,分Windows和Unix-like(Linux/macOS)两种情况:
Windows平台(调用WinAPI)
用GetVersionExW函数获取详细版本信息:
program get_win_version use, intrinsic :: iso_c_binding implicit none type, bind(c) :: OSVERSIONINFOEX integer(c_ulong) :: dwOSVersionInfoSize integer(c_ulong) :: dwMajorVersion integer(c_ulong) :: dwMinorVersion integer(c_ulong) :: dwBuildNumber integer(c_ulong) :: dwPlatformId character(c_char) :: szCSDVersion(128) integer(c_ushort) :: wServicePackMajor integer(c_ushort) :: wServicePackMinor integer(c_ushort) :: wSuiteMask integer(c_byte) :: wProductType integer(c_byte) :: wReserved end type OSVERSIONINFOEX interface integer(c_long) function GetVersionExW(osvi) bind(c, name='GetVersionExW') import :: OSVERSIONINFOEX type(OSVERSIONINFOEX), intent(inout) :: osvi end function GetVersionExW end interface type(OSVERSIONINFOEX) :: osvi integer(c_long) :: result osvi%dwOSVersionInfoSize = sizeof(osvi) result = GetVersionExW(osvi) if (result /= 0) then print *, "Windows Version: ", osvi%dwMajorVersion, ".", osvi%dwMinorVersion, " Build ", osvi%dwBuildNumber print *, "Service Pack: ", osvi%wServicePackMajor, ".", osvi%wServicePackMinor else print *, "Failed to get Windows version" end if end program get_win_version
Linux/macOS平台(执行系统命令)
通过execute_command_line调用系统命令获取版本:
program get_unix_version implicit none character(len=256) :: version_output integer :: iostat #ifdef __linux__ ! Linux用lsb_release显示发行版名称和版本 call execute_command_line("lsb_release -d | cut -f2", output=version_output, exitstat=iostat, wait=.true.) #elif __APPLE__ ! macOS用sw_vers获取产品版本 call execute_command_line("sw_vers -productVersion", output=version_output, exitstat=iostat, wait=.true.) #endif if (iostat == 0) then print *, "OS Version: ", trim(version_output) else print *, "Failed to get OS version" end if end program get_unix_version
3. CPU架构(32/64-bit)
两种方式:预处理指令识别,或者调用系统命令/API:
预处理指令方式(简单直接)
program get_cpu_arch implicit none #ifdef _WIN64 print *, "CPU Architecture: 64-bit (x86_64)" #elif _WIN32 print *, "CPU Architecture: 32-bit (x86)" #elif __x86_64__ print *, "CPU Architecture: 64-bit (x86_64)" #elif __i386__ print *, "CPU Architecture: 32-bit (x86)" #elif __aarch64__ print *, "CPU Architecture: 64-bit (ARM64)" #elif __arm__ print *, "CPU Architecture: 32-bit (ARM)" #else print *, "CPU Architecture: Unknown" #endif end program get_cpu_arch
系统命令/API方式(更准确)
Windows用GetSystemInfo,Linux/macOS用uname -m,逻辑和前面的版本获取类似,这里就不重复写了。
4. CPU核心数
同样分平台实现:
Windows平台(WinAPI)
用GetSystemInfo获取处理器数量:
program get_win_cores use, intrinsic :: iso_c_binding implicit none type, bind(c) :: SYSTEM_INFO integer(c_ushort) :: wProcessorArchitecture integer(c_ushort) :: wReserved integer(c_ulong) :: dwPageSize type(c_ptr) :: lpMinimumApplicationAddress type(c_ptr) :: lpMaximumApplicationAddress type(c_ptr) :: dwActiveProcessorMask integer(c_ulong) :: dwNumberOfProcessors integer(c_ulong) :: dwProcessorType integer(c_ulong) :: dwAllocationGranularity integer(c_ushort) :: wProcessorLevel integer(c_ushort) :: wProcessorRevision end type SYSTEM_INFO interface subroutine GetSystemInfo(si) bind(c, name='GetSystemInfo') import :: SYSTEM_INFO type(SYSTEM_INFO), intent(out) :: si end subroutine GetSystemInfo end interface type(SYSTEM_INFO) :: si call GetSystemInfo(si) print *, "Number of CPU Cores: ", si%dwNumberOfProcessors end program get_win_cores
Linux/macOS平台(sysconf或系统命令)
优先调用sysconf函数,失败时fallback到系统命令:
program get_unix_cores use, intrinsic :: iso_c_binding implicit none interface function sysconf(name) bind(c, name='sysconf') import :: c_int, c_long integer(c_int), intent(in) :: name integer(c_long) :: sysconf end function sysconf end interface integer(c_int), parameter :: _SC_NPROCESSORS_ONLN = 84 ! 通用参数 integer(c_long) :: core_count character(len=256) :: output integer :: iostat core_count = sysconf(_SC_NPROCESSORS_ONLN) if (core_count > 0) then print *, "Number of CPU Cores: ", core_count else ! 调用nproc命令(Linux)或sysctl(macOS) #ifdef __linux__ call execute_command_line("nproc", output=output, exitstat=iostat) #elif __APPLE__ call execute_command_line("sysctl -n hw.ncpu", output=output, exitstat=iostat) #endif if (iostat == 0) then read(output, *) core_count print *, "Number of CPU Cores: ", core_count else print *, "Failed to get CPU core count" end if end if end program get_unix_cores
最后提醒一句:如果要写跨平台的代码,可以把这些逻辑用预处理指令整合到一个程序里,编译时会自动适配目标系统。
内容的提问来源于stack exchange,提问作者Daniel R. Livingston
相关产品推荐
相关产品推荐

