sampat nayak
sampat nayak

Reputation: 123

how to call a C function which is defined in another layer of yocto?

I have followed the instruction provided in the site:

https://wiki.yoctoproject.org/wiki/Building_your_own_recipes_from_first_principles

I have successfully flashed the yocto image by building my own layer. But my question is :

Is it possible to create two layers i.e Layer 1 and Layer 2? the Layer 1 contains C program.

SimpleLibrary.c

#include<stdio.h>

int add_numbers(int a, int b)
{
    return a+b;
}

The Layer 1 must provide this library(.so) so that Layer 2 can make use of the mentioned function (add_numbers) .

The Layer 2 i have a c program which will call the function in the Layer 1

#include <stdio.h>

int main()
{
    int a = 2, b = 3;

    int sum = add_numbers(a, b);
    printf("%d",sum);
}

If possible how to compile the Layer 1, Layer 2 and what are the changes that I should make in conf files of both layer?

Upvotes: 0

Views: 309

Answers (1)

Sunil Kumar
Sunil Kumar

Reputation: 42

Create a Library recipe for the SimpleLibrary.c and .h files using

...

SRC_URI := " \
        file://SimpleLibrary.c  \
        file://SimpleLibrary.h  \
        "

S = "${WORKDIR}/"

PACKAGES = "${PN} ${PN}-dev ${PN}-dbg ${PN}-staticdev"

RDEPENDS_${PN}-staticdev = ""
RDEPENDS_${PN}-dev = ""
RDEPENDS_${PN}-dbg = ""

do_compile() {
             ${CC} *.c -c
             ${AR} rcs libSimpleLibrary.a *.o
}

do_install() {
             install -d ${D}${libdir}
             install -m 0644 libSimpleLibrary.a ${D}${libdir}
}

TARGET_CC_ARCH += "${LDFLAGS}"

and then for the application use the below

FILESEXTRAPATHS_prepend := "${THISDIR}<path to the simple Library header file>:"

SRC_URI = "file://app.c \
           file://SimpleLibrary.h \ 
        "

S = "${WORKDIR}/"

DEPENDS = "SimpleLibrary"

do_compile() {
             ${CC} *.c -o app -lSimpleLibrary
}

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

TARGET_CC_ARCH += "${LDFLAGS}"

Or basically, when you add DEPENDS = "SimpleLibrary". the header files shall of your library will be fetched into Application's staging directory. so, you can include them and compile without any problem

Upvotes: 1

Related Questions