skeeto / w64devkit

Portable C and C++ Development Kit for x64 (and x86) Windows
The Unlicense
3k stars 211 forks source link

Get a bad file descriptor error when using gcc in Windows 7 (64-bit) commmad prompt #75

Closed JohnnyLee91 closed 1 year ago

JohnnyLee91 commented 1 year ago

I got a bad file descriptor error when using gcc in Windows 7 (64-bit) commmad prompt. But if I launch w64devkit.exe and execute gcc command whihin it, everything goes well.

gcc in Windows command prompt:

C:\Users\Johnny>gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=C:/MinGW-w64/bin/../libexec/gcc/x86_64-w64-mingw32/13.1.0/lto-wrapper.exe Target: x86_64-w64-mingw32 Configured with: /gcc-13.1.0/configure --prefix=/w64devkit --with-sysroot=/w64devkit/x86_64-w64-mingw32 --with-native-system-header-dir=/include --target=x86_64-w64-mingw32 --host=x86_64-w64-mingw32 --enable-static --disable-shared --with-pic --with-gmp-include=/deps/include --with-gmp-lib=/deps/lib --with-mpc-include=/deps/include --with-mpc-lib=/deps/lib --with-mpfr-include=/deps/include --with-mpfr-lib=/deps/lib --enable-languages=c,c++ --enable-libgomp --enable-threads=posix --enable-version-specific-runtime-libs --disable-dependency-tracking --disable-multilib --disable-nls --disable-win32-registry --enable-mingw-wildcard CFLAGS_FOR_TARGET=-Os CXXFLAGS_FOR_TARGET=-Os LDFLAGS_FOR_TARGET=-s CFLAGS=-Os CXXFLAGS=-Os LDFLAGS=-s Thread model: posix Supported LTO compression algorithms: zlib gcc version 13.1.0 (GCC)

C:\Users\Johnny> C:\Users\Johnny>gcc "E:\Study\C\Projects\test.c" /usr/bin/ld: error: C:/MinGW-w64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/lib/../lib/crt2.o: lseek: Bad file descriptor collect2.exe: error: ld returned 1 exit status

C:\Users\Johnny>

gcc in w64devkit console:

~ # gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=C:/MinGW-w64/bin/../libexec/gcc/x86_64-w64-mingw32/13.1.0/lto-wrapper.exe Target: x86_64-w64-mingw32 Configured with: /gcc-13.1.0/configure --prefix=/w64devkit --with-sysroot=/w64devkit/x86_64-w64-mingw32 --with-native-system-header-dir=/include --target=x86_64-w64-mingw32 --host=x86_64-w64-mingw32 --enable-static --disable-shared --with-pic --with-gmp-include=/deps/include --with-gmp-lib=/deps/lib --with-mpc-include=/deps/include --with-mpc-lib=/deps/lib --with-mpfr-include=/deps/include --with-mpfr-lib=/deps/lib --enable-languages=c,c++ --enable-libgomp --enable-threads=posix --enable-version-specific-runtime-libs --disable-dependency-tracking --disable-multilib --disable-nls --disable-win32-registry --enable-mingw-wildcard CFLAGS_FOR_TARGET=-Os CXXFLAGS_FOR_TARGET=-Os LDFLAGS_FOR_TARGET=-s CFLAGS=-Os CXXFLAGS=-Os LDFLAGS=-s Thread model: posix Supported LTO compression algorithms: zlib gcc version 13.1.0 (GCC) ~ # ~ # gcc "E:\Study\C\Projects\test.c" ~ #

I have tried run gcc in the command prompt of Windows 11, it also works well.

Here is my Windows 7 system info:

OS Name Microsoft Windows 7 Ultimate Version 6.1.7601 Service Pack 1 Build 7601 Other OS Description Not Available OS Manufacturer Microsoft Corporation System Name LIMINGYANG System Manufacturer Hewlett-Packard System Model HP ProDesk 480 G2 MT (TPM) System Type x64-based PC Processor Intel(R) Core(TM) i3-4160 CPU @ 3.60GHz, 3600 Mhz, 2 Core(s), 4 Logical Processor(s) BIOS Version/Date Hewlett-Packard L02 v02.39, 12/11/2014 SMBIOS Version 2.7 Windows Directory C:\Windows System Directory C:\Windows\system32 Boot Device \Device\HarddiskVolume1 Locale People's Republic of China Hardware Abstraction Layer Version = "6.1.7601.24384" User Name LIMINGYANG\Johnny Time Zone China Standard Time Installed Physical Memory (RAM) 8.00 GB Total Physical Memory 7.92 GB Available Physical Memory 2.96 GB Total Virtual Memory 15.8 GB Available Virtual Memory 6.84 GB Page File Space 7.92 GB Page File C:\pagefile.sys

Peter0x44 commented 1 year ago

C:/MinGW-w64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/lib/../lib/crt2.o This path doesn't look like it's from w64devkit to me, is it possible you have multiple compiler installations conflicting on windows 7?

skeeto commented 1 year ago

The verbose output listing matches w64devkit, so despite the root being called "MinGW-w64" that looks like w64devkit. Perhaps the filesystem is corrupt? Looks like it got a seek error on crt2.o. Try checking, say, the md5sum on that file to see that it matches a working install. I suspect there will be an error computing the sum.

skeeto commented 1 year ago

On second thought, I see what you mean now, @Peter0x44. It's the right linker executable, but it's probably picking up the wrong CRT bits, from another installation.

JohnnyLee91 commented 1 year ago

@Peter0x44 @skeeto Sorry~ I forgot to mention that I have changed the root directory name from "w64devkit" to "MinGW-w64". And I also have tried with the original root directory name, but the issue still exists.

Tim-Sobolev commented 1 year ago

@Peter0x44 @skeeto Sorry~ I forgot to mention that I have changed the root directory name from "w64devkit" to "MinGW-w64". And I also have tried with the original root directory name, but the issue still exists.

Windows 7 x64 SP1, all updates, no other C/C++ installs. Didn't have any problems with gcc, at least at basic "Hello world!"

#include <stdio.h>

int main(int argc, char* argv[])
{
    printf("Hello World!\r\n");
    return 0;
}

Was downloaded: w64devkit-fortran-1.19.0.zip

Was unpacked to: C:\ProgramData

In console (cmd.exe):

C:\WORK>gcc
'gcc' is not recognized as an internal or external command,
operable program or batch file.

C:\WORK>set path=%path%;%ALLUSERSPROFILE%\w64devkit\bin

C:\WORK>gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=C:/ProgramData/w64devkit/bin/../libexec/gcc/x86_64-w64-mingw32/13.1.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: /gcc-13.1.0/configure --prefix=/w64devkit --with-sysroot=/w64devkit/x86_64-w64-mingw32 --with
-native-system-header-dir=/include --target=x86_64-w64-mingw32 --host=x86_64-w64-mingw32 --enable-static --dis
able-shared --with-pic --with-gmp-include=/deps/include --with-gmp-lib=/deps/lib --with-mpc-include=/deps/incl
ude --with-mpc-lib=/deps/lib --with-mpfr-include=/deps/include --with-mpfr-lib=/deps/lib --enable-languages=c,
c++,fortran --enable-libgomp --enable-threads=posix --enable-version-specific-runtime-libs --disable-dependenc
y-tracking --disable-multilib --disable-nls --disable-win32-registry --enable-mingw-wildcard CFLAGS_FOR_TARGET
=-Os CXXFLAGS_FOR_TARGET=-Os LDFLAGS_FOR_TARGET=-s CFLAGS=-Os CXXFLAGS=-Os LDFLAGS=-s
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 13.1.0 (GCC)

C:\WORK>gcc test.c -o test.exe

C:\WORK>test.exe
Hello World!
JohnnyLee91 commented 1 year ago

@skeeto @Peter0x44 Hey guys, thanks for your tips. I have fixed this issue which is not a bug of w64devkit itself. That's because I got the cygwin and w64devkit (renamed as "MinGW-64") on my system in a mess:

C:\Users\Johnny>where gcc C:\MinGW-w64\bin\gcc.exe

C:\Users\Johnny>where gcc2 C:\Cygwin64\bin\gcc2.exe

C:\Users\Johnny> C:\Users\Johnny>where ld C:\Cygwin64\bin\ld.exe C:\MinGW-w64\bin\ld.exe

With such a mess in the PATH environment variable, when I run gcc, the ld.exe in cygiwn environment wil be called by mistake... Sorry for wasting your precious time and I will close this issue.