gpt4 book ai didi

android - 使用 CMake 在 C++ android 应用程序中使用 libcurl

转载 作者:太空狗 更新时间:2023-10-29 13:54:03 25 4
gpt4 key购买 nike

也许这个论坛上已经有一些关于这个问题的答案,但我已经尝试了很多解决方案,但我仍然没有解决这个问题。

我必须使用 C++ 制作一个 android 应用程序,它使用 libcurl。

无论我做什么,我都无法运行我的程序,因为它确实找不到库。

在我的 .cpp 中,我使用了这一行:

#include <curl/curl.h>

这是我的 CMakeLists.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 库(但我对它的了解还不够多,无法告诉您如何操作)。

关于android - 使用 CMake 在 C++ android 应用程序中使用 libcurl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41633600/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com