如何判断CPU 架构和操作系统类型
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>

int main()
{
#if defined __linux__
std::cout<<"linux system"<<std::endl;
#elif defined _WIN32
std::cout<<"windows system"<<std::endl;
#endif

#if defined __aarch64__
std::cout<<"this is arm cpu"<<std::endl;
#elif defined __x86_64__
std::cout<<"this id x86 cpu"<<std::endl;
#endif
return 0;
}

cmake 中判断CPU 架构,操作系统类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cmake_minimum_required(VERSION 3.10.0)

message(${CMAKE_HOST_SYSTEM_NAME})
message(${CMAKE_HOST_SYSTEM_PROCESSOR})

if(CMAKE_HOST_SYSTEM_NAME MATCHES "Linux")
message("this is Linux")
elseif(CMAKE_HOST_SYSTEM_NAME MATCHES "Windows")
message("this is Windows")
endif()
if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "aarch64")
message("this is aarch64 cpu")
elseif(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "x86_64")
message("this is x86_64 cpu")
endif()