- 1 year ago
Can't link 64-bit object file into an executable with MinGW32 gcc / ld
If you're using the MinGW32 version of GCC and LD (linker), it is designed for creating 32-bit executables. It does not support linking 64-bit object files directly into the executable.
To link a 64-bit object file into an executable, you need to use a 64-bit version of MinGW (MinGW-w64) that supports 64-bit compilation and linking. MinGW-w64 is a separate distribution of MinGW that provides both 32-bit and 64-bit development environments.
Here's what you can do:
Download and install MinGW-w64 for your desired architecture (x86_64 for 64-bit) from the official website or other trusted sources. Make sure to select the appropriate architecture during the installation process.
Set up your build environment to use the MinGW-w64 toolchain. Update your PATH environment variable to point to the MinGW-w64 bin directory or use the provided shell environment (mingw-w64 shell) to ensure that the correct compiler and linker are used.
Recompile the object file using the MinGW-w64 compiler (
x86_64-w64-mingw32-gcc
instead ofgcc
). Make sure to specify the correct architecture during compilation.bashx86_64-w64-mingw32-gcc -c file.c -o file.o
This will generate a 64-bit object file (
file.o
) compatible with the MinGW-w64 toolchain.Link the object file with the MinGW-w64 linker (
x86_64-w64-mingw32-ld
) to create the executable.bashx86_64-w64-mingw32-ld file.o -o executable.exe
This will generate a 64-bit executable (
executable.exe
) that includes the 64-bit object file.
By using the appropriate MinGW-w64 toolchain for 64-bit development, you can successfully link a 64-bit object file into an executable.