我正在基于OpenSSL编译一些代码。编译器,参数和错误如下所示。
$ gcc -I/home/share/pkp/include -L/home/share/pkp/lib -lssl -lcrypto -Wall -O3 -Wpedantic -Wno-unused-result -Wno-pointer-sign ot.c -o ot
/tmp/cck3sgve.o: In function `main':
ot.c:(.text.startup+0x20): undefined reference to `EVP_get_digestbyname'
ot.c:(.text.startup+0x31): undefined reference to `EVP_MD_CTX_new'
ot.c:(.text.startup+0x41): undefined reference to `EVP_DigestInit_ex'
ot.c:(.text.startup+0x53): undefined reference to `EVP_DigestFinal_ex'
ot.c:(.text.startup+0x5b): undefined reference to `EVP_MD_CTX_free'
collect2: error: ld returned 1 exit status
这里是编译环境中组件的版本。
$ uname -a
Linux yzx-lab3 4.4.0-17763-Microsoft #379-Microsoft Wed Mar 06 19:16:00 PST 2019 x86_64 x86_64 x86_64 GNU/Linux
$ /home/share/pkp/bin/openssl version
./bin/openssl: relocation error: ./bin/openssl: symbol EVP_mdc2 version OPENSSL_1_1_0 not defined in file libcrypto.so.1.1 with link time reference
$ grep "OPENSSL_VERSION" /home/share/pkp/openssl-1.1.1a/include/openssl/opensslv.h
# define OPENSSL_VERSION_NUMBER 0x1010101fL
# define OPENSSL_VERSION_TEXT "OpenSSL 1.1.1a 20 Nov 2018"
$ gcc --version
gcc (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
我知道有关类似错误的问题已经很多,并且已经尝试了许多解决方案。但是它们不起作用。
1)首先,我获取源代码并将所有文件解压缩到“ / home / share / pkp”。
wget https://www.openssl.org/source/old/1.1.1/openssl-1.1.1a.tar.gz
然后编译并安装openssl库。
./config --prefix=/home/share/pkp --openssldir=/home/share/pkp/openssl
make && make test && make install
在安装过程中没有错误消息。
2)为确保openssl正确编译和安装,我进行了一些验证。
$ ls /home/share/pkp/lib/ -l
total 14912
drwxrwxrwx 1 share share 4096 Jun 18 12:30 engines-1.1
-rw-r--r-- 1 share share 5700252 Jun 18 12:30 libcrypto.a
lrwxrwxrwx 1 share share 16 Jun 18 12:30 libcrypto.so -> libcrypto.so.1.1
-rwxr-xr-x 1 share share 3414744 Jun 18 12:30 libcrypto.so.1.1
-rw-r--r-- 1 share share 1043216 Jun 18 12:30 libssl.a
lrwxrwxrwx 1 share share 13 Jun 18 12:30 libssl.so -> libssl.so.1.1
-rwxr-xr-x 1 share share 692696 Jun 18 12:30 libssl.so.1.1
$ nm /home/share/pkp/lib/libcrypto.a | grep "EVP_get_digestbyname" | wc -l
24
$ du /home/share/pkp/include/openssl -ah
4.0K /home/share/pkp/include/openssl/aes.h
36K /home/share/pkp/include/openssl/asn1.h
0 /home/share/pkp/include/openssl/asn1_mac.h
16K /home/share/pkp/include/openssl/asn1err.h
36K /home/share/pkp/include/openssl/asn1t.h
4.0K /home/share/pkp/include/openssl/async.h
4.0K /home/share/pkp/include/openssl/asyncerr.h
36K /home/share/pkp/include/openssl/bio.h
8.0K /home/share/pkp/include/openssl/bioerr.h
4.0K /home/share/pkp/include/openssl/blowfish.h
24K /home/share/pkp/include/openssl/bn.h
8.0K /home/share/pkp/include/openssl/bnerr.h
4.0K /home/share/pkp/include/openssl/buffer.h
4.0K /home/share/pkp/include/openssl/buffererr.h
4.0K /home/share/pkp/include/openssl/camellia.h
4.0K /home/share/pkp/include/openssl/cast.h
4.0K /home/share/pkp/include/openssl/cmac.h
20K /home/share/pkp/include/openssl/cms.h
12K /home/share/pkp/include/openssl/cmserr.h
4.0K /home/share/pkp/include/openssl/comp.h
4.0K /home/share/pkp/include/openssl/comperr.h
8.0K /home/share/pkp/include/openssl/conf.h
4.0K /home/share/pkp/include/openssl/conf_api.h
4.0K /home/share/pkp/include/openssl/conferr.h
20K /home/share/pkp/include/openssl/crypto.h
4.0K /home/share/pkp/include/openssl/cryptoerr.h
16K /home/share/pkp/include/openssl/ct.h
4.0K /home/share/pkp/include/openssl/cterr.h
8.0K /home/share/pkp/include/openssl/des.h
16K /home/share/pkp/include/openssl/dh.h
4.0K /home/share/pkp/include/openssl/dherr.h
12K /home/share/pkp/include/openssl/dsa.h
4.0K /home/share/pkp/include/openssl/dsaerr.h
4.0K /home/share/pkp/include/openssl/dtls1.h
12K /home/share/pkp/include/openssl/e_os2.h
4.0K /home/share/pkp/include/openssl/ebcdic.h
64K /home/share/pkp/include/openssl/ec.h
0 /home/share/pkp/include/openssl/ecdh.h
0 /home/share/pkp/include/openssl/ecdsa.h
16K /home/share/pkp/include/openssl/ecerr.h
36K /home/share/pkp/include/openssl/engine.h
8.0K /home/share/pkp/include/openssl/engineerr.h
12K /home/share/pkp/include/openssl/err.h
76K /home/share/pkp/include/openssl/evp.h
12K /home/share/pkp/include/openssl/evperr.h
4.0K /home/share/pkp/include/openssl/hmac.h
4.0K /home/share/pkp/include/openssl/idea.h
8.0K /home/share/pkp/include/openssl/kdf.h
4.0K /home/share/pkp/include/openssl/kdferr.h
8.0K /home/share/pkp/include/openssl/lhash.h
4.0K /home/share/pkp/include/openssl/md2.h
4.0K /home/share/pkp/include/openssl/md4.h
4.0K /home/share/pkp/include/openssl/md5.h
4.0K /home/share/pkp/include/openssl/mdc2.h
12K /home/share/pkp/include/openssl/modes.h
216K /home/share/pkp/include/openssl/obj_mac.h
8.0K /home/share/pkp/include/openssl/objects.h
4.0K /home/share/pkp/include/openssl/objectserr.h
16K /home/share/pkp/include/openssl/ocsp.h
4.0K /home/share/pkp/include/openssl/ocsperr.h
8.0K /home/share/pkp/include/openssl/opensslconf.h
8.0K /home/share/pkp/include/openssl/opensslv.h
8.0K /home/share/pkp/include/openssl/ossl_typ.h
16K /home/share/pkp/include/openssl/pem.h
0 /home/share/pkp/include/openssl/pem2.h
8.0K /home/share/pkp/include/openssl/pemerr.h
12K /home/share/pkp/include/openssl/pkcs12.h
4.0K /home/share/pkp/include/openssl/pkcs12err.h
12K /home/share/pkp/include/openssl/pkcs7.h
8.0K /home/share/pkp/include/openssl/pkcs7err.h
4.0K /home/share/pkp/include/openssl/rand.h
8.0K /home/share/pkp/include/openssl/rand_drbg.h
8.0K /home/share/pkp/include/openssl/randerr.h
4.0K /home/share/pkp/include/openssl/rc2.h
4.0K /home/share/pkp/include/openssl/rc4.h
4.0K /home/share/pkp/include/openssl/rc5.h
4.0K /home/share/pkp/include/openssl/ripemd.h
24K /home/share/pkp/include/openssl/rsa.h
12K /home/share/pkp/include/openssl/rsaerr.h
8.0K /home/share/pkp/include/openssl/safestack.h
4.0K /home/share/pkp/include/openssl/seed.h
4.0K /home/share/pkp/include/openssl/sha.h
4.0K /home/share/pkp/include/openssl/srp.h
4.0K /home/share/pkp/include/openssl/srtp.h
112K /home/share/pkp/include/openssl/ssl.h
0 /home/share/pkp/include/openssl/ssl2.h
16K /home/share/pkp/include/openssl/ssl3.h
48K /home/share/pkp/include/openssl/sslerr.h
4.0K /home/share/pkp/include/openssl/stack.h
12K /home/share/pkp/include/openssl/store.h
8.0K /home/share/pkp/include/openssl/storeerr.h
4.0K /home/share/pkp/include/openssl/symhacks.h
72K /home/share/pkp/include/openssl/tls1.h
24K /home/share/pkp/include/openssl/ts.h
8.0K /home/share/pkp/include/openssl/tserr.h
4.0K /home/share/pkp/include/openssl/txt_db.h
16K /home/share/pkp/include/openssl/ui.h
4.0K /home/share/pkp/include/openssl/uierr.h
4.0K /home/share/pkp/include/openssl/whrlpool.h
44K /home/share/pkp/include/openssl/x509.h
32K /home/share/pkp/include/openssl/x509_vfy.h
8.0K /home/share/pkp/include/openssl/x509err.h
36K /home/share/pkp/include/openssl/x509v3.h
12K /home/share/pkp/include/openssl/x509v3err.h
1.6M /home/share/pkp/include/openssl
3)最后,我尝试了其他编译命令,但仍然无法正常工作。
$ gcc -I/home/share/pkp/include -L/home/share/pkp/lib -lcrypto -lssl -Wall -O3 -Wpedantic -Wno-unuse
d-result -Wno-pointer-sign ot.c -o ot
$ gcc -I/home/share/pkp/include -L/home/share/pkp/lib -lssl -lcrypto -Wall -O3 -Wpedantic -Wno-unuse
d-result -Wno-pointer-sign ot.c -o ot
$ gcc -I/home/share/pkp/include -L/home/share/pkp/lib -lcrypto -lssl -Wall -Wpedantic -Wno-unuse
d-result -Wno-pointer-sign ot.c -o ot
$ gcc -I/home/share/pkp/include -L/home/share/pkp/lib -lcrypto -O3 -Wall -Wpedantic -Wno-unuse
d-result -Wno-pointer-sign ot.c -o ot
4)似乎来自下载源代码的openssl与“ apt-get”安装的openssl冲突。所以我尝试了另一种方法。
./config && make && make test && make install
它将把openssl安装到系统目录中。然后我运行
$ gcc -lssl -lcrypto ot.c -o ot
/tmp/ccrFwoa7.o: In function `main':
ot.c:(.text+0x2d): undefined reference to `EVP_get_digestbyname'
ot.c:(.text+0x66): undefined reference to `EVP_MD_CTX_new'
ot.c:(.text+0x82): undefined reference to `EVP_DigestInit_ex'
ot.c:(.text+0x99): undefined reference to `EVP_DigestFinal_ex'
ot.c:(.text+0xa5): undefined reference to `EVP_MD_CTX_free'
collect2: error: ld returned 1 exit status
它仍然失败。 Envn我遵循here中的解决方案,尝试静态链接libcrypto和libssl。它再次失败。
$ gcc -lssl -lcrypto -ldl -lz -static -static-libgcc ot.c -o ot
/tmp/cc39C5Gq.o: In function `main':
ot.c:(.text+0x2d): undefined reference to `EVP_get_digestbyname'
ot.c:(.text+0x66): undefined reference to `EVP_MD_CTX_new'
ot.c:(.text+0x82): undefined reference to `EVP_DigestInit_ex'
ot.c:(.text+0x99): undefined reference to `EVP_DigestFinal_ex'
ot.c:(.text+0xa5): undefined reference to `EVP_MD_CTX_free'
collect2: error: ld returned 1 exit status
我确定所有静态库都在正确的位置。
$ whereis lib{ssl,crypto,z,dl}.a
libssl: /usr/lib/x86_64-linux-gnu/libssl.a /usr/lib/x86_64-linux-gnu/libssl.so /usr/local/lib/libssl.a /usr/local/lib/libssl.so
libcrypto: /usr/lib/x86_64-linux-gnu/libcrypto.a /usr/lib/x86_64-linux-gnu/libcrypto.so /usr/local/lib/libcrypto.a /usr/local/lib/libcrypto.so /mnt/c/Windows/System32/libcrypto.dll
libz: /usr/lib/x86_64-linux-gnu/libz.a /usr/lib/x86_64-linux-gnu/libz.so
libdl: /usr/lib/x86_64-linux-gnu/libdl.a /usr/lib/x86_64-linux-gnu/libdl.so
5)来这里发布这个问题。
#include <stdio.h>
#include <openssl/evp.h>
int main(int argc, char *argv[]){
EVP_MD_CTX *mdctx;
const EVP_MD *md;
unsigned char md_value[EVP_MAX_MD_SIZE];
int md_len, i;
md = EVP_get_digestbyname(argv[1]);
if(!md) {
printf("Unknown message digest %s\n", argv[1]);
exit(1);
}
mdctx = EVP_MD_CTX_new();
EVP_DigestInit_ex(mdctx, md, NULL);
EVP_DigestFinal_ex(mdctx, md_value, &md_len);
EVP_MD_CTX_free(mdctx);
printf("Digest is: ");
for (i = 0; i < md_len; i++)
printf("%02x", md_value[i]);
printf("\n");
exit(0);
}
代码应正确编译。但现在它显示“对“ EVP_get_digestbyname”的未定义引用”。
答案 0 :(得分:1)
您必须将库放在编译命令的末尾,如下所示:
gcc -I/home/share/pkp/include -L/home/share/pkp/lib -Wall -O3 -Wpedantic -Wno-unused-result -Wno-pointer-sign ot.c -o ot -lcrypto -lssl
您可以在这里知道原因:Why does the library linker flag sometimes have to go at the end using GCC?
我在机器上重现了错误,此更改解决了该问题。