Я собираю некоторые коды на основе 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 из загруженного исходного кода конфликтует с openssl, установленным "apt-get". Поэтому я попробую другой метод.
./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 Я следую решению в здесь , пытаясь статически связать 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».