简体   繁体   English

qmake:检测目标位宽(32位或64位)

[英]qmake: detect target bit-width (32-bit or 64-bit)

I have settings in my program that depend on the bit-width of the target of my compilation. 我的程序中的设置取决于编译目标的位宽。 In case the width is 32-bit, some special macro has to be defined due to memory constraints. 在宽度为32位的情况下,由于存储器限制,必须定义一些特殊的宏。

I could not find any way in qmake to detect the bit-width of the target, while the same option is available in cmake with: CMAKE_SIZEOF_VOID_P ; 在qmake中找不到任何方法来检测目标的位宽,而cmake中的相同选项可用: CMAKE_SIZEOF_VOID_P ; where 8 is 64-bit and 4 is 32-bit. 其中8是64位,4是32位。

Is there something similar for qmake? qmake有类似的东西吗?


EDIT: Background on the problem as requested in the comments 编辑:评论中要求的问题背景

Part 1 : There's a C library that I'm using in my C++11 program that needs a macro to act differently on 32-bit systems. 第1部分 :我在C ++ 11程序中使用的C库需要宏在32位系统上采取不同的行为。

Part 2: In 32-bit systems, the memory is limited to 4 GB of virtual memory . 第2部分:在32位系统中,内存限制为4 GB的虚拟内存 Even if you're running a 64-bit system and machine, and even if you have 500 GB of swap memory, a 32-bit program cannot use more than 4 GB. 即使您运行的是64位系统和计算机,即使您有500 GB的交换内存,32位程序也不能使用超过4 GB的内存。 That's why, the library I'm using has special settings for 32-bit as to limit the amount of memory it uses. 这就是为什么我正在使用的库具有32位的特殊设置,以限制它使用的内存量。 Hence, I need to know whether we're compiling for a 32-bit target (eg, Raspberry Pi), to activate a required macro. 因此,我需要知道我们是否正在编译32位目标(例如,Raspberry Pi),以激活所需的宏。

Part 3: The library is built as a custom target in qmake before building my software. 第3部分:在构建我的软件之前,库是作为qmake中的自定义目标构建的。 Once the library is built, my software is built and is linked to that library. 构建库后,我的软件就构建完成并链接到该库。

I ended up using this solution. 我最终使用了这个解决方案。 First I added this to support linux: 首先我添加了这个来支持linux:

linux-g++:QMAKE_TARGET.arch = $$QMAKE_HOST.arch
linux-g++-32:QMAKE_TARGET.arch = x86
linux-g++-64:QMAKE_TARGET.arch = x86_64

and then this: 然后这个:

contains(QMAKE_TARGET.arch, x86_64) {
    message("Compiling for a 64-bit system")
} else {
    DEFINES += ABC
    message("Compiling for a 32-bit system")
}

Learned this from here . 这里学到了这一点

you can add to the .pro file something like 你可以添加.pro文件之类的东西

*-64
{
    message( "Building for 64 bit machine...")
}

so when qmake is executed, you should see the msg 所以当执行qmake时,你应该看到msg

Building for 64 bit machine... 为64位机器构建...

You should be able to use a macro in conjunction with compile-time constants (but not preprocessor constants) to set things up: 您应该能够将宏与编译时常量(但不是预处理器常量)结合使用来进行设置:

#define TARGET_64 (sizeof(void*) == 8)
#define TARGET_32 (sizeof(void*) == 4)

Then, for example to change the amount of memory allocated: 然后,例如,更改分配的内存量:

char buffer1[TARGET_32 ? 0x10000000 : 0x40000000];
char *buffer2;

void foo(void) {
  buffer2 = malloc(TARGET_32 ? 0x10000000 : 0x40000000);
}

There's probably no need for a macro to be tested with #ifdef - if you think that there is, you'd need to show the subject code in your question. 可能不需要使用#ifdef测试宏 - 如果您认为存在,则需要在您的问题中显示主题代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM