Link 将 AES256 示例与 OpenSSL 结合使用时出错

Link error when using AES256 example with OpenSSL

在 Ubuntu 14.04 上使用 gcc 4.8.2 编译 openssl 示例。

gcc SSLsample.c -lssl3

linker给出未定义的符号:

SSLsample.c:(.text+0x25d): undefined reference to `EVP_CIPHER_CTX_new'
SSLsample.c:(.text+0x272): undefined reference to `EVP_aes_256_cbc'
SSLsample.c:(.text+0x294): undefined reference to `EVP_DecryptInit_ex'
SSLsample.c:(.text+0x2bc): undefined reference to `EVP_DecryptUpdate'
SSLsample.c:(.text+0x2ed): undefined reference to `EVP_DecryptFinal_ex'
SSLsample.c:(.text+0x309): undefined reference to `EVP_CIPHER_CTX_free'
collect2: error: ld returned 1 exit status
make: *** [sample] Error 1

engines下有很多库,但是列为备用实现,主要实现是什么?

给 link 的关于 -lssl 和 -lcrypt 的建议听起来不错,但看一下:

gcc SSLsample.c  -lssl -lcrypt
/tmp/cczNovmQ.o: In function `main':
SSLsample.c:(.text+0x4a): undefined reference to `ERR_load_crypto_strings'
SSLsample.c:(.text+0x4f): undefined reference to `OPENSSL_add_all_algorithms_no\
conf'
SSLsample.c:(.text+0x59): undefined reference to `OPENSSL_config'
SSLsample.c:(.text+0xc6): undefined reference to `BIO_dump_fp'
SSLsample.c:(.text+0x12c): undefined reference to `EVP_cleanup'
SSLsample.c:(.text+0x131): undefined reference to `ERR_free_strings'
/tmp/cczNovmQ.o: In function `handleErrors':
SSLsample.c:(.text+0x167): undefined reference to `ERR_print_errors_fp'
/tmp/cczNovmQ.o: In function `encrypt':
SSLsample.c:(.text+0x18c): undefined reference to `EVP_CIPHER_CTX_new'
SSLsample.c:(.text+0x1a1): undefined reference to `EVP_aes_256_cbc'
SSLsample.c:(.text+0x1c3): undefined reference to `EVP_EncryptInit_ex'
SSLsample.c:(.text+0x1eb): undefined reference to `EVP_EncryptUpdate'
SSLsample.c:(.text+0x21c): undefined reference to `EVP_EncryptFinal_ex'
SSLsample.c:(.text+0x238): undefined reference to `EVP_CIPHER_CTX_free'
/tmp/cczNovmQ.o: In function `decrypt':
SSLsample.c:(.text+0x25d): undefined reference to `EVP_CIPHER_CTX_new'
SSLsample.c:(.text+0x272): undefined reference to `EVP_aes_256_cbc'
SSLsample.c:(.text+0x294): undefined reference to `EVP_DecryptInit_ex'
SSLsample.c:(.text+0x2bc): undefined reference to `EVP_DecryptUpdate'
SSLsample.c:(.text+0x2ed): undefined reference to `EVP_DecryptFinal_ex'
SSLsample.c:(.text+0x309): undefined reference to `EVP_CIPHER_CTX_free'
collect2: error: ld returned 1 exit status
 gcc SSLsample.c -lssl3

使用:

gcc SSLsample.c -o sample.exe -lssl -lcrypto

图书馆的名称和顺序很重要。它的 libssllibcrypto,并且 libcrypto 必须跟在 libssl 之后,因为 libssl 取决于 libcrypto.

您也可以使用静态存档:

gcc SSLsample.c /usr/lib/libssl.a /usr/lib/libcrypto.a -o sample.exe

这避免了链接器和库路径的问题,以及 LD_PRELOAD 运行时的技巧。

AES256 等密码和其他加密实用程序是 libcrypto 库的一部分; libssl 主要关注 SSL/TLS 协议。 Link 与 -lcrypto 而不是 -lssl3.