При компиляции без указания либ выдаёт следующее:
C:\Users\admin\AppData\Local\Temp\ccWyoIvB.o:a.c:(.text+0x1d): undefined reference to `ERR_print_errors_fp'
C:\Users\admin\AppData\Local\Temp\ccWyoIvB.o:a.c:(.text+0x34): undefined reference to `SSL_load_error_strings'
C:\Users\admin\AppData\Local\Temp\ccWyoIvB.o:a.c:(.text+0x39): undefined reference to `SSL_library_init'
C:\Users\admin\AppData\Local\Temp\ccWyoIvB.o:a.c:(.text+0x4d): undefined reference to `SSL_CTX_free'
C:\Users\admin\AppData\Local\Temp\ccWyoIvB.o:a.c:(.text+0x58): undefined reference to `BIO_free_all'
C:\Users\admin\AppData\Local\Temp\ccWyoIvB.o:a.c:(.text+0x69): undefined reference to `TLSv1_2_client_method'
C:\Users\admin\AppData\Local\Temp\ccWyoIvB.o:a.c:(.text+0x89): undefined reference to `SSL_CTX_new'
C:\Users\admin\AppData\Local\Temp\ccWyoIvB.o:a.c:(.text+0xa9): undefined reference to `BIO_new_ssl_connect'
C:\Users\admin\AppData\Local\Temp\ccWyoIvB.o:a.c:(.text+0x112): undefined reference to `BIO_ctrl'
C:\Users\admin\AppData\Local\Temp\ccWyoIvB.o:a.c:(.text+0x138): undefined reference to `SSL_ctrl'
C:\Users\admin\AppData\Local\Temp\ccWyoIvB.o:a.c:(.text+0x15d): undefined reference to `BIO_ctrl'
C:\Users\admin\AppData\Local\Temp\ccWyoIvB.o:a.c:(.text+0x180): undefined reference to `BIO_ctrl'
C:\Users\admin\AppData\Local\Temp\ccWyoIvB.o:a.c:(.text+0x1bd): undefined reference to `SSL_CTX_load_verify_locations'
C:\Users\admin\AppData\Local\Temp\ccWyoIvB.o:a.c:(.text+0x1db): undefined reference to `SSL_get_verify_result'
C:\Users\admin\AppData\Local\Temp\ccWyoIvB.o:a.c:(.text+0x235): undefined reference to `BIO_puts'
C:\Users\admin\AppData\Local\Temp\ccWyoIvB.o:a.c:(.text+0x270): undefined reference to `BIO_read'
collect2.exe: error: ld returned 1 exit status
При добавлении -lssl:
C:\Users\admin\AppData\Local\Temp\cc2363zP.o:a.c:(.text+0x1d): undefined reference to `ERR_print_errors_fp'
C:\Users\admin\AppData\Local\Temp\cc2363zP.o:a.c:(.text+0x34): undefined reference to `SSL_load_error_strings'
C:\Users\admin\AppData\Local\Temp\cc2363zP.o:a.c:(.text+0x39): undefined reference to `SSL_library_init'
C:\Users\admin\AppData\Local\Temp\cc2363zP.o:a.c:(.text+0x58): undefined reference to `BIO_free_all'
C:\Users\admin\AppData\Local\Temp\cc2363zP.o:a.c:(.text+0x69): undefined reference to `TLSv1_2_client_method'
C:\Users\admin\AppData\Local\Temp\cc2363zP.o:a.c:(.text+0x112): undefined reference to `BIO_ctrl'
C:\Users\admin\AppData\Local\Temp\cc2363zP.o:a.c:(.text+0x15d): undefined reference to `BIO_ctrl'
C:\Users\admin\AppData\Local\Temp\cc2363zP.o:a.c:(.text+0x180): undefined reference to `BIO_ctrl'
C:\Users\admin\AppData\Local\Temp\cc2363zP.o:a.c:(.text+0x235): undefined reference to `BIO_puts'
C:\Users\admin\AppData\Local\Temp\cc2363zP.o:a.c:(.text+0x270): undefined reference to `BIO_read'
collect2.exe: error: ld returned 1 exit status
При добавлении -lcrypto и -lssl:
C:\Users\admin\AppData\Local\Temp\ccSOQAah.o:a.c:(.text+0x34): undefined reference to `SSL_load_error_strings'
C:\Users\admin\AppData\Local\Temp\ccSOQAah.o:a.c:(.text+0x39): undefined reference to `SSL_library_init'
C:\Users\admin\AppData\Local\Temp\ccSOQAah.o:a.c:(.text+0x69): undefined reference to `TLSv1_2_client_method'
collect2.exe: error: ld returned 1 exit status
Компилятор: MinGw
Когда пробывал через cmake, точно также было.
Какие библиотеки нужно указать, чтоб произошла линковка?
Код:
gcc a.c -lssl -lcrypto
#include <stdio.h>
#include <stdlib.h>
#include <openssl/bio.h>
#include <openssl/err.h>
#include <openssl/ssl.h>
#define BuffSize 1024
void report_and_exit(const char* msg) {
perror(msg);
ERR_print_errors_fp(stderr);
exit(-1);
}
void init_ssl() {
SSL_load_error_strings();
SSL_library_init();
}
void cleanup(SSL_CTX* ctx, BIO* bio) {
SSL_CTX_free(ctx);
BIO_free_all(bio);
}
void secure_connect(const char* hostname) {
char name[BuffSize];
char request[BuffSize];
char response[BuffSize];
const SSL_METHOD* method = TLSv1_2_client_method();
if (NULL == method) report_and_exit("TLSv1_2_client_method...");
SSL_CTX* ctx = SSL_CTX_new(method);
if (NULL == ctx) report_and_exit("SSL_CTX_new...");
BIO* bio = BIO_new_ssl_connect(ctx);
if (NULL == bio) report_and_exit("BIO_new_ssl_connect...");
SSL* ssl = NULL;
sprintf(name, "%s:%s", hostname, "https");
BIO_get_ssl(bio, &ssl);
SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
BIO_set_conn_hostname(bio, name);
if (BIO_do_connect(bio) <= 0) {
cleanup(ctx, bio);
report_and_exit("BIO_do_connect...");
}
if (!SSL_CTX_load_verify_locations(ctx,
"/etc/ssl/certs/ca-certificates.crt",
"/etc/ssl/certs/"))
report_and_exit("SSL_CTX_load_verify_locations...");
long verify_flag = SSL_get_verify_result(ssl);
if (verify_flag != X509_V_OK)
fprintf(stderr,
"##### Certificate verification error (%i) but continuing...\n",
(int) verify_flag);
sprintf(request,
"GET / HTTP/1.1\x0D\x0AHost: %s\x0D\x0A\x43onnection: Close\x0D\x0A\x0D\x0A",
hostname);
BIO_puts(bio, request);
while (1) {
memset(response, '\0', sizeof(response));
int n = BIO_read(bio, response, BuffSize);
if (n <= 0) break;
puts(response);
}
cleanup(ctx, bio);
}
int main() {
init_ssl();
const char* hostname = "www.google.com:443";
fprintf(stderr, "Trying an HTTPS connection to %s...\n", hostname);
secure_connect(hostname);
return 0;
}