添加OpenSSL代码(SSL_library_init)后程序无法运行

问题描述 投票:0回答:1

我为 WIndows10 编译,使用 gcc for Cygwin64 openssl是通过cygwin的安装程序安装的 添加 SSL_library_init();

前后代码及编译运行结果对比:

测试.c

#include <openssl/ssl.h>
#include <stdio.h>

void main()
{
    printf("%s","test");
    // SSL_library_init();

}

测试2.c

#include <openssl/ssl.h>
#include <stdio.h>

void main()
{
    printf("%s","test");
    SSL_library_init();

}

运行测试.c:

PS C:\code_c> gcc -v -o test .\test.c -lssl -lcrypto
Using built-in specs.
COLLECT_GCC=/usr/bin/gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-w64-mingw32/12/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: /mnt/share/cygpkgs/mingw64-x86_64-gcc/mingw64-x86_64-gcc.x86_64/src/gcc-12.4.0/configure --srcdir=/mnt/share/cygpkgs/mingw64-x86_64-gcc/mingw64-x86_64-gcc.x86_64/src/gcc-12.4.0 --prefix=/usr --exec-prefix=/usr --localstatedir=/var --sysconfdir=/etc --docdir=/usr/share/doc/mingw64-x86_64-gcc --htmldir=/usr/share/doc/mingw64-x86_64-gcc/html -C --build=x86_64-pc-cygwin --host=x86_64-pc-cygwin --target=x86_64-w64-mingw32 --without-libiconv-prefix --without-libintl-prefix --with-sysroot=/usr/x86_64-w64-mingw32/sys-root --with-build-sysroot=/usr/x86_64-w64-mingw32/sys-root --disable-multilib --disable-win32-registry --enable-languages=c,c++,fortran,lto,objc,obj-c++ --enable-fully-dynamic-string --enable-graphite --enable-libgomp --enable-libquadmath --enable-libquadmath-support --enable-libssp --enable-version-specific-runtime-libs --enable-libgomp --enable-libada --with-dwarf2 --with-gcc-major-version-only --with-gnu-ld --with-gnu-as --with-tune=generic --with-cloog-include=/usr/include/cloog-isl --with-system-zlib --enable-threads=posix --libexecdir=/usr/lib
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 12.4.0 (GCC)
COLLECT_GCC_OPTIONS='-v' '-o' 'test.exe' '-mtune=generic' '-march=x86-64' '-dumpdir' 'test-'
 /usr/lib/gcc/x86_64-w64-mingw32/12/cc1.exe -quiet -v -D_REENTRANT .\test.c -quiet -dumpdir test- -dumpbase .\test.c -dumpbase-ext .c -mtune=generic -march=x86-64 -version -o /cygdrive/c/Users/ADMINM~1/AppData/Local/Temp/ccSpZwNu.s
GNU C17 (GCC) version 12.4.0 (x86_64-w64-mingw32)
        compiled by GNU C version 12.4.0, GMP version 6.3.0, MPFR version 4.2.1, MPC version 1.3.1, isl version isl-0.27-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/x86_64-w64-mingw32/sys-root/usr/local/include"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-w64-mingw32/12/../../../../x86_64-w64-mingw32/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-w64-mingw32/12/include
 /usr/lib/gcc/x86_64-w64-mingw32/12/include-fixed
 /usr/x86_64-w64-mingw32/sys-root/mingw/include
End of search list.
GNU C17 (GCC) version 12.4.0 (x86_64-w64-mingw32)
        compiled by GNU C version 12.4.0, GMP version 6.3.0, MPFR version 4.2.1, MPC version 1.3.1, isl version isl-0.27-GMP

Compiler executable checksum: c58d452fc50d2e54c4f03a2b00330126
 /usr/lib/gcc/x86_64-w64-mingw32/12/../../../../x86_64-w64-mingw32/bin/as.exe -v -o /cygdrive/c/Users/ADMINM~1/AppData/Local/Temp/ccC6I3Xt.o /cygdrive/c/Users/ADMINM~1/AppData/Local/Temp/ccSpZwNu.s
GNU assembler version 2.43.1 (x86_64-w64-mingw32) using BFD version (GNU Binutils) 2.43.1
COMPILER_PATH=/usr/lib/gcc/x86_64-w64-mingw32/12/:/usr/lib/gcc/x86_64-w64-mingw32/12/:/usr/lib/gcc/x86_64-w64-mingw32/:/usr/lib/gcc/x86_64-w64-mingw32/12/:/usr/lib/gcc/x86_64-w64-mingw32/:/usr/lib/gcc/x86_64-w64-mingw32/12/../../../../x86_64-w64-mingw32/bin/
LIBRARY_PATH=/usr/lib/gcc/x86_64-w64-mingw32/12/:/usr/lib/gcc/x86_64-w64-mingw32/12/../../../../x86_64-w64-mingw32/lib/../lib/:/usr/x86_64-w64-mingw32/sys-root/mingw/lib/../lib/:/usr/lib/gcc/x86_64-w64-mingw32/12/../../../../x86_64-w64-mingw32/lib/:/usr/x86_64-w64-mingw32/sys-root/mingw/lib/
COLLECT_GCC_OPTIONS='-v' '-o' 'test.exe' '-mtune=generic' '-march=x86-64' '-dumpdir' 'test.'
 /usr/lib/gcc/x86_64-w64-mingw32/12/collect2.exe -plugin /usr/lib/gcc/x86_64-w64-mingw32/12/cyglto_plugin.dll -plugin-opt=/usr/lib/gcc/x86_64-w64-mingw32/12/lto-wrapper.exe -plugin-opt=-fresolution=/cygdrive/c/Users/ADMINM~1/AppData/Local/Temp/ccdL68fZ.res -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_eh -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt -plugin-opt=-pass-through=-lkernel32 -plugin-opt=-pass-through=-lpthread -plugin-opt=-pass-through=-ladvapi32 -plugin-opt=-pass-through=-lshell32 -plugin-opt=-pass-through=-luser32 -plugin-opt=-pass-through=-lkernel32 -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_eh -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt -plugin-opt=-pass-through=-lkernel32 --sysroot=/usr/x86_64-w64-mingw32/sys-root -m i386pep -Bdynamic -o test.exe /usr/x86_64-w64-mingw32/sys-root/mingw/lib/../lib/crt2.o /usr/lib/gcc/x86_64-w64-mingw32/12/crtbegin.o -L/usr/lib/gcc/x86_64-w64-mingw32/12 -L/usr/lib/gcc/x86_64-w64-mingw32/12/../../../../x86_64-w64-mingw32/lib/../lib -L/usr/x86_64-w64-mingw32/sys-root/mingw/lib/../lib -L/usr/lib/gcc/x86_64-w64-mingw32/12/../../../../x86_64-w64-mingw32/lib -L/usr/x86_64-w64-mingw32/sys-root/mingw/lib /cygdrive/c/Users/ADMINM~1/AppData/Local/Temp/ccC6I3Xt.o -lssl -lcrypto -lmingw32 -lgcc -lgcc_eh -lmoldname -lmingwex -lmsvcrt -lkernel32 -lpthread -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 -lgcc -lgcc_eh -lmoldname -lmingwex -lmsvcrt -lkernel32 /usr/x86_64-w64-mingw32/sys-root/mingw/lib/../lib/default-manifest.o /usr/lib/gcc/x86_64-w64-mingw32/12/crtend.o
COLLECT_GCC_OPTIONS='-v' '-o' 'test.exe' '-mtune=generic' '-march=x86-64' '-dumpdir' 'test.'
PS C:\code_c> ./test.exe
test
PS C:\code_c>

运行test2.c

PS C:\code_c> gcc -v -o test2.exe .\test2.c -lssl -lcrypto
Using built-in specs.
COLLECT_GCC=/usr/bin/gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-w64-mingw32/12/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: /mnt/share/cygpkgs/mingw64-x86_64-gcc/mingw64-x86_64-gcc.x86_64/src/gcc-12.4.0/configure --srcdir=/mnt/share/cygpkgs/mingw64-x86_64-gcc/mingw64-x86_64-gcc.x86_64/src/gcc-12.4.0 --prefix=/usr --exec-prefix=/usr --localstatedir=/var --sysconfdir=/etc --docdir=/usr/share/doc/mingw64-x86_64-gcc --htmldir=/usr/share/doc/mingw64-x86_64-gcc/html -C --build=x86_64-pc-cygwin --host=x86_64-pc-cygwin --target=x86_64-w64-mingw32 --without-libiconv-prefix --without-libintl-prefix --with-sysroot=/usr/x86_64-w64-mingw32/sys-root --with-build-sysroot=/usr/x86_64-w64-mingw32/sys-root --disable-multilib --disable-win32-registry --enable-languages=c,c++,fortran,lto,objc,obj-c++ --enable-fully-dynamic-string --enable-graphite --enable-libgomp --enable-libquadmath --enable-libquadmath-support --enable-libssp --enable-version-specific-runtime-libs --enable-libgomp --enable-libada --with-dwarf2 --with-gcc-major-version-only --with-gnu-ld --with-gnu-as --with-tune=generic --with-cloog-include=/usr/include/cloog-isl --with-system-zlib --enable-threads=posix --libexecdir=/usr/lib
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 12.4.0 (GCC)
COLLECT_GCC_OPTIONS='-v' '-o' 'test2.exe' '-mtune=generic' '-march=x86-64' '-dumpdir' 'test2-'
 /usr/lib/gcc/x86_64-w64-mingw32/12/cc1.exe -quiet -v -D_REENTRANT .\test2.c -quiet -dumpdir test2- -dumpbase .\test2.c -dumpbase-ext .c -mtune=generic -march=x86-64 -version -o /cygdrive/c/Users/ADMINM~1/AppData/Local/Temp/cccgzwcS.s
GNU C17 (GCC) version 12.4.0 (x86_64-w64-mingw32)
        compiled by GNU C version 12.4.0, GMP version 6.3.0, MPFR version 4.2.1, MPC version 1.3.1, isl version isl-0.27-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/x86_64-w64-mingw32/sys-root/usr/local/include"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-w64-mingw32/12/../../../../x86_64-w64-mingw32/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-w64-mingw32/12/include
 /usr/lib/gcc/x86_64-w64-mingw32/12/include-fixed
 /usr/x86_64-w64-mingw32/sys-root/mingw/include
End of search list.
GNU C17 (GCC) version 12.4.0 (x86_64-w64-mingw32)
        compiled by GNU C version 12.4.0, GMP version 6.3.0, MPFR version 4.2.1, MPC version 1.3.1, isl version isl-0.27-GMP

Compiler executable checksum: c58d452fc50d2e54c4f03a2b00330126
COLLECT_GCC_OPTIONS='-v' '-o' 'test2.exe' '-mtune=generic' '-march=x86-64' '-dumpdir' 'test2-'
 /usr/lib/gcc/x86_64-w64-mingw32/12/../../../../x86_64-w64-mingw32/bin/as.exe -v -o /cygdrive/c/Users/ADMINM~1/AppData/Local/Temp/cc7nbWjK.o /cygdrive/c/Users/ADMINM~1/AppData/Local/Temp/cccgzwcS.s
GNU assembler version 2.43.1 (x86_64-w64-mingw32) using BFD version (GNU Binutils) 2.43.1
COMPILER_PATH=/usr/lib/gcc/x86_64-w64-mingw32/12/:/usr/lib/gcc/x86_64-w64-mingw32/12/:/usr/lib/gcc/x86_64-w64-mingw32/:/usr/lib/gcc/x86_64-w64-mingw32/12/:/usr/lib/gcc/x86_64-w64-mingw32/:/usr/lib/gcc/x86_64-w64-mingw32/12/../../../../x86_64-w64-mingw32/bin/
LIBRARY_PATH=/usr/lib/gcc/x86_64-w64-mingw32/12/:/usr/lib/gcc/x86_64-w64-mingw32/12/../../../../x86_64-w64-mingw32/lib/../lib/:/usr/x86_64-w64-mingw32/sys-root/mingw/lib/../lib/:/usr/lib/gcc/x86_64-w64-mingw32/12/../../../../x86_64-w64-mingw32/lib/:/usr/x86_64-w64-mingw32/sys-root/mingw/lib/
COLLECT_GCC_OPTIONS='-v' '-o' 'test2.exe' '-mtune=generic' '-march=x86-64' '-dumpdir' 'test2.'
 /usr/lib/gcc/x86_64-w64-mingw32/12/collect2.exe -plugin /usr/lib/gcc/x86_64-w64-mingw32/12/cyglto_plugin.dll -plugin-opt=/usr/lib/gcc/x86_64-w64-mingw32/12/lto-wrapper.exe -plugin-opt=-fresolution=/cygdrive/c/Users/ADMINM~1/AppData/Local/Temp/ccmPwAbT.res -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_eh -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt -plugin-opt=-pass-through=-lkernel32 -plugin-opt=-pass-through=-lpthread -plugin-opt=-pass-through=-ladvapi32 -plugin-opt=-pass-through=-lshell32 -plugin-opt=-pass-through=-luser32 -plugin-opt=-pass-through=-lkernel32 -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_eh -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt -plugin-opt=-pass-through=-lkernel32 --sysroot=/usr/x86_64-w64-mingw32/sys-root -m i386pep -Bdynamic -o test2.exe /usr/x86_64-w64-mingw32/sys-root/mingw/lib/../lib/crt2.o /usr/lib/gcc/x86_64-w64-mingw32/12/crtbegin.o -L/usr/lib/gcc/x86_64-w64-mingw32/12 -L/usr/lib/gcc/x86_64-w64-mingw32/12/../../../../x86_64-w64-mingw32/lib/../lib -L/usr/x86_64-w64-mingw32/sys-root/mingw/lib/../lib -L/usr/lib/gcc/x86_64-w64-mingw32/12/../../../../x86_64-w64-mingw32/lib -L/usr/x86_64-w64-mingw32/sys-root/mingw/lib /cygdrive/c/Users/ADMINM~1/AppData/Local/Temp/cc7nbWjK.o -lssl -lcrypto -lmingw32 -lgcc -lgcc_eh -lmoldname -lmingwex -lmsvcrt -lkernel32 -lpthread -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 -lgcc -lgcc_eh -lmoldname -lmingwex -lmsvcrt -lkernel32 /usr/x86_64-w64-mingw32/sys-root/mingw/lib/../lib/default-manifest.o /usr/lib/gcc/x86_64-w64-mingw32/12/crtend.o
COLLECT_GCC_OPTIONS='-v' '-o' 'test2.exe' '-mtune=generic' '-march=x86-64' '-dumpdir' 'test2.'
PS C:\code_c> .\test2.exe
PS C:\code_c>

为什么? 编译看起来还不错

我在 WSL 的 kali 中运行相同的命令 test2 能够正常打印出测试结果(不是交叉编译)

c windows openssl cygwin
1个回答
0
投票

注意到您正在使用MinGWGCC,所以我安装了相同的OpenSSL风格(mingw64-x86_64-openssl)。无论如何,我必须这样做,因为 Cygwin 的“标准”OpenSSL 没有开发内容(头文件,libs)。

main00.c

#include <stdio.h>
#include <openssl/ssl.h>


int main()
{
    printf("%s\n", OPENSSL_VERSION_TEXT);
    SSL_library_init();
    printf("\nDone.\n\n");
    return 0;
}

输出CygwinMinTTY):

[cfati@cfati-5510-0:/cygdrive/e/Work/Dev/StackExchange/StackOverflow/q079241224]> ~/sopr.sh
### Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ###

[064bit prompt]> ls
main00.c
[064bit prompt]>
[064bit prompt]> # Not relying on any environment variables (working with full paths), so I know for shure what I'm calling and from where
[064bit prompt]> x86_64-w64-mingw32-gcc -o main00_pc064.exe main00.c -I/usr/x86_64-w64-mingw32/sys-root/mingw/include -L /usr/x86_64-w64-mingw32/sys-root/mingw/lib64 -lssl
[064bit prompt]>
[064bit prompt]> ls
main00.c  main00_pc064.exe
[064bit prompt]>
[064bit prompt]> ./main00_pc064.exe
[064bit prompt]> # Same behavior as yours
[064bit prompt]> echo $?
127
[064bit prompt]>
[064bit prompt]> ldd ./main00_pc064.exe
        ntdll.dll => /cygdrive/c/WINDOWS/SYSTEM32/ntdll.dll (0x7ffc58ff0000)
        KERNEL32.DLL => /cygdrive/c/WINDOWS/System32/KERNEL32.DLL (0x7ffc57ba0000)
        KERNELBASE.dll => /cygdrive/c/WINDOWS/System32/KERNELBASE.dll (0x7ffc56940000)
        msvcrt.dll => /cygdrive/c/WINDOWS/System32/msvcrt.dll (0x7ffc58230000)
        libssl-3-x64.dll => not found
[064bit prompt]> # Look at last line (dependency)
[064bit prompt]>
[064bit prompt]> export _PATH=${PATH}
[064bit prompt]> export PATH=${PATH}:/usr/x86_64-w64-mingw32/sys-root/mingw/bin
[064bit prompt]> ldd ./main00_pc064.exe
        ntdll.dll => /cygdrive/c/WINDOWS/SYSTEM32/ntdll.dll (0x7ffc58ff0000)
        KERNEL32.DLL => /cygdrive/c/WINDOWS/System32/KERNEL32.DLL (0x7ffc57ba0000)
        KERNELBASE.dll => /cygdrive/c/WINDOWS/System32/KERNELBASE.dll (0x7ffc56940000)
        msvcrt.dll => /cygdrive/c/WINDOWS/System32/msvcrt.dll (0x7ffc58230000)
        libssl-3-x64.dll => /usr/x86_64-w64-mingw32/sys-root/mingw/bin/libssl-3-x64.dll (0x7ffc23ad0000)
        libcrypto-3-x64.dll => /usr/x86_64-w64-mingw32/sys-root/mingw/bin/libcrypto-3-x64.dll (0x7ffc1cd40000)
        ADVAPI32.dll => /cygdrive/c/WINDOWS/System32/ADVAPI32.dll (0x7ffc57ae0000)
        sechost.dll => /cygdrive/c/WINDOWS/System32/sechost.dll (0x7ffc587e0000)
        RPCRT4.dll => /cygdrive/c/WINDOWS/System32/RPCRT4.dll (0x7ffc57010000)
        bcrypt.dll => /cygdrive/c/WINDOWS/System32/bcrypt.dll (0x7ffc56790000)
        USER32.dll => /cygdrive/c/WINDOWS/System32/USER32.dll (0x7ffc58340000)
        win32u.dll => /cygdrive/c/WINDOWS/System32/win32u.dll (0x7ffc567c0000)
        GDI32.dll => /cygdrive/c/WINDOWS/System32/GDI32.dll (0x7ffc58700000)
        gdi32full.dll => /cygdrive/c/WINDOWS/System32/gdi32full.dll (0x7ffc56ea0000)
        msvcp_win.dll => /cygdrive/c/WINDOWS/System32/msvcp_win.dll (0x7ffc567f0000)
        ucrtbase.dll => /cygdrive/c/WINDOWS/System32/ucrtbase.dll (0x7ffc56690000)
        WS2_32.dll => /cygdrive/c/WINDOWS/System32/WS2_32.dll (0x7ffc582d0000)
        zlib1.dll => /usr/x86_64-w64-mingw32/sys-root/mingw/bin/zlib1.dll (0x7ffc38c20000)
[064bit prompt]>
[064bit prompt]> # Run again
[064bit prompt]> ./main00_pc064.exe
OpenSSL 3.0.15 3 Sep 2024

Done.

就是这样,显然 Cygwin 足够愚蠢,可以默默地崩溃并且不让用户知道加载程序找不到可执行文件的依赖项(可能是因为它是为普通 Win 构建的(不依赖于cygwin1.dll)?)。

由于它是由 MinGW 构建的,因此它应该在 Cygwin 之外运行。这是 Cmd 终端的输出:

[cfati@CFATI-5510-0:e:\Work\Dev\StackExchange\StackOverflow\q079241224]> sopr.bat
### Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ###

[prompt]>
[prompt]> main00_pc064.exe

[prompt]> :: This time a dialog popped up stating: "The code execution cannot proceed because libssl-3-x64.dll was not found. Reinstalling the program may fix this problem."
[prompt]> set _PATH=%PATH%

[prompt]> set PATH=%PATH%;c:\Install\pc064\Cygwin\Cygwin\Version\usr\x86_64-w64-mingw32\sys-root\mingw\bin

[prompt]>
[prompt]> main00_pc064.exe
OpenSSL 3.0.15 3 Sep 2024

Done.

关于你的第一个st程序,它运行是因为它没有链接到OpenSSL库,因为它不使用其中的任何内容(你可以从编译行中删除它们,它仍然可以工作)。

读起来可能很有趣:

© www.soinside.com 2019 - 2024. All rights reserved.