将CURL作为静态库添加到C ++ CMake项目中
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将CURL作为静态库添加到C ++ 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()
以上是关于将CURL作为静态库添加到C ++ CMake项目中的主要内容,如果未能解决你的问题,请参考以下文章
如何将 TensorFlow Lite 构建为静态库并从单独的(CMake)项目链接到它?
cmake:arm-xm-linux交叉编译工具链文件及交叉支持HTTPS的curl静态库