cmake跨平台编译之判断操作系统平台、32位64位系统_谁吃薄荷糖的博客-CSDN博客

  1. if(CMAKE_CROSSCOMPILING)
  2. message(STATUS "Cross Comliling!!!, ARM")
  3. set(CURRENT_SYSTEM "Arm")
  4. else()
  5. message(STATUS "No Cross Comliling!!!")
  6. endif()
  1. if(CMAKE_CL_64)
  2. set(CURRENT_PLATFORM "x64")
  3. message(STATUS "Current Platform is ${CURRENT_PLATFORM}")
  4. else(CMAKE_CL_64)
  5. set(CURRENT_PLATFORM "x86")
  6. message(STATUS "Current Platform is ${CURRENT_PLATFORM}")
  7. endif(CMAKE_CL_64)
  1. if(CMAKE_SYSTEM_NAME MATCHES "Linux")
  2. set(CURRENT_SYSTEM "Linux")
  3. elseif(CMAKE_SYSTEM_NAME MATCHES "Windows")
  4. set(CURRENT_SYSTEM "Windows")
  5. elseif(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
  6. set(CURRENT_SYSTEM "FreeBSD")
  7. endif()

CMakeLists.txt文件节选:

  1. message(STATUS "===============================")
  2. MESSAGE(STATUS "current operation system is ${CMAKE_SYSTEM}")
  3. message(STATUS "current operation system name is ${CMAKE_SYSTEM_NAME}")
  4. if(CMAKE_CL_64)
  5. set(CURRENT_PLATFORM "x64")
  6. message(STATUS "Current Platform is ${CURRENT_PLATFORM}")
  7. else(CMAKE_CL_64)
  8. set(CURRENT_PLATFORM "x86")
  9. message(STATUS "Current Platform is ${CURRENT_PLATFORM}")
  10. endif(CMAKE_CL_64)
  11. if(CMAKE_CROSSCOMPILING)
  12. message(STATUS "Cross Comliling!!!, ARM")
  13. set(CURRENT_SYSTEM "Arm")
  14. else()
  15. message(STATUS "No Cross Comliling!!!")
  16. #根据不同平台给CURRENT_SYSTEM命名
  17. if(CMAKE_SYSTEM_NAME MATCHES "Linux")
  18. set(CURRENT_SYSTEM "Linux")
  19. elseif(CMAKE_SYSTEM_NAME MATCHES "Windows")
  20. set(CURRENT_SYSTEM "Windows")
  21. elseif(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
  22. set(CURRENT_SYSTEM "FreeBSD")
  23. endif()
  24. endif()

cmake打印信息(Ubuntu20系统x86测试测试):

  1. root@root:/home/root123/testdemo/demo1/build
  2. -- ===============================
  3. -- current operation system is Linux-5.11.0-41-generic
  4. -- current operation system name is Linux
  5. -- Current Platform is x86
  6. -- No Cross Comliling!!!
  7. -- ===============================
  8. -- /home/root123/testdemo/demo1/Lib/Debug/Linux/x86
  9. -- ===============================