简体   繁体   中英

Adding CURL as static library to a C++ CMake project

I have a problem with adding CURL library to CMake project. In my CMakeList.txt I have the following lines for adding 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()

Everything is okay on Windows. But on Linux, I got this message from the make install command:

/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

The compilation was resolved with the following replacement.

#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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM