CURL package not found with static libraries

Hello,

I need to build a standalone executable which uses MAVSDK and therefore I want to build MAVSDK as static library. But when I set the flag BUILD_SHARED_LIBS=OFF with

cmake -DCMAKE_BUILD_TYPE=Debug -DBUILD_SHARED_LIBS=OFF -DENABLE_MAVLINK_PASSTHROUGH=ON -DBUILD_TEST=ON -Bbuild -H.

I finish with the following error when I build my application:

CMake Error at /usr/share/cmake-3.13/Modules/CMakeFindDependencyMacro.cmake:48 (find_package):
  Could not find a package configuration file provided by "CURL" with any of
  the following names:

    CURLConfig.cmake
    curl-config.cmake

  Add the installation prefix of "CURL" to CMAKE_PREFIX_PATH or set
  "CURL_DIR" to a directory containing one of the above files.  If "CURL"
  provides a separate development package or SDK, be sure it has been
  installed.
Call Stack (most recent call first):
  /usr/local/lib/cmake/MAVSDK/MAVSDKConfig.cmake:7 (find_dependency)
  CMakeLists.txt:14 (find_package)

I finish with the same error with MAVSDK 0.33.1 and with develop branch. I saw a similar issue here but when enabling shared library.

I’m missing something @JulianOes, @JonasVautherin ?

I too have same problem. I build mavsdk as static libs and compile with my application. mavsdk static libs build fine but later when I include them into my app, during my app compilation, getting curl undefined reference errors. Should I inlcude curl lib into my makefile? I think the mavsdk static lib would have already statically linked curl lib?

You’re missing some defines

-DCMAKE_INSTALL_PREFIX=install -DDEPS_INSTALL_PATH=install

Hi,

thank you for the answer. I updated with the given flag

cmake -DCMAKE_BUILD_TYPE=Debug -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX=install -DDEPS_INSTALL_PATH=install -DENABLE_MAVLINK_PASSTHROUGH=ON -DBUILD_TEST=ON -Bbuild -H.

but It still fails with the missing dependency of CURL. Another proposition on slack is

to link curl (and probably the other dependencies) in your project as well, since MAVSDK depends on them

I’ll try this a soon as possible and give an update here

Hi @dakejahl,

now I get your point (sorry I’m discovering CMake building environment). So when I add the flags -DCMAKE_INSTALL_PREFIX=install -DDEPS_INSTALL_PATH=install and after building I have an install folder with all the dependencies and static libraries of MAVSDK. How should I integrate that into my project with CMake now ?

Because I didn’t catch your point, I started to git all dependencies in my project (jsoncpp, curl, tinyxml2) to be able to use MAVSDK as static library. What is the best practice here ?

My thought would be to integrate MAVSDK as a third party and let it integrate all the libs it requires with the flags you provide me but I’m not sure how to proceed. Any guidance would be really appreciated

So to finish my monologue :joy:

I managed to compile MAVSDK and all its dependencies. I failed to build a static executable because it crahes when I try to create a Mavsdk instance. But now that everything is downloaded and compiled locally I can survivre without a static executable…

To give some hints to anyone trying the same, I have almost copy/paste what MAVSDK does for its libraries, so with a file CMakeLists.txt like below (in third_party/MAVSDK folder):

cmake_minimum_required(VERSION 3.1)

project(external-MAVSDK)
include(ExternalProject)

list(APPEND CMAKE_ARGS
    "-DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_INSTALL_PREFIX}"
    "-DDEPS_INSTALL_PATH=${CMAKE_INSTALL_PREFIX}"
    "-DCMAKE_POSITION_INDEPENDENT_CODE=ON"
    "-DCMAKE_BUILD_TYPE=Release"
    "-DBUILD_SHARED_LIBS=ON"
    "-DENABLE_MAVLINK_PASSTHROUGH=ON"
    "-DBUILD_TEST=ON"
    "-Bbuild"
    "-H.")

message(STATUS "Preparing external project \"MAVSDK\" with args:")
foreach(CMAKE_ARG ${CMAKE_ARGS})
    message(STATUS "-- ${CMAKE_ARG}")
endforeach()

ExternalProject_Add(
    MAVSDK
    GIT_REPOSITORY https://github.com/mavlink/MAVSDK.git
    GIT_TAG v0.35.1
    PREFIX MAVSDK
    CMAKE_ARGS "${CMAKE_ARGS}"
    )

This probably isn’t best practice, but since I am modifying my version of MAVSDK this is the best way for me. To include statically I create a lib/ directory in my projects root folder structure and put my compiled mavsdk folder in it (the folder that ends up in install/)

My top level cmake has this

set(LIB_DIR "${CMAKE_CURRENT_SOURCE_DIR}/lib")
set(CMAKE_PREFIX_PATH "${CMAKE_PREFIX_PATH};${LIB_DIR}/mavsdk-armv6_v0.31.0")
find_package(MAVSDK REQUIRED)

And then when I need to use the library in a source file, in my src/ directory

target_link_libraries(my_app
    MAVSDK::mavsdk
    MAVSDK::mavsdk_telemetry
}

Hope this helps!

and did you manage to run your application when you build the executable statically with something like below and with the flag -DBUILD_SHARED_LIBS=OFF ?

target_link_libraries(your_target_name -static)

On my side, adding these flags let me build the executable statically but the app crashes when I run it. The crash seems to happen when I create a Mavsdk instance in my code.