使用CMake链接静态cURL时出错

时间:2018-05-23 20:31:58

标签: curl cmake

我正在尝试构建使用cURL库的示例程序。不幸的是,我在链接过程中遇到了很多错误。

系统:Linux-x86_64,GCC版本7.2.2

1)因为需要SSL支持,所以我建立了OpenSSL库。我下载了openssl-1.1.1-pre5.tar.gz,解压缩并配置它以进行静态构建:

$ ./Configure --prefix="${HOME}/Source/openssl-1.1.1-pre5" no-shared linux-x86_64

然后

$ make VERBOSE=1
$ make install

2)接下来,我准备了ZLib库:下载了zlib-1.2.11.tar.gz,解压缩并配置它以进行静态构建:

$ ./configure --prefix="${HOME}/Source/zlib-1.2.11" --static

然后

$ make VERBOSE=1
$ make install

3)然后我构建了cURL,也作为静态库:

$ ./configure --prefix="${HOME}/Source/curl-7.59.0" --with-ssl="${HOME}/Source/openssl-1.1.1-pre5" --with-zlib="${HOME}/Source/zlib-1.2.11" --enable-static --disable-shared

...   配置摘要:

  curl version:     7.59.0
  Host setup:       x86_64-pc-linux-gnu
  Install prefix:   /home/sergei/Source/curl-7.59.0
  Compiler:         gcc
  SSL support:      enabled (OpenSSL)
  SSH support:      no      (--with-libssh2)
  zlib support:     enabled
  brotli support:   no      (--with-brotli)
  GSS-API support:  no      (--with-gssapi)
  TLS-SRP support:  enabled
  resolver:         POSIX threaded
  IPv6 support:     enabled
  Unix sockets support: enabled
  IDN support:      no      (--with-{libidn2,winidn})
  Build libcurl:    Shared=no, Static=yes
  Built-in manual:  enabled
  --libcurl option: enabled (--disable-libcurl-option)
  Verbose errors:   enabled (--disable-verbose)
  SSPI support:     no      (--enable-sspi)
  ca cert bundle:   /etc/ssl/certs/ca-certificates.crt
  ca cert path:     no
  ca fallback:      no
  LDAP support:     no      (--enable-ldap / --with-ldap-lib / --with-lber-lib)
  LDAPS support:    no      (--enable-ldaps)
  RTSP support:     enabled
  RTMP support:     no      (--with-librtmp)
  metalink support: no      (--with-libmetalink)
  PSL support:      no      (libpsl not found)
  HTTP2 support:    disabled (--with-nghttp2)
  Protocols:        DICT FILE FTP FTPS GOPHER HTTP HTTPS IMAP IMAPS POP3 POP3S RTSP SMB SMBS SMTP SMTPS TELNET TFTP

然后

$ make VERBOSE=1
$ make install

在构建完成后,我在$ {HOME} /Source/curl-7.59.0中有cURL目录结构:

/bin
/include
/lib    <-- here are libcurl.a and libcurl.la 
/share

4)接下来我准备了一个例子,我从simple.c得到了(来自/ docs / examples的cURL示例):

#include <iostream>
#include <stdio.h>

#define CURL_STATICLIB
#include <curl/curl.h>    

int main(int argc, char** argv) 
{
    CURL *curl;
    CURLcode res;

    curl = curl_easy_init();

    if(curl) 
    {
        curl_easy_setopt(curl, CURLOPT_URL, "http://rambler.ru");

        /* example.com is redirected, so we tell libcurl to follow redirection */
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

        /* Perform the request, res will get the return code */
        res = curl_easy_perform(curl);

        /* Check for errors */
        if(res != CURLE_OK)
          fprintf(stderr, "curl_easy_perform() failed: %s\n",
                  curl_easy_strerror(res));

        /* always cleanup */
        curl_easy_cleanup(curl);
    }

    return 0; 
}

写了CMakeLists.txt:

cmake_minimum_required(VERSION 2.8) 

project(curl_example) 

set(SOURCE_EXE main.cpp)

add_library(curl_library STATIC IMPORTED) 

set_target_properties(curl_library PROPERTIES IMPORTED_LOCATION 
                      "/home/sergei/Source/curl-7.59.0/lib/libcurl.a")

include_directories("/home/sergei/Source/curl-7.59.0/include")


add_executable(main ${SOURCE_EXE})

target_link_libraries(main curl_library) 

当我尝试构建时,我遇到了很多错误(在“[100%]链接CXX可执行文件主”之后),如下所示:

/home/sergei/Source/curl-7.59.0/lib/libcurl.a(libcurl_la-version.o): In function `curl_version.part.0':
version.c:(.text+0x68): undefined reference to `zlibVersion'
/home/sergei/Source/curl-7.59.0/lib/libcurl.a(libcurl_la-version.o): In function `curl_version_info.part.1':
version.c:(.text+0xde): undefined reference to `zlibVersion'
/home/sergei/Source/curl-7.59.0/lib/libcurl.a(libcurl_la-content_encoding.o): In function `gzip_init_writer':
content_encoding.c:(.text+0x230): undefined reference to `zlibVersion'
content_encoding.c:(.text+0x266): undefined reference to `inflateInit2_'
content_encoding.c:(.text+0x289): undefined reference to `inflateInit2_'
/home/sergei/Source/curl-7.59.0/lib/libcurl.a(libcurl_la-content_encoding.o): In function `exit_zlib':
content_encoding.c:(.text+0x31c): undefined reference to `inflateEnd'
/home/sergei/Source/curl-7.59.0/lib/libcurl.a(libcurl_la-content_encoding.o): In function `inflate_stream':
content_encoding.c:(.text+0x435): undefined reference to `inflate'
content_encoding.c:(.text+0x4eb): undefined reference to `inflateEnd'
content_encoding.c:(.text+0x504): undefined reference to `inflateInit2_'

并且像这样:

openssl.c:(.text+0x4ae): undefined reference to `SSL_get_error'
openssl.c:(.text+0x4c3): undefined reference to `ERR_get_error'
openssl.c:(.text+0x553): undefined reference to `ERR_error_string_n'
/home/sergei/Source/curl-7.59.0/lib/libcurl.a(libcurl_la-openssl.o): In function `ssl_ui_writer':
openssl.c:(.text+0x8e0): undefined reference to `UI_get_string_type'
openssl.c:(.text+0x8f0): undefined reference to `UI_get0_user_data'
openssl.c:(.text+0x8fd): undefined reference to `UI_get_input_flags'
openssl.c:(.text+0x906): undefined reference to `UI_OpenSSL'
openssl.c:(.text+0x90e): undefined reference to `UI_method_get_writer'

你能不能帮助我:我做错了什么?

P.S。:在CMakeLists.txt中添加“add_definitions(-DCURL_STATICLIB)”行无效。

感谢。

1 个答案:

答案 0 :(得分:0)

在CMakelists.txt中添加一行:

link_libraries(z)

ref:https://blog.csdn.net/bright789/article/details/84523298