判断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()

根据编译器的情况,有下面的宏用于调试程序:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//判断是不是C++环境,需要注意的是ROS环境中这里为否
#ifdef _cplusplus
printf("C++\n");
#endif
//判断是不是C环境
#ifdef __STDC__
printf("C\n");
#endif
//输出语句所在函数,行,文件等参数
printf("function %s: Line 25\n",__func__); //或者用__FUNCTION__
printf("pretty function %s: Line 25\n",__PRETTY_FUNCTION__); //函数声明,包括了参数
printf("line: %d\n",__LINE__);
printf("current file: %s\n",__FILE__);
printf("date: %s\n",__DATE__);
printf("time: %s\n",__TIME__);