Search code examples
cmakeldazure-iot-hubazure-iot-sdk

Linking error related to libcurl while building azure-iot-sdk-c sample code


I was facing an issue while building azure-iot-sdk-c sample code with below toolchain.cmake configuration.

SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_C_COMPILER   arm-linux-gcc)
SET(CMAKE_CXX_COMPILER arm-linux-g++)
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
SET(CURL_INCLUDE_DIR /curl_path/include/)
SET(CURL_LIBRARY /curl_path/lib/libcurl.so)

Below is the error which I am getting on console:

/sysroot/bin/ld: cannot find -lcurl

During the compilation of azure-sdk libraries cmake is taking CURL_INCLUDE_DIR and CURL_LIBRARY path as expected. but during the building of sample code, which present inside azure-sdk repo, it is failing with -lcurl not found error. This issue is due to CMake is looking into the wrong directory (i.e sysroot/usr/lib/) instead of searching libcurl. so inside CURL_LIBRARY passed from toolchain.cmake.

How i can make CMake to look into CURL_LIBRARY directory for linking of sample code binaries to resolve this issue?


Solution

  • This issue was because in azure-iot-sdk sample code they are directly referring curl library before finding curl package .which is something like find_package(CURL) they should be doing before target_link_libraries(azure_sample_bin lcurl) . i was able to fix it adding below code inside azure-iot-sdk-c/CMakeLists.txt

    add_library( curl SHARED IMPORTED)
    set_property(TARGET curl PROPERTY IMPORTED_LOCATION "${CURL_LIBRARY}")
    

    Also this issue can be avoided if we stop building sample codes.This can be done by making skip_samples=ON .But even though this flag is ON by default inside azure-iot-sdk-c/CMakeLists.txt , it is not making any effect while building(may be beacuse this line of code is at wrong place ).we can still aviod building sample code by passing skip_samples:BOOL=ON while running cmake as below

    cmake "-Dskip_samples:BOOL=ON"  path_to_azure_sdk/