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
}
相关推荐
_nirvana_w_3 分钟前
Qt项目链接库时遇到的坑:-l选项的正确用法
开发语言·c++·qt·qt框架·elawidgettools
我命由我123457 分钟前
Visual Studio - Visual Studio 修改项目的字符集
c语言·开发语言·c++·ide·学习·visualstudio·visual studio
郝学胜-神的一滴14 分钟前
Python变量本质:从指针哲学到Vibe Coding优化
开发语言·c++·python·程序人生
s_w.h15 分钟前
【 C++ 】搜索二叉树
java·开发语言·c++·算法
俩娃妈教编程18 分钟前
2023 年 09 月 二级真题(2)--数字黑洞
c++·算法·while
SCLchuck19 分钟前
std::function 在析构阶段触发非法内存访问
c++·lambda
星火开发设计19 分钟前
关联式容器:map 与 multimap 的键值对存储
java·开发语言·数据结构·c++·算法
云泽80819 分钟前
从图形界面到跨平台王者:Qt 客户端开发全解析
开发语言·qt
散峰而望24 分钟前
【算法竞赛】二叉树
开发语言·数据结构·c++·算法·深度优先·动态规划·宽度优先
持梦远方28 分钟前
QML 与 C++ 后端交互学习笔记
c++·qt·学习·交互