gccyoctobitbakegpsd

How to get gps.h into a Yocto recipe build?


I've built a simple recipe which works as long as I don't need gps.h:

recipes/foo (dunfell) $ cat foo_3.0.0.bb 
DESCRIPTION = "FOO Daemon"
LICENSE = "CLOSED"

SRC_URI = " file://*.* \
    "
S = "${WORKDIR}"

INSANE_SKIP_${PN} = "ldflags"
INHIBIT_PACKAGE_DEBUG_SPLIT = "1"
INHIBIT_PACKAGE_STRIP = "1"

do_compile() {
    cd ${S}/src
    make
    cp foo ~/
    cd -
}

do_install() {
    install -d ${D}${bindir}
    install -m 0755 foo ${D}${bindir}
}

gps.h is in /usr/include on my local machine, but as Yocto is cross-compiling it provides a reasonable explanation of why it can't use the local /usr/include/gps.h:

cc1: error: include location "/usr/include" is unsafe for cross-compilation [-Werror=poison-system-directories]
foo.c:54:10: fatal error: gps.h: No such file or directory
   54 | #include <gps.h>
      |          ^~~~~~~
cc1: all warnings being treated as errors

I've tried IMAGE_INSTALL_append " libgps-dev" and " gps-lib-dev" in my layer.conf but neither of those work.

How can I get the gps.h header into my Yocto project/recipe at build time?


Solution

  • Let me copy your recipe and add some comments first:

    DESCRIPTION = "FOO Daemon"
    LICENSE = "CLOSED"
    
    # --- COMMENT ---
    # It is not recommended to use "*" with SRC_URI, 
    # as Yocto will not keep track of your files if you edit them 
    # so it will never rebuild automaticall after a change
    # Best practice is to sepecify the local files like:
    # SRC_URI = "file://src"
    # This will make bitbake unpacks the "src" folder into ${WORKDIR}
    # --- COMMENT ---
    SRC_URI = " file://*.* \
        "
    
    # --- COMMENT ---
    # The ${S} variable is the defautl workind directory for compilation tasks, 
    # do_configure, do_compile, ..., 
    # So, if you have "src" folder that will be unpacked into ${WORKDIR}
    # you need to set S to that:
    # S = "${WORKDIR}/src"
    # --- COMMENT ---
    S = "${WORKDIR}"
    
    INSANE_SKIP_${PN} = "ldflags"
    INHIBIT_PACKAGE_DEBUG_SPLIT = "1"
    INHIBIT_PACKAGE_STRIP = "1"
    
    # --- COMMENT ---
    # If your project has a "Makefile" you can use the "autotools" class
    # it runs oe_runmake automatically
    # inherit autotools
    # If you want to copy the output to your home directory you can do it in "do_install"
    # If you use autotools you do not need do_compile
    # --- COMMENT ---
    do_compile() {
        cd ${S}/src
        make
        cp foo ~/
        cd -
    }
    
    do_install() {
        install -d ${D}${bindir}
        install -m 0755 foo ${D}${bindir}
    }
    
    # --- COMMENT ---
    # Do not forget to specify your output files into FILES for do_package to work well
    # FILES_${PN} = "${bindir}/foo"
    # --- COMMENT ---
    

    Now, after dealing with that, if your recipe requires something at build-time, than the dependency needs to exist in the same recipe's workding directory, because if you are adding libgps into IMAGE_INSTALL it will be present in the rootfs but not during your build time.

    So, to do that, you need to specify the dependencies recipe with DEPENDS.

    I have looked for gps.h and I found it packages with gpsd recipe.

    So, try:

    DEPENDS += "gpsd"
    

    So, the final recipe would look like the following:

    DESCRIPTION = "FOO Daemon"
    LICENSE = "CLOSED"
    
    SRC_URI = "file://src"
    
    S = "${WORKDIR}/src"
    
    DEPENDS += "gpsd"
    
    inherit autotools
    
    do_install(){
        install -d ${D}${bindir}
        install -m 0755 foo ${D}${bindir}
        cp foo ~/
    }
    
    FILES_${PN} = "${bindir}/foo"
    

    The only thing left is to test.