androidc++cmake

Using libcurl in C++ Android application using CMake


Maybe there are already some answers on this forum about this problem, but I tried a lot of solutions already, and I still didn't solve this problem.

I have to make an Android application using C++, which uses libcurl.

And whatever I do, I can't run my program because it doesn't find the library.

In my .cpp, I use this line:

#include <curl/curl.h>

And this is my 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} )

I used the commands:

sudo aptitude install libcurl4-gnutls-dev

and

sudo aptitude install libcurl-dev


Solution

  • You should rather use the built-in functionality to integrate 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})
    

    As it seems you are cross-compiling for Android, the following will make the curl library that you installed for your Ubuntu available for the compilation (add it before what is above).

    set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)
    set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE BOTH)
    set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE BOTH)
    

    But it might then be problematic in the linking step, since you installed the library for your Ubuntu and the program you want to compile it with is for Android. If this does not work, try to have the curl library in your cross-compiling toolset (but I don't know enough about it to tell you how to do it).