使用 CMake 在 android 应用程序中C++ libcurl
Using libcurl in C++ android application using CMake
也许这个论坛上已经有一些关于这个问题的答案,但是我已经尝试了很多解决方案,但我仍然没有解决这个问题。
我必须使用C++制作一个Android应用程序,而使用libcurl。
无论我做什么,我都无法运行我的程序,因为它找不到库。
在我的.cpp中,我使用这一行:
#include <curl/curl.h>
这是我的CMakeList.txt
# Sets the minimum version of CMake required to build the native
# library. You should either keep the default value or only pass a
# value of 3.4.0 or lower.
cmake_minimum_required(VERSION 3.4.1)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds it for you.
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
# Associated headers in the same location as their source
# file are automatically included.
src/main/cpp/native-lib.cpp )
# Searches for a specified prebuilt library and stores the path as a
# variable. Because system libraries are included in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
-lcurl
# Specifies the name of the NDK library that
# you want CMake to locate.
log
-lcurl )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in the
# build script, prebuilt third-party libraries, or system libraries.
INCLUDE_DIRECTORIES($/usr/include/)
target_link_libraries( # Specifies the target library.
native-lib
-lcurl
# Links the target library to the log library
# included in the NDK.
${log-lib} )
我使用了命令:
sudo aptitude install libcurl4-gnutls-dev
和
sudo aptitude install libcurl-dev
你应该使用内置功能来集成libcurl:
[...]
find_package(CURL REQUIRED)
include_directories(${CURL_INCLUDE_DIRS})
add_library(native-lib SHARED src/main/cpp/native-lib.cpp)
target_link_libraries(native-lib ${CURL_LIBRARIES})
由于您似乎正在为 Android 进行交叉编译,以下内容将使您为 Ubuntu 安装的 curl 库可用于编译(将其添加到上述内容之前)。
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE BOTH)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE BOTH)
但是在链接步骤中可能会出现问题,因为您为Ubuntu安装了库,并且要编译它的程序适用于Android。如果这不起作用,请尝试将 curl 库放在交叉编译工具集中(但我对它的了解不足以告诉您如何操作)。
我认为你需要指示 cmake 找到 libcurl 依赖项。
请阅读How_To_Find_Libraries。
看看find_package和FindCURL模块
作为快速入门,请将以下内容添加到 CMakeLists.txt:
find_package(CURL REQUIRED)
include_directories(${CURL_INCLUDE_DIRS})
add_library(native-lib SHARED ... )
target_link_libraries(native-lib ${CURL_LIBRARIES})
(更好的是:使用 target_include_directories() 而不是 include_directories())。
相关文章:
- 发送一个带有libcurl C++问题的帖子请求:s
- 使用libcurl提交批量url的正确BING Api POST url是什么
- 在 libcurl 连接池中预创建连接
- libcurl 和 DNS ttl 中的内部连接管理
- 如何在 cmake 库中包含 libcurl
- 如何应用 libcurl 的持久连接选项
- VS 2015 链接错误 无法构建依赖于 libcurl 的项目
- 组合字符串不适用于 libCurl,C++
- Poloniex API "Invalid command" c++ libcurl
- libcurl :显示正在运行的上传和下载速率
- c++ libCurl :如何使用libCurl接受过期的证书
- libcurl :C++处理多个异步请求
- 现代C++的libcurl和JSON问题
- 使用 Libcurl 库进行文件下载
- 如何在C++生成器中使用libcurl
- Libcurl c++ "undefined reference to" (Windows/MinGW/g++)
- 如何使用 libcurl 在本地 IP 上获取 HTML?
- 如何将带有 -F 选项的 curl 命令转换为 libcurl
- 如何使用 libCurl 将访问令牌发送到服务器 API
- C++ libcurl - 无法从 URL 检索整个 html 内容