Vcpkg: OpenSSL, curl: linking static libraries on Linux needs extra CMake linking arguments?

Created on 18 Sep 2018  路  8Comments  路  Source: microsoft/vcpkg

I have the following example files:

l.h:

#ifndef L_H
#define L_H

#include <openssl/asn1.h>

typedef struct
{
    ASN1_OBJECT *type;
    ASN1_OCTET_STRING *signature;
} TimeStampContentInfo;

DECLARE_ASN1_FUNCTIONS(TimeStampContentInfo);

int l_something();

#endif

l.c:

#include "l.h"

#include <curl/curl.h>
#include <openssl/evp.h>
#include <openssl/asn1t.h>

ASN1_SEQUENCE(TimeStampContentInfo) = {
    ASN1_SIMPLE(TimeStampContentInfo, type, ASN1_OBJECT),
    ASN1_EXP_OPT(TimeStampContentInfo, signature, ASN1_OCTET_STRING, 0)} ASN1_SEQUENCE_END(TimeStampContentInfo);
IMPLEMENT_ASN1_FUNCTIONS(TimeStampContentInfo);

int l_something()
{
    OpenSSL_add_all_algorithms();

    TimeStampContentInfo a;

    CURL *curl = curl_easy_init();
    curl_easy_cleanup(curl);

    return 0;
}

m.c:

#include "l.h"

int main()
{
    return l_something();
}

CMakeLists.txt:

cmake_minimum_required(VERSION 3.11)
project(dsign VERSION 1.3.0 LANGUAGES C)


add_library(libdsign l.c)
target_include_directories(libdsign
    PUBLIC 
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
    PRIVATE
        ${CMAKE_CURRENT_SOURCE_DIR}
)

find_package(OpenSSL REQUIRED)
target_link_libraries(libdsign PUBLIC OpenSSL::SSL OpenSSL::Crypto)

find_package(CURL REQUIRED)
target_link_libraries(libdsign PUBLIC ${CURL_LIBRARIES})
target_include_directories(libdsign PRIVATE ${CURL_INCLUDE_DIRS})


add_executable(dsign m.c)
target_link_libraries(dsign libdsign)

I installed openssl and curl through vcpkg (Fedora 28 x64).

Building the project nets me many "undefined reference" warnings:

$ rm -rf build && mkdir build && cmake -E chdir build cmake -GNinja -DCMAKE_TOOLCHAIN_FILE=/tmp/vcpkg/scripts/buildsystems/vcpkg.cmake .. && LC_ALL=C cmake --build build
-- The C compiler identification is GNU 8.1.1
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Found OpenSSL: /tmp/vcpkg/installed/x64-linux/debug/lib/libcrypto.a (found version "1.0.2p") 
-- Found CURL: /tmp/vcpkg/installed/x64-linux/lib/libcurl.a (found version "7.61.1-DEV") 
-- Configuring done
-- Generating done
-- Build files have been written to: /home/nikolaus/Entwicklung/vcpkg_test/build
[4/4] Linking C executable dsign
FAILED: dsign 
: && /usr/bin/cc    CMakeFiles/dsign.dir/m.c.o  -o dsign  liblibdsign.a /tmp/vcpkg/installed/x64-linux/debug/lib/libssl.a /tmp/vcpkg/installed/x64-linux/debug/lib/libcrypto.a -ldl /tmp/vcpkg/installed/x64-linux/lib/libcurl.a && :
/tmp/vcpkg/installed/x64-linux/lib/libcurl.a(version.c.o): In function `curl_version':
version.c:(.text+0x1ad): undefined reference to `zlibVersion'
/tmp/vcpkg/installed/x64-linux/lib/libcurl.a(version.c.o): In function `curl_version_info':
version.c:(.text+0x270): undefined reference to `zlibVersion'
/tmp/vcpkg/installed/x64-linux/lib/libcurl.a(content_encoding.c.o): In function `exit_zlib':
content_encoding.c:(.text+0x1e2): undefined reference to `inflateEnd'
/tmp/vcpkg/installed/x64-linux/lib/libcurl.a(content_encoding.c.o): In function `inflate_stream':
content_encoding.c:(.text+0x3d1): undefined reference to `inflate'
content_encoding.c:(.text+0x4ad): undefined reference to `inflateEnd'
content_encoding.c:(.text+0x4ca): undefined reference to `inflateInit2_'
/tmp/vcpkg/installed/x64-linux/lib/libcurl.a(content_encoding.c.o): In function `deflate_init_writer':
content_encoding.c:(.text+0x5e8): undefined reference to `inflateInit_'
/tmp/vcpkg/installed/x64-linux/lib/libcurl.a(content_encoding.c.o): In function `gzip_init_writer':
content_encoding.c:(.text+0x73a): undefined reference to `zlibVersion'
content_encoding.c:(.text+0x76a): undefined reference to `inflateInit2_'
content_encoding.c:(.text+0x7ac): undefined reference to `inflateInit2_'
/tmp/vcpkg/installed/x64-linux/lib/libcurl.a(curl_threads.c.o): In function `Curl_thread_create':
curl_threads.c:(.text+0x19e): undefined reference to `pthread_create'
/tmp/vcpkg/installed/x64-linux/lib/libcurl.a(curl_threads.c.o): In function `Curl_thread_destroy':
curl_threads.c:(.text+0x1fb): undefined reference to `pthread_detach'
/tmp/vcpkg/installed/x64-linux/lib/libcurl.a(curl_threads.c.o): In function `Curl_thread_join':
curl_threads.c:(.text+0x234): undefined reference to `pthread_join'
/tmp/vcpkg/installed/x64-linux/lib/libcurl.a(openssl.c.o): In function `tap_ssl_key':
openssl.c:(.text+0x101): undefined reference to `SSL_get_session'
/tmp/vcpkg/installed/x64-linux/lib/libcurl.a(openssl.c.o): In function `Curl_ossl_seed':
openssl.c:(.text+0x58f): undefined reference to `RAND_load_file'
openssl.c:(.text+0x735): undefined reference to `RAND_file_name'
openssl.c:(.text+0x754): undefined reference to `RAND_load_file'
/tmp/vcpkg/installed/x64-linux/lib/libcurl.a(openssl.c.o): In function `cert_stuff':
openssl.c:(.text+0xa11): undefined reference to `SSL_CTX_set_default_passwd_cb_userdata'
openssl.c:(.text+0xa27): undefined reference to `SSL_CTX_set_default_passwd_cb'
openssl.c:(.text+0xa74): undefined reference to `SSL_CTX_use_certificate_chain_file'
openssl.c:(.text+0xadd): undefined reference to `SSL_CTX_use_certificate_file'
openssl.c:(.text+0xcbb): undefined reference to `SSL_CTX_use_certificate'
openssl.c:(.text+0xe78): undefined reference to `PKCS12_parse'
openssl.c:(.text+0xef1): undefined reference to `SSL_CTX_use_certificate'
openssl.c:(.text+0xf4e): undefined reference to `SSL_CTX_use_PrivateKey'
openssl.c:(.text+0xf86): undefined reference to `SSL_CTX_check_private_key'
openssl.c:(.text+0xfec): undefined reference to `SSL_CTX_add_client_CA'
openssl.c:(.text+0x1036): undefined reference to `SSL_CTX_ctrl'
openssl.c:(.text+0x1176): undefined reference to `SSL_CTX_use_PrivateKey_file'
openssl.c:(.text+0x12e1): undefined reference to `ENGINE_load_private_key'
openssl.c:(.text+0x1330): undefined reference to `SSL_CTX_use_PrivateKey'
openssl.c:(.text+0x13f3): undefined reference to `SSL_new'
openssl.c:(.text+0x142c): undefined reference to `SSL_get_certificate'
openssl.c:(.text+0x145e): undefined reference to `SSL_get_privatekey'
openssl.c:(.text+0x1488): undefined reference to `SSL_get_privatekey'
openssl.c:(.text+0x14da): undefined reference to `SSL_free'
openssl.c:(.text+0x14ef): undefined reference to `SSL_CTX_check_private_key'
/tmp/vcpkg/installed/x64-linux/lib/libcurl.a(openssl.c.o): In function `Curl_ossl_init':
openssl.c:(.text+0x15f6): undefined reference to `OPENSSL_load_builtin_modules'
openssl.c:(.text+0x160f): undefined reference to `SSL_load_error_strings'
openssl.c:(.text+0x1614): undefined reference to `SSL_library_init'
/tmp/vcpkg/installed/x64-linux/lib/libcurl.a(openssl.c.o): In function `Curl_ossl_cleanup':
openssl.c:(.text+0x16f4): undefined reference to `SSL_COMP_free_compression_methods'
/tmp/vcpkg/installed/x64-linux/lib/libcurl.a(openssl.c.o): In function `Curl_ossl_set_engine_default':
openssl.c:(.text+0x192c): undefined reference to `ENGINE_set_default'
/tmp/vcpkg/installed/x64-linux/lib/libcurl.a(openssl.c.o): In function `ossl_close':
openssl.c:(.text+0x1a4d): undefined reference to `SSL_shutdown'
openssl.c:(.text+0x1a61): undefined reference to `SSL_set_connect_state'
openssl.c:(.text+0x1a75): undefined reference to `SSL_free'
openssl.c:(.text+0x1aa8): undefined reference to `SSL_CTX_free'
/tmp/vcpkg/installed/x64-linux/lib/libcurl.a(openssl.c.o): In function `Curl_ossl_shutdown':
openssl.c:(.text+0x1bad): undefined reference to `SSL_shutdown'
openssl.c:(.text+0x1c31): undefined reference to `SSL_read'
openssl.c:(.text+0x1c53): undefined reference to `SSL_get_error'
openssl.c:(.text+0x1da3): undefined reference to `SSL_free'
/tmp/vcpkg/installed/x64-linux/lib/libcurl.a(openssl.c.o): In function `Curl_ossl_session_free':
openssl.c:(.text+0x1dd8): undefined reference to `SSL_SESSION_free'
/tmp/vcpkg/installed/x64-linux/lib/libcurl.a(openssl.c.o): In function `verifystatus':
openssl.c:(.text+0x2537): undefined reference to `SSL_ctrl'
openssl.c:(.text+0x25b8): undefined reference to `OCSP_response_status'
openssl.c:(.text+0x25ce): undefined reference to `OCSP_response_status_str'
openssl.c:(.text+0x2609): undefined reference to `OCSP_response_get1_basic'
openssl.c:(.text+0x264f): undefined reference to `SSL_get_peer_cert_chain'
openssl.c:(.text+0x2669): undefined reference to `SSL_CTX_get_cert_store'
openssl.c:(.text+0x2686): undefined reference to `OCSP_basic_verify'
openssl.c:(.text+0x26d3): undefined reference to `OCSP_resp_get0'
openssl.c:(.text+0x2704): undefined reference to `OCSP_single_get0_status'
openssl.c:(.text+0x2726): undefined reference to `OCSP_check_validity'
openssl.c:(.text+0x275b): undefined reference to `OCSP_cert_status_str'
openssl.c:(.text+0x27a7): undefined reference to `OCSP_crl_reason_str'
openssl.c:(.text+0x27e1): undefined reference to `OCSP_resp_count'
/tmp/vcpkg/installed/x64-linux/lib/libcurl.a(openssl.c.o): In function `ssl_tls_trace':
openssl.c:(.text+0x2bf9): undefined reference to `SSL_alert_desc_string_long'
/tmp/vcpkg/installed/x64-linux/lib/libcurl.a(openssl.c.o): In function `get_ssl_version_txt':
openssl.c:(.text+0x2e75): undefined reference to `SSL_version'
/tmp/vcpkg/installed/x64-linux/lib/libcurl.a(openssl.c.o): In function `ossl_connect_step1':
openssl.c:(.text+0x35ab): undefined reference to `SSLv23_client_method'
openssl.c:(.text+0x35dc): undefined reference to `SSLv3_client_method'
openssl.c:(.text+0x362b): undefined reference to `SSL_CTX_free'
openssl.c:(.text+0x363f): undefined reference to `SSL_CTX_new'
openssl.c:(.text+0x36b8): undefined reference to `SSL_CTX_ctrl'
openssl.c:(.text+0x36f1): undefined reference to `SSL_CTX_set_msg_callback'
openssl.c:(.text+0x3718): undefined reference to `SSL_CTX_ctrl'
openssl.c:(.text+0x3952): undefined reference to `SSL_CTX_ctrl'
openssl.c:(.text+0x3985): undefined reference to `SSL_CTX_set_next_proto_select_cb'
openssl.c:(.text+0x3a2d): undefined reference to `SSL_CTX_set_alpn_protos'
openssl.c:(.text+0x3c5a): undefined reference to `SSL_CTX_set_cipher_list'
openssl.c:(.text+0x3cd0): undefined reference to `SSL_CTX_load_verify_locations'
openssl.c:(.text+0x3db2): undefined reference to `X509_LOOKUP_file'
openssl.c:(.text+0x3dc8): undefined reference to `SSL_CTX_get_cert_store'
openssl.c:(.text+0x3df9): undefined reference to `X509_load_crl_file'
openssl.c:(.text+0x3e51): undefined reference to `SSL_CTX_get_cert_store'
openssl.c:(.text+0x3ea1): undefined reference to `SSL_CTX_set_verify'
openssl.c:(.text+0x3f29): undefined reference to `SSL_free'
openssl.c:(.text+0x3f44): undefined reference to `SSL_new'
openssl.c:(.text+0x400f): undefined reference to `SSL_ctrl'
openssl.c:(.text+0x4023): undefined reference to `SSL_set_connect_state'
openssl.c:(.text+0x4096): undefined reference to `SSL_ctrl'
openssl.c:(.text+0x4188): undefined reference to `SSL_set_session'
openssl.c:(.text+0x423c): undefined reference to `BIO_f_ssl'
openssl.c:(.text+0x42c7): undefined reference to `SSL_set_bio'
openssl.c:(.text+0x42e2): undefined reference to `SSL_set_fd'
/tmp/vcpkg/installed/x64-linux/lib/libcurl.a(openssl.c.o): In function `ossl_connect_step2':
openssl.c:(.text+0x4415): undefined reference to `SSL_connect'
openssl.c:(.text+0x445e): undefined reference to `SSL_get_error'
openssl.c:(.text+0x4526): undefined reference to `SSL_get_verify_result'
openssl.c:(.text+0x4548): undefined reference to `X509_verify_cert_error_string'
openssl.c:(.text+0x4755): undefined reference to `SSL_get_current_cipher'
openssl.c:(.text+0x475d): undefined reference to `SSL_CIPHER_get_name'
openssl.c:(.text+0x47c7): undefined reference to `SSL_get0_alpn_selected'
/tmp/vcpkg/installed/x64-linux/lib/libcurl.a(openssl.c.o): In function `get_cert_chain':
openssl.c:(.text+0x4bea): undefined reference to `SSL_get_peer_cert_chain'
openssl.c:(.text+0x55b7): undefined reference to `PEM_write_bio_X509'
/tmp/vcpkg/installed/x64-linux/lib/libcurl.a(openssl.c.o): In function `servercert':
openssl.c:(.text+0x590c): undefined reference to `SSL_get_peer_certificate'
openssl.c:(.text+0x5ec0): undefined reference to `PEM_read_bio_X509'
openssl.c:(.text+0x6150): undefined reference to `SSL_get_verify_result'
openssl.c:(.text+0x61fb): undefined reference to `X509_verify_cert_error_string'
openssl.c:(.text+0x6235): undefined reference to `X509_verify_cert_error_string'
/tmp/vcpkg/installed/x64-linux/lib/libcurl.a(openssl.c.o): In function `ossl_connect_step3':
openssl.c:(.text+0x653d): undefined reference to `SSL_get1_session'
openssl.c:(.text+0x6621): undefined reference to `SSL_SESSION_free'
/tmp/vcpkg/installed/x64-linux/lib/libcurl.a(openssl.c.o): In function `Curl_ossl_data_pending':
openssl.c:(.text+0x6b62): undefined reference to `SSL_pending'
openssl.c:(.text+0x6b92): undefined reference to `SSL_pending'
/tmp/vcpkg/installed/x64-linux/lib/libcurl.a(openssl.c.o): In function `ossl_send':
openssl.c:(.text+0x6c45): undefined reference to `SSL_write'
openssl.c:(.text+0x6c6b): undefined reference to `SSL_get_error'
/tmp/vcpkg/installed/x64-linux/lib/libcurl.a(openssl.c.o): In function `ossl_recv':
openssl.c:(.text+0x6f06): undefined reference to `SSL_read'
openssl.c:(.text+0x6f33): undefined reference to `SSL_get_error'
/tmp/vcpkg/installed/x64-linux/lib/libcurl.a(openssl.c.o): In function `Curl_ossl_version':
openssl.c:(.text+0x7026): undefined reference to `SSLeay'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

I have to explicitly link all these like this:

cmake_minimum_required(VERSION 3.11)
project(dsign VERSION 1.3.0 LANGUAGES C)


add_library(libdsign l.c)
target_include_directories(libdsign
    PUBLIC 
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
    PRIVATE
        ${CMAKE_CURRENT_SOURCE_DIR}
)

find_package(ZLIB REQUIRED)
find_package(Threads)

find_package(OpenSSL REQUIRED)
target_link_libraries(libdsign PUBLIC OpenSSL::SSL OpenSSL::Crypto)

find_package(CURL REQUIRED)
target_link_libraries(libdsign PUBLIC ${CURL_LIBRARIES})
target_include_directories(libdsign PRIVATE ${CURL_INCLUDE_DIRS})


add_executable(dsign m.c)
target_link_libraries(dsign libdsign ${CURL_LIBRARIES} OpenSSL::SSL OpenSSL::Crypto ZLIB::ZLIB Threads::Threads)

Is this expected or am I doing something wrong? It works without this on Windows.

port-bug

Most helpful comment

https://github.com/Microsoft/vcpkg/commit/ee325d14276e7bd7961e94e58293b02d6e9e92da makes it work, thanks :) 馃憤 for merging.

All 8 comments

https://github.com/Microsoft/vcpkg/commit/ee325d14276e7bd7961e94e58293b02d6e9e92da makes it work, thanks :) 馃憤 for merging.

Thanks for reporting the issue!

Thanks for fixing it :) when will the fix be merged?

Note that even after adding OpenSSL, ZLIB and Threads there are some missing dependencies (crypt32, and possibly wsock32 and ws2_32). The following program and CMake file can be used to reproduce this problem. Compile with -DADD_MISSING_DEPENDENCIES=yes to get a successful build.

#include <iostream>
#include <curl/curl.h>

int main() {
  auto vinfo = curl_version_info(CURLVERSION_NOW);
  std::cout << "Running with SSL=" << vinfo->ssl_version << std::endl;

  CURL *curl = curl_easy_init();
  if (curl == nullptr) {
    std::cerr << "Error creating CURL handle" << std::endl;
    return 1;
  }
  curl_easy_setopt(curl, CURLOPT_URL, "https://www.nghttp2.org/httpbin/");
  CURLcode res = curl_easy_perform(curl);
  curl_easy_cleanup(curl);

  return 0;
}
cmake_minimum_required(VERSION 3.5)

project(link-curl CXX C)

# Configure the Compiler options, we will be using C++11 features.
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(OpenSSL REQUIRED)
find_package(ZLIB REQUIRED)
find_package(Threads REQUIRED)

find_package(CURL REQUIRED)
if (NOT TARGET CURL::CURL)
    add_library(CURL::CURL UNKNOWN IMPORTED)
    set_property(TARGET CURL::CURL
                 APPEND
                 PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${CURL_INCLUDE_DIR}")
    set_property(TARGET CURL::CURL
                 APPEND
                 PROPERTY IMPORTED_LOCATION "${CURL_LIBRARY}")
    set_property(TARGET CURL::CURL
            APPEND
            PROPERTY INTERFACE_LINK_LIBRARIES
            OpenSSL::SSL
            OpenSSL::Crypto
            ZLIB::ZLIB
            Threads::Threads)

    message("CURL_LIBRARY=${CURL_LIBRARY}")
    message("CULR_LIBRARIES=${CURL_LIBRARIES}")
endif ()

add_executable(link_curl link_curl.cc)
target_link_libraries(link_curl
                      PRIVATE CURL::CURL)

if (ADD_MISSING_DEPENDENCIES)
    if ("${CURL_LIBRARY}" MATCHES "${CMAKE_STATIC_LIBRARY_SUFFIX}$")
        if (WIN32)
            set_property(TARGET CURL::CURL
                    APPEND
                    PROPERTY INTERFACE_LINK_LIBRARIES
                    crypt32 wsock32 ws2_32)
        endif ()
    endif ()
endif ()

Hi @madig and @coryan does this issue still exist on your local now?

Just tried it with git master packages and it seems to work.

Works for me using the current master for vcpkg, thanks for following up.

Thanks for your feedback. Please open a new issue if you have any other problems.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cjvaijo picture cjvaijo  路  3Comments

pkeir picture pkeir  路  3Comments

pakdel picture pakdel  路  3Comments

jack17529 picture jack17529  路  3Comments

angelmixu picture angelmixu  路  3Comments