The Quantum Physicist
The Quantum Physicist

Reputation: 26336

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.

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; where 8 is 64-bit and 4 is 32-bit.

Is there something similar for 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.

Part 2: In 32-bit systems, the memory is limited to 4 GB of virtual memory. 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. That's why, the library I'm using has special settings for 32-bit as to limit the amount of memory it uses. Hence, I need to know whether we're compiling for a 32-bit target (e.g., Raspberry Pi), to activate a required macro.

Part 3: The library is built as a custom target in qmake before building my software. Once the library is built, my software is built and is linked to that library.

Upvotes: 4

Views: 1518

Answers (3)

The Quantum Physicist
The Quantum Physicist

Reputation: 26336

I ended up using this solution. First I added this to support 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.

Upvotes: 2

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.

Upvotes: 0

you can add to the .pro file something like

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

so when qmake is executed, you should see the msg

Building for 64 bit machine...

Upvotes: 0

Related Questions