Q:

Cygwin gcc creates corrupted binary - the procedure entry point could not be located in the dynamic link library

I have been using Cygwin64 gcc to generate nightly build of my programs on Windows. Not sure since when this started, the exe created from the gcc on cygwin64 (8.0 or 9.x) quits silently when running. Using strace, the error shows as
"The procedure entry point could not be located in the dynamic link library"
To reproduce this issue, you may run the below commands in a cygwin64 terminal
git clone https://github.com/fangq/mcxcl.git
cd mcxcl/src
make clean
make
../bin/mcxcl

for the reason described in my other question, I can only compile it using cygwin gcc (x86_64-pc-cygwin-gcc.exe/x86_64-pc-cygwin-g++.exe) but not mingw64 gcc. The code needs OpenCL.dll (typically comes with your graphics driver, and located in C:\Windows\System32\)
However, even the compilation did not give any error, once the binary ../bin/mcxcl is generated, running it returns immediately without any message. Running strace ../bin/mcxcl prints the entry point error above.
Running the same commands in MSYS2 using the gcc package (not mingw64-gcc!), the output executable is fine. Both gcc in MSYS2 and Cygwin64 have the same version (9.3).
Can someone tell me what could be different between the two environments? If possible, how to make work? actually, it used to work. But broke after some recent cygwin upgrades.

A:

it turned out the issue was caused by linking with OpenCL.dll file.
here is the default and incorrect way to link with OpenCL.dll in Cygwin
g++ *.obj -lOpenCL -static -o ../bin/mcxcl

even though C:\Windows\System32\OpenCL.dll is listed by ldd, the binary does not run properly (see the log below). It is quite strange that even though libOpenCL is not on the system, g++ still outputs a binary without any complaints.
log when OpenCL.dll is linked incorrectly
fangq@zen:~/space/git/Project/github/mcxcl/src$ g++ -v mcx_host.obj mcx_utils.obj tictoc.obj mcxcl.obj mcx_shapes.obj cjson/cJSON.obj -L"C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v8.0"/lib/x64 -lOpenCL -o ../bin/mcxcl
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/lto-wrapper.exe
Target: x86_64-pc-cygwin
...
Thread model: posix
gcc version 9.3.0 (GCC)
COMPILER_PATH=/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/:/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/:/usr/lib/gcc/x86_64-pc-cygwin/:/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/:/usr/lib/gcc/x86_64-pc-cygwin/:/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/bin/
LIBRARY_PATH=/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/:/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/lib/../lib/:/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../lib/:/lib/../lib/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/lib/:/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-LC:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v8.0/lib/x64' '-o' '../bin/mcxcl.exe' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-pc-cygwin/9.3.0/collect2.exe -plugin /usr/lib/gcc/x86_64-pc-cygwin/9.3.0/cyglto_plugin.dll -plugin-opt=/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/lto-wrapper.exe -plugin-opt=-fresolution=/tmp/ccfQi5vc.res -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lcygwin -plugin-opt=-pass-through=-ladvapi32 -plugin-opt=-pass-through=-lshell32 -plugin-opt=-pass-through=-luser32 -plugin-opt=-pass-through=-lkernel32 -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc --build-id -m i386pep --wrap _Znwm --wrap _Znam --wrap _ZdlPv --wrap _ZdaPv --wrap _ZnwmRKSt9nothrow_t --wrap _ZnamRKSt9nothrow_t --wrap _ZdlPvRKSt9nothrow_t --wrap _ZdaPvRKSt9nothrow_t -Bdynamic --dll-search-prefix=cyg --tsaware -o ../bin/mcxcl.exe /usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../lib/crt0.o /usr/lib/gcc/x86_64-pc-cygwin/9.3.0/crtbegin.o -LC:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v8.0/lib/x64 -L/usr/lib/gcc/x86_64-pc-cygwin/9.3.0 -L/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/lib/../lib -L/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../lib -L/lib/../lib -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/lib -L/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../.. mcx_host.obj mcx_utils.obj tictoc.obj mcxcl.obj mcx_shapes.obj cjson/cJSON.obj -lOpenCL -lstdc++ -lgcc_s -lgcc -lcygwin -ladvapi32 -lshell32 -luser32 -lkernel32 -lgcc_s -lgcc /usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../lib/default-manifest.o /usr/lib/gcc/x86_64-pc-cygwin/9.3.0/crtend.o
/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../lib/libOpenCL.a when searching for -lOpenCL
COLLECT_GCC_OPTIONS='-v' '-LC:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v8.0/lib/x64' '-o' '../bin/mcxcl.exe' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
fangq@zen:~/space/git/Project/github/mcxcl/src$ ldd ../bin/mcxcl
        ntdll.dll => /cygdrive/c/WINDOWS/SYSTEM32/ntdll.dll (0x7ffcf9310000)
        KERNEL32.DLL => /cygdrive/c/WINDOWS/System32/KERNEL32.DLL (0x7ffcf6d90000)
        KERNELBASE.dll => /cygdrive/c/WINDOWS/System32/KERNELBASE.dll (0x7ffcf58b0000)
        cygwin1.dll => /cygdrive/d/Users/fangq/Gitroot/Project/github/mcxcl/bin/cygwin1.dll (0x180040000)
        OpenCL.dll => /cygdrive/c/WINDOWS/SYSTEM32/OpenCL.dll (0x7ffcb9ac0000)
        ADVAPI32.dll => /cygdrive/c/WINDOWS/System32/ADVAPI32.dll (0x7ffcf9120000)
        cygstdc++-6.dll => /usr/bin/cygstdc++-6.dll (0x3fbc30000)
fangq@zen:~/space/git/Project/github/mcxcl/src$ strace ../bin/mcxcl
--- Process 20212 created
--- Process 20212 loaded C:\Windows\System32\ntdll.dll at 00007ffcf9310000
--- Process 20212 loaded C:\Windows\System32\kernel32.dll at 00007ffcf6d90000
--- Process 20212 loaded C:\Windows\System32\KernelBase.dll at 00007ffcf58b0000
--- Process 20212 thread 16812 created
--- Process 20212 thread 15708 created
--- Process 20212 thread 19804 created
--- Process 20212 loaded D:\Users\fangq\Gitroot\Project\github\mcxcl\bin\cygwin1.dll at 0000000180040000
--- Process 20212 loaded C:\Windows\System32\OpenCL.dll at 00007ffcb9ac0000
--- Process 20212 loaded C:\Windows\System32\advapi32.dll at 00007ffcf9120000
--- Process 20212 loaded C:\cygwin64\bin\cygstdc++-6.dll at 00000003fbc30000
--- Process 20212 loaded C:\Windows\System32\msvcrt.dll at 00007ffcf6e80000
--- Process 20212 loaded C:\Windows\System32\sechost.dll at 00007ffcf7c80000
--- Process 20212 loaded C:\Windows\System32\rpcrt4.dll at 00007ffcf67e0000
--- Process 20212 loaded C:\cygwin64\bin\cyggcc_s-seh-1.dll at 00000003feec0000
--- Process 20212 loaded C:\Windows\System32\cfgmgr32.dll at 00007ffcf6020000
--- Process 20212 loaded C:\Windows\System32\ucrtbase.dll at 00007ffcf5ca0000
--- Process 20212 loaded C:\Windows\System32\ole32.dll at 00007ffcf7230000
--- Process 20212 loaded C:\Windows\System32\combase.dll at 00007ffcf6f20000
--- Process 20212 loaded C:\Windows\System32\bcryptprimitives.dll at 00007ffcf5c20000
--- Process 20212 loaded C:\Windows\System32\gdi32.dll at 00007ffcf6e50000
--- Process 20212 loaded C:\Windows\System32\gdi32full.dll at 00007ffcf5710000
--- Process 20212 loaded C:\Windows\System32\msvcp_win.dll at 00007ffcf5b20000
--- Process 20212 loaded C:\Windows\System32\user32.dll at 00007ffcf7af0000
--- Process 20212 loaded C:\Windows\System32\win32u.dll at 00007ffcf6070000
--- Process 20212, exception c0000139 at 00007ffcf9312b10
--- Process 20212 thread 16812 exited with status 0xc0000139
--- Process 20212 thread 19804 exited with status 0xc0000139
--- Process 20212 thread 15708 exited with status 0xc0000139
--- Process 20212 exited with status 0xc0000139

However, if I replace -lOpenCL by "C:\Windows\System32\OpenCL.dll" directly, ie.
g++ *.obj "C:\Windows\System32\OpenCL.dll" -static -o ../bin/mcxcl

now the generated file has a lot more dlls using ldd, and it runs properly.
Logs when directly link with "C:\Windows\System32\OpenCL.dll"
fangq@zen:~/space/git/Project/github/mcxcl/src$ g++ -v mcx_host.obj mcx_utils.obj tictoc.obj mcxcl.obj mcx_shapes.obj cjson/cJSON.obj -L"C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v8.0"/lib/x64 "C:\Windows\System32\OpenCL.dll" -static -o ../bin/mcxcl
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/lto-wrapper.exe
Target: x86_64-pc-cygwin
...
Thread model: posix
gcc version 9.3.0 (GCC)
COMPILER_PATH=/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/:/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/:/usr/lib/gcc/x86_64-pc-cygwin/:/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/:/usr/lib/gcc/x86_64-pc-cygwin/:/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/bin/
LIBRARY_PATH=/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/:/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/lib/../lib/:/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../lib/:/lib/../lib/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/lib/:/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-LC:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v8.0/lib/x64' '-static' '-o' '../bin/mcxcl.exe' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-pc-cygwin/9.3.0/collect2.exe -plugin /usr/lib/gcc/x86_64-pc-cygwin/9.3.0/cyglto_plugin.dll -plugin-opt=/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/lto-wrapper.exe -plugin-opt=-fresolution=/tmp/ccVd88qq.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_eh -plugin-opt=-pass-through=-lcygwin -plugin-opt=-pass-through=-ladvapi32 -plugin-opt=-pass-through=-lshell32 -plugin-opt=-pass-through=-luser32 -plugin-opt=-pass-through=-lkernel32 -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_eh --build-id -m i386pep --wrap _Znwm --wrap _Znam --wrap _ZdlPv --wrap _ZdaPv --wrap _ZnwmRKSt9nothrow_t --wrap _ZnamRKSt9nothrow_t --wrap _ZdlPvRKSt9nothrow_t --wrap _ZdaPvRKSt9nothrow_t -Bstatic --dll-search-prefix=cyg --tsaware -o ../bin/mcxcl.exe /usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../lib/crt0.o /usr/lib/gcc/x86_64-pc-cygwin/9.3.0/crtbegin.o -LC:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v8.0/lib/x64 -L/usr/lib/gcc/x86_64-pc-cygwin/9.3.0 -L/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/lib/../lib -L/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../lib -L/lib/../lib -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/lib -L/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../.. mcx_host.obj mcx_utils.obj tictoc.obj mcxcl.obj mcx_shapes.obj cjson/cJSON.obj C:\Windows\System32\OpenCL.dll -lstdc++ -lgcc -lgcc_eh -lcygwin -ladvapi32 -lshell32 -luser32 -lkernel32 -lgcc -lgcc_eh /usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../lib/default-manifest.o /usr/lib/gcc/x86_64-pc-cygwin/9.3.0/crtend.o
COLLECT_GCC_OPTIONS='-v' '-LC:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v8.0/lib/x64' '-static' '-o' '../bin/mcxcl.exe' '-mtune=generic' '-march=x86-64'
fangq@zen:~/space/git/Project/github/mcxcl/src$ ldd ../bin/mcxcl
        ntdll.dll => /cygdrive/c/WINDOWS/SYSTEM32/ntdll.dll (0x7ffcf9310000)
        KERNEL32.DLL => /cygdrive/c/WINDOWS/System32/KERNEL32.DLL (0x7ffcf6d90000)
        KERNELBASE.dll => /cygdrive/c/WINDOWS/System32/KERNELBASE.dll (0x7ffcf58b0000)
        cygwin1.dll => /cygdrive/d/Users/fangq/Gitroot/Project/github/mcxcl/bin/cygwin1.dll (0x180040000)
        OpenCL.dll => /cygdrive/c/WINDOWS/SYSTEM32/OpenCL.dll (0x7ffcb9e60000)
        ADVAPI32.dll => /cygdrive/c/WINDOWS/System32/ADVAPI32.dll (0x7ffcf9120000)
        msvcrt.dll => /cygdrive/c/WINDOWS/System32/msvcrt.dll (0x7ffcf6e80000)
        sechost.dll => /cygdrive/c/WINDOWS/System32/sechost.dll (0x7ffcf7c80000)
        RPCRT4.dll => /cygdrive/c/WINDOWS/System32/RPCRT4.dll (0x7ffcf67e0000)
        CFGMGR32.dll => /cygdrive/c/WINDOWS/System32/CFGMGR32.dll (0x7ffcf6020000)
        ucrtbase.dll => /cygdrive/c/WINDOWS/System32/ucrtbase.dll (0x7ffcf5ca0000)
        ole32.dll => /cygdrive/c/WINDOWS/System32/ole32.dll (0x7ffcf7230000)
        combase.dll => /cygdrive/c/WINDOWS/System32/combase.dll (0x7ffcf6f20000)
        bcryptPrimitives.dll => /cygdrive/c/WINDOWS/System32/bcryptPrimitives.dll (0x7ffcf5c20000)
        GDI32.dll => /cygdrive/c/WINDOWS/System32/GDI32.dll (0x7ffcf6e50000)
        gdi32full.dll => /cygdrive/c/WINDOWS/System32/gdi32full.dll (0x7ffcf5710000)
        msvcp_win.dll => /cygdrive/c/WINDOWS/System32/msvcp_win.dll (0x7ffcf5b20000)
        USER32.dll => /cygdrive/c/WINDOWS/System32/USER32.dll (0x7ffcf7af0000)
        win32u.dll => /cygdrive/c/WINDOWS/System32/win32u.dll (0x7ffcf6070000)
fangq@zen:~/space/git/Project/github/mcxcl/src$ ../bin/mcxcl
==============================================================================
=                       Monte Carlo eXtreme (MCX) -- OpenCL                  =
...

