Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does MinGW-w64 generate 32-bit binaries?

Tags:

c++

g++

mingw-w64

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?

like image 858
Semisonic Avatar asked Mar 02 '23 20:03

Semisonic


1 Answers

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.

like image 137
Remy Lebeau Avatar answered Mar 15 '23 14:03

Remy Lebeau