c++ - 将 CURL 作为静态库添加到 C++ CMake 项目

标签 c++ linux curl cmake

我在将 CURL 库添加到 CMake 项目时遇到问题。在我的 CMakeList.txt 中,我有以下几行用于添加 CURL:

    #option(CURL_STATICLIB "Set to ON to build libcurl with static linking."  ON)
if(WIN32)
    add_definitions("-DCURL_STATICLIB")
endif()
option(LIBCURL_ENABLE "Enable or disable the requirement of libcurl" ON)

if(LIBCURL_ENABLE)
    find_path(LCURL_INCLUDE_DIR
        NAMES
            curl.h
        PATHS
            /usr/include/curl
            ENV "PROGRAMFILES(X86)"
            ENV "LIBCURL_ROOT"
        PATH_SUFFIXES
            include)

    find_library(LCURL
        NAMES
            libcurl.a
            libcurl.lib
        PATHS
            /usr/lib/x86_64-linux-gnu
            /usr
        PATH_SUFFIXES
            lib
            lib/x86_64-linux-gnu)
    if(LCURL STREQUAL "LCURL-NOTFOUND")
        message(FATAL_ERROR "libcurl NOT found: use `-DLIBCURL_ENABLE=OFF` to build without libcurl support")
    else()
        set(LIBS ${LIBS} ${LCURL})
        include_directories(AFTER ${LCURL_INCLUDE_DIR})
    endif()
else()
    add_definitions("-DCONF_NO_LIBCURL")
endif()
...

if(LIBCURL_ENABLE)
    target_link_libraries(app ${ZLIB_LIBRARIES} ${LCURL})
endif()

在 Windows 上一切正常。但是在 Linux 上,我从 make install 命令中得到了这条消息:

/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libcurl.a(libcurl_la-content_encoding.o): undefined reference to symbol 'inflateInit2_'
/usr/lib/x86_64-linux-gnu/libz.so: error adding symbols: DSO missing from command line

最佳答案

通过以下替换解决了编译问题。

#option(CURL_STATICLIB "Set to ON to build libcurl with static linking."  ON)
if(WIN32)
    add_definitions("-DCURL_STATICLIB")
endif()
set(CURL_LIBRARY "-lcurl") 
find_package(CURL REQUIRED) 
include_directories(${CURL_INCLUDE_DIR})

if(LIBCURL_ENABLE)
    target_link_libraries(app ${CURL_LIBRARIES})
endif()

关于c++ - 将 CURL 作为静态库添加到 C++ CMake 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50783730/

相关文章:

php - 重定向 htaccess 或 php?

linux - 如何编译要使用 Gprof 进行分析的驱动程序

c++ - 在 C++ 中使用 vector 合并排序

c++ 映射删除函数不能与迭代器一起正常工作

c - 如何更改 makefile 以避免对数学库中函数的 undefined reference ?

linux - unix FTPS cURL 目录创建失败

c++ - Windows SChannel 支持 CURL SFTP 吗?

curl - 如何使用 cURL 调试 CORS 请求?

c++ - 从 GtkWidget 获取 X11 窗口句柄

java - 如何用其他语言编写带有绑定(bind)的Qt插件系统?