QT、c/c++通过宏自动判断平台

QT、c/c++通过宏自动判断平台

  • [Chapter1 QT、c/c++通过宏自动判断平台](#Chapter1 QT、c/c++通过宏自动判断平台)

Chapter1 QT、c/c++通过宏自动判断平台

原文链接:https://blog.csdn.net/qq_32348883/article/details/123063830

背景

为了更好的进行跨平台移植、编译、调试。

具体操作

宏操作

cpp 复制代码
#ifdef _WIN32
   //define something for Windows (32-bit and 64-bit, this part is common)
   #ifdef _WIN64
      //define something for Windows (64-bit only)
   #else
      //define something for Windows (32-bit only)
   #endif
#elif __ANDROID__
    // do android something
#elif __linux__
    // do linux something
#elif __unix__ // all unices not caught above
    // do Unix something
#elif defined(_POSIX_VERSION)
    // do POSIX something
#else
   #error "Unknown compiler"
#endif

or 代码内操作

注意: 条件没有使能的编译内容,编译器不会对该内容进行错误检查。

cpp 复制代码
#if defined(_WIN32)
    std::cout << "this is win32 compiler" << endl;
#elif defined(_WIN64)
    std::cout << "this is win64 compiler" << endl;
#elif defined(__linux__)
    std::cout << "this is linux compiler" << endl;
#elif defined(__unix__)
    std::cout << "this is unix compiler" << endl;
#elif defined(__ANDROID__)
    std::cout << "this is android compiler" << endl;
#endif

附注QT .pro自动判断平台

QT 工程.pro内的宏自定义判断平台

bash 复制代码
unix {  
    TARGET = appname
}
macx {
    TARGET = appname2
}
win32 {
    TARGET = appname3
}
相关推荐
小鹿软件办公15 分钟前
KDE 重磅发布:digiKam 9.0 正式登场,全面升级 Qt 6 核心
开发语言·qt·digikam
Ronin15 分钟前
QT中使用toInt函数判断条件时,要注意越界
开发语言·qt
载数而行52016 分钟前
QT系列,对象树 栈和堆 QDebug以及日志打印
c++·qt·学习
笨笨马甲18 分钟前
Qt 嵌入式开发概述
qt
永远的魔术1号21 分钟前
QtMaterialDialog对话框无法正常显示问题排查与解决方案
qt
xiaoye-duck24 分钟前
《算法题讲解指南:优选算法-分治-快排》--45.数组中的第k个最大元素,46.最小的k个数
c++·算法
SCBAiotAigc24 分钟前
2026.3.7:具身智能之51单片机<二>:ISP烧录过程
c++·人工智能·单片机·嵌入式硬件·51单片机·c
tankeven30 分钟前
HJ125 最大最小路
c++·算法
梦游钓鱼33 分钟前
Timestamp.cc和Timestamp.h文件分析
开发语言·c++
十年编程老舅37 分钟前
Linux GDB 调试超详细教程:入门 + 实战
linux·c++·gdb