4

I'm using the MinGW-w64 version tagged as x86_64-8.1.0-posix-seh-rt_v6-rev0. When running g++ --version, I see this:

g++.exe (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0

From my understanding, this version of g++ should generate 64-bit binaries by default.

The compiler command is like this:

g++.exe -std=c++17 -g main.cpp -o main.exe

However, if main.cpp looks like this:

#include <iostream>
int main() {
    std::cout << sizeof(long);
    return 0;
}

It prints 4 instead of 8. I tried using a -m64 compiler flag, but it changed nothing.

What am I doing wrong, and how to fix this?

Semisonic
  • 1,152
  • 8
  • 21
  • 2
    `sizeof(long)` does not define if binary is 32 or 64 bitm - see https://stackoverflow.com/a/589685/432358 and for LLP64 de-facto standard – Slava Feb 05 '20 at 18:40
  • Does this answer your question? [What does the C++ standard state the size of int, long type to be?](https://stackoverflow.com/questions/589575/what-does-the-c-standard-state-the-size-of-int-long-type-to-be) – Alan Birtles Feb 05 '20 at 18:41
  • @AlanBirtles, the link @Slava posted is more relevant I think, considering we're talking about different de-facto standards on various platforms. The same code I posted above was returning `8` when built on Linux, hence the misinterpretation of the observed behavior on Windows. Remy's answer is at the same time the best option to determine whether the binary is a 64-bit one. – Semisonic Feb 05 '20 at 18:46
  • err, that is the post that Slava linked to? – Alan Birtles Feb 05 '20 at 19:04
  • @AlanBirtles, hm, yes, indeed. Not the top answer though. Still, I stand corrected. – Semisonic Feb 05 '20 at 19:22

1 Answers1

8

long is not guaranteed to be 64 bits in size in a 64bit executable. In fact, on Windows, long is always 32 bits under both x86 and x64. Use long long or __int64 or int64_t if you need a 64 bit integer. If you just want to check if your executable is compiled for 32-bit vs 64-bit, use sizeof(void*) instead.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770