Jade
Jade

Reputation: 469

How to pass sysroot from Yocto to cmake

I am using a Yocto recipe to build a cmake project. In the Yocto enviroment, I confirmed that --sysroot=my-app/recipe-sysroot and all dependent libraries have been installed into that location. In my CMakeList.txt, I set link_directories(/usr/lib), but error messages occur while building to indicate the dependent libraries cannot be found. Does anyone know how to pass the Yocto sysroot environment to cmake?

My recipe, inherit cmake

DEPENDS = "\
        azure-iot-sdk-c \
        openssl \
        zlib \
        curl \
        boost \
        parson \
        gtest \
"

S = "${WORKDIR}/git"
B = "${WORKDIR}/build"

EXTRA_OECMAKE = "\
        -DCMAKE_BUILD_TYPE=Debug \
        -DBUILD_SAMPLES=ON \
        -DBUILD_TESTS=ON \
        -DBUILD_TOOLS=ON \
"

FILES_${PN}-staticdev = "${libdir}/*.a"
FILES_${PN} += "${libdir}/*.so"
FILES_SOLIBSDEV = ""
INSANE_SKIP_${PN} += "dev-so rpaths"

And if I set the real path in my CMakeList, it works,

set(LIBRARY_DIR "${CMAKE_CURRENT_LIST_DIR}/../recipe-sysroot/usr/lib")
set(STATIC_LIBRARY ${LIBRARY_DIR}/libiothub_client.a)

But if I set the related path according to sysroot, it does not work,

set(LIBRARY_DIR "/usr/lib")

Upvotes: 3

Views: 4740

Answers (1)

Florian Berndl
Florian Berndl

Reputation: 1474

Don't do:

set(LIBRARY_DIR "${CMAKE_CURRENT_LIST_DIR}/../recipe-sysroot/usr/lib")

It is usually not necessary to have any yocto specific information in your cmake. Instead add

include(GNUInstallDirs)

In your recipe add:

inherit cmake

If your cmake installs the static library and headers correctly e.g.:

install (TARGETS <yourtarget> DESTINATION ${CMAKE_INSTALL_LIBDIR})
file(GLOB HEADERS include/*.h)
install(FILES ${HEADERS} DESTINATION  ${CMAKE_INSTALL_INCLUDEDIR})

My above advice should work, if you not install the static library using cmake you have to define an do_install task in your package recipe. For example

do_install() {
   install -m 0755 -d ${D}${libdir} 
   install -m 0755 ${B}/test-staticlibrary.a  ${D}/${libdir}
}

If you post your cmake files I can assist you further

Upvotes: 4

Related Questions