【问题标题】:Using libcurl in C++ android application using CMake在使用 CMake 的 C++ android 应用程序中使用 libcurl
【发布时间】:2017-01-13 11:26:41
【问题描述】:

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

我必须使用 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

【问题讨论】:

    标签: android c++ cmake


    【解决方案1】:

    您应该使用内置功能来集成 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 库(但我对此了解不多,无法告诉您如何操作)。

    【讨论】:

    • 嗨,谢谢你的回答当我使用这个时,我得到这个错误:找不到CURL(缺少:CURL_LIBRARY CURL_INCLUDE_DIR)
    • 你能在上面试试set(CURL_INCLUDE_DIR "/usr/include")吗(如果“curl/curl.h”在那里)?
    • 好吧,我认为这是一个解决方案,因为现在我只得到了这个:找不到 CURL (missing: CURL_LIBRARY) (found version "7.47.0")
    • @SamyLeGalloudec 您将问题标记为 Android,您是否在交叉编译?如果你这样做,这可能需要修改。
    • 我正在用我的电脑为安卓智能手机编写一个应用程序。不知道是不是exaclty交叉编译(见识少见见谅..)
    【解决方案2】:

    我认为您需要指示 cmake 查找 libcurl 依赖项。
    请阅读How_To_Find_Libraries
    看看find_packageFindCURL module

    作为快速入门,将以下内容添加到您的 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())。

    【讨论】:

    • 您好,感谢您的回答。就像我对 oLen 说的一样,我真的不知道我必须放入什么 (${CURL_INCLUDE_DIRS}) 我应该放入 ${/usr/include/curl} 还是直接 /usr/include...?对不起,如果我问初学者问题,我第一次做 CMakeFile
    • 只要准确地写“${CURL_INCLUDE_DIRS}”,它是一个由 find_package() 填充的变量,如果找到 libcurl(即像你的情况一样安装为 Linux 包),请阅读我的链接包含在我的答案中以获取更多详细信息。
    • 当我完全按照你说的那样做时,我得到了这个错误:找不到 CURL (missing: CURL_LIBRARY CURL_INCLUDE_DIR) 你认为它来自安装吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-02
    • 2016-10-27
    • 1970-01-01
    • 2012-02-11
    • 1970-01-01
    • 2017-03-16
    • 1970-01-01
    相关资源
    最近更新 更多