简体   繁体   中英

64-bit version of GCC not compiling 64-bit exe

I am beginner regarding gcc command line compilation. I need a help regarding -m64 flag.

I installed gcc compiler using MinGW. I checked for gcc version by following, gcc -v command, which shows Target: x86_64-w64-mingw32 . So I assume, 64-bit version of gcc is installed.

Objective : I wrote a small program to check, if the main.exe is generated for 32 or 64 bit.

#include<stdio.h>

int main(void)
{
    printf("The Size is: %lu\n", sizeof(long));
    
    return 0;   
}

I compiled using following command, gcc -o main main.c . When I execute the main.exe, it outputs, The Size is: 4 .

But I expected the output to be `The Size is: 8'.

So i modified the command as gcc -m64 -o main main.c . When I executed the main.exe again, still it outputs `The Size is: 4'

How to compile for 64-bit version exe?

As others have said in the comments, the size of long can be 8 or 4 bytes on a 64bit system. You can try sizeof(size_t) or sizeof(void*) . Even this might not be reliable on every system (but should work for Windows, Linux, macOS).

x86_64-w64-mingw32: The mingw32 is compiler that will generate 32bits executables. The references to 64bit in you package name indicates that this compiler runs in 64bits mode.

If you wan't to generate 64 bits executables, you will need mingw64 compiler: https://www.mingw-w64.org/

Here is a better way of doing it.

First download Sigcheck from Microsoft https://docs.microsoft.com/en-us/sysinternals/downloads/sigcheck then run it like below:

C:\Sigcheck>sigcheck64.exe -u -e "C:\Sublime C++ Projects\runtime_measure.exe"

Sigcheck v2.82 - File version and signature viewer
Copyright (C) 2004-2021 Mark Russinovich
Sysinternals - www.sysinternals.com

c:\sublime c++ projects\runtime_measure.exe:
        Verified:       Unsigned
        Link date:      7:43 PM 12/8/2021
        Publisher:      n/a
        Company:        n/a
        Description:    n/a
        Product:        n/a
        Prod version:   n/a
        File version:   n/a
        MachineType:    64-bit

As you can see, in this case, runtime_measure.exe is a 64-bit binary.

Don't forget to give the correct address so that the terminal can find and execute sigcheck64.exe from the directory you have placed it.

Also, notice the use of two parameters -u and -e in the command.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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