cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Observer
Observer
911 Views
Registered: ‎03-18-2020

How to add recipe in yocto to build curl example?

Jump to solution

I can build the test.c without using yocto in my terminal easily. So this means
there is no problem with the test.c or the libcurl in my host machine.

However, when I build using yocto it complains that it cannot find curl/curl.h.

DEBUG: Executing shell function do_compile
| test.c:5:10: fatal error: curl/curl.h: No such file or directory
| #include <curl/curl.h>
| ^~~~~~~~~~~~~
| compilation terminated.
| WARNING: exit code 1 from a shell command.


Here is my directory structure which contains my example based out of libcurl.

├── curlTest
│   ├── curlTest
│   │   ├── README.txt
│   │   └── test.c
│   └── curlTest.bb

Here is my curlTest.bb
curlTest.bb

SUMMARY = "Simple Hello World Application"
DESCRIPTION = "A test application to demonstrate how to create a recipe \
by directly compiling C files with BitBake."

SECTION = "examples"
PRIORITY = "optional"

LICENSE = "MIT"
LIC_FILES_CHKSUM = "\
file://README.txt;md5=321180101a13e8916e6753497a4f9c82"

SRC_URI = "file://test.c \
file://README.txt"

S = "${WORKDIR}"

do_compile() {
${CC} ${CFLAGS} ${LDFLAGS} -o testCurl test.c -lcurl
}

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

Here is the example of libcurl based out of the libcurl website.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <curl/curl.h>

struct MemoryStruct {
char *memory;
size_t size;
};

static size_t
WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)userp;

char *ptr = realloc(mem->memory, mem->size + realsize + 1);
if(ptr == NULL) {
/* out of memory! */
printf("not enough memory (realloc returned NULL)\n");
return 0;
}
mem->memory = ptr;
memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;

return realsize;
}

int main(void)
{
CURL *curl_handle;
CURLcode res;

struct MemoryStruct chunk;

chunk.memory = malloc(1); /* will be grown as needed by the realloc above */
chunk.size = 0; /* no data at this point */

curl_global_init(CURL_GLOBAL_ALL);

/* init the curl session */
curl_handle = curl_easy_init();

/* specify URL to get */
curl_easy_setopt(curl_handle, CURLOPT_URL, "https://www.example.com/");

/* send all data to this function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);

/* we pass our 'chunk' struct to the callback function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);

/* some servers don't like requests that are made without a user-agent
field, so we provide one */
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");

/* get it! */
res = curl_easy_perform(curl_handle);

/* check for errors */
if(res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
}
else {
/*
* Now, our chunk.memory points to a memory block that is chunk.size
* bytes big and contains the remote file.
*
* Do something nice with it!
*/

printf("%lu bytes retrieved\n", (unsigned long)chunk.size);
}

/* cleanup curl stuff */
curl_easy_cleanup(curl_handle);

free(chunk.memory);

/* we're done with libcurl, so clean it up */
curl_global_cleanup();

return 0;
}
0 Kudos
Reply
1 Solution

Accepted Solutions
Moderator
Moderator
773 Views
Registered: ‎04-24-2017

Hi @joshis1 ,

Here is the example.

  1. Create a apps project using template option.
    $ petalinux-create -t apps -n curltest --template c --enable
    $ tree project-spec/meta-user/recipes-apps/curltest/
    project-spec/meta-user/recipes-apps/curltest/
    ├── curltest.bb
    ├── files
    │   ├── curltest.c
    │   └── Makefile
    └── README
    
    1 directory, 4 files
    
  2. Modify curltest.c, here I'm using simple example from curl documentation https://curl.haxx.se/libcurl/c/simple.html
    /* <DESC>
     * Very simple HTTP GET
     * </DESC>
     */
    #include <stdio.h>
    #include <curl/curl.h>
    
    int main(void)
    {
      CURL *curl;
      CURLcode res;
    
      curl = curl_easy_init();
      if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
        /* example.com is redirected, so we tell libcurl to follow redirection */
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
    
        /* Perform the request, res will get the return code */
        res = curl_easy_perform(curl);
        /* Check for errors */
        if(res != CURLE_OK)
          fprintf(stderr, "curl_easy_perform() failed: %s\n",
                  curl_easy_strerror(res));
    
        /* always cleanup */
        curl_easy_cleanup(curl);
      }
      return 0;
    }
  3. Modify curltest.bb to include dependencies packages(like curl, ssl, crypto etc), compiler and linker flags as show below.
    #
    # This file is the curltest recipe.
    #
    
    SUMMARY = "Simple curltest application"
    SECTION = "PETALINUX/apps"
    LICENSE = "MIT"
    LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"
    
    SRC_URI = " \
        file://curltest.c \
        file://Makefile \
    "
    
    S = "${WORKDIR}"
    
    DEPENDS += " \
        curl \
        openssl \
    "
    
    CFLAGS += "-c -g"
    
    LDFLAGS += "-lpthread -lcurl -lssl"
    
    EXTRA_OEMAKE = "'CFLAGS=${CFLAGS}' 'LDFLAGS=${LDFLAGS}'"
    
    do_compile() {
        oe_runmake
    }
    
    do_install() {
        install -d ${D}${bindir}
        install -m 0755 curltest ${D}${bindir}
    }
  4. Build curltest apps
    $ petalinux-build -c curltest
Thanks,
Sandeep
PetaLinux Yocto | Embedded SW Support

---------------------------------------------------------------------------
Don’t forget to Reply, Kudo, and Accept as Solution.
---------------------------------------------------------------------------

View solution in original post

3 Replies
Xilinx Employee
Xilinx Employee
812 Views
Registered: ‎09-17-2019

Hi @joshis1 ,

You are using a Yocto or a petalinux? if you are using petalinux please refer to a UG1144.

https://www.xilinx.com/support/documentation/sw_manuals/xilinx2020_1/ug1144-petalinux-tools-reference-guide.pdf 

With Regards,

Shubhamgi

0 Kudos
Reply
Moderator
Moderator
774 Views
Registered: ‎04-24-2017

Hi @joshis1 ,

Here is the example.

  1. Create a apps project using template option.
    $ petalinux-create -t apps -n curltest --template c --enable
    $ tree project-spec/meta-user/recipes-apps/curltest/
    project-spec/meta-user/recipes-apps/curltest/
    ├── curltest.bb
    ├── files
    │   ├── curltest.c
    │   └── Makefile
    └── README
    
    1 directory, 4 files
    
  2. Modify curltest.c, here I'm using simple example from curl documentation https://curl.haxx.se/libcurl/c/simple.html
    /* <DESC>
     * Very simple HTTP GET
     * </DESC>
     */
    #include <stdio.h>
    #include <curl/curl.h>
    
    int main(void)
    {
      CURL *curl;
      CURLcode res;
    
      curl = curl_easy_init();
      if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
        /* example.com is redirected, so we tell libcurl to follow redirection */
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
    
        /* Perform the request, res will get the return code */
        res = curl_easy_perform(curl);
        /* Check for errors */
        if(res != CURLE_OK)
          fprintf(stderr, "curl_easy_perform() failed: %s\n",
                  curl_easy_strerror(res));
    
        /* always cleanup */
        curl_easy_cleanup(curl);
      }
      return 0;
    }
  3. Modify curltest.bb to include dependencies packages(like curl, ssl, crypto etc), compiler and linker flags as show below.
    #
    # This file is the curltest recipe.
    #
    
    SUMMARY = "Simple curltest application"
    SECTION = "PETALINUX/apps"
    LICENSE = "MIT"
    LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"
    
    SRC_URI = " \
        file://curltest.c \
        file://Makefile \
    "
    
    S = "${WORKDIR}"
    
    DEPENDS += " \
        curl \
        openssl \
    "
    
    CFLAGS += "-c -g"
    
    LDFLAGS += "-lpthread -lcurl -lssl"
    
    EXTRA_OEMAKE = "'CFLAGS=${CFLAGS}' 'LDFLAGS=${LDFLAGS}'"
    
    do_compile() {
        oe_runmake
    }
    
    do_install() {
        install -d ${D}${bindir}
        install -m 0755 curltest ${D}${bindir}
    }
  4. Build curltest apps
    $ petalinux-build -c curltest
Thanks,
Sandeep
PetaLinux Yocto | Embedded SW Support

---------------------------------------------------------------------------
Don’t forget to Reply, Kudo, and Accept as Solution.
---------------------------------------------------------------------------

View solution in original post

Visitor
Visitor
555 Views
Registered: ‎05-11-2020

Dear sandeepg:

I encountered a similar problem, i can understand your example code,

but my issue is that, how to enable the curl library in petlinux project or add the open curl source firstly ?

after i follow your example steps, i can not build successfully, the log as follow:

[INFO] building project
[INFO] sourcing bitbake
INFO: bitbake petalinux-user-image
Loading cache: 100% |#########################################################################################################################################################| Time: 0:00:00
Loaded 3262 entries from dependency cache.
Parsing recipes: 100% |#######################################################################################################################################################| Time: 0:00:01
Parsing of 2472 .bb files complete (2439 cached, 33 parsed). 3265 targets, 226 skipped, 0 masked, 0 errors.
NOTE: Resolving any missing task queue dependencies
Initialising tasks: 100% |####################################################################################################################################################| Time: 0:00:06
Checking sstate mirror object availability: 100% |############################################################################################################################| Time: 0:00:17
NOTE: Executing SetScene Tasks
NOTE: Executing RunQueue Tasks
ERROR: curltest-1.0-r0 do_compile: oe_runmake failed
ERROR: curltest-1.0-r0 do_compile: Function failed: do_compile (log file is located at /home/alinx/work/trunk/demo_prj/build/tmp/work/cortexa9hf-neon-xilinx-linux-gnueabi/curltest/1.0-r0/temp/log.do_compile.18545)
ERROR: Logfile of failure stored in: /home/alinx/work/trunk/demo_prj/build/tmp/work/cortexa9hf-neon-xilinx-linux-gnueabi/curltest/1.0-r0/temp/log.do_compile.18545
Log data follows:
| DEBUG: Executing shell function do_compile
| NOTE: make -j 8 CFLAGS= -O2 -pipe -g -feliminate-unused-debug-types -fdebug-prefix-map=/home/alinx/work/trunk/demo_prj/build/tmp/work/cortexa9hf-neon-xilinx-linux-gnueabi/curltest/1.0-r0=/usr/src/debug/curltest/1.0-r0 -fdebug-prefix-map=/home/alinx/work/trunk/demo_prj/build/tmp/sysroots/x86_64-linux= -fdebug-prefix-map=/home/alinx/work/trunk/demo_prj/build/tmp/sysroots/plnx_arm= -c -g LDFLAGS=-Wl,-O1 -Wl,--hash-style=gnu -Wl,--as-needed -lpthread -lcurl -lssl
| ERROR: oe_runmake failed
| arm-xilinx-linux-gnueabi-gcc -march=armv7-a -marm -mfpu=neon -mfloat-abi=hard -mcpu=cortex-a9 --sysroot=/home/alinx/work/trunk/demo_prj/build/tmp/sysroots/plnx_arm -Wl,-O1 -Wl,--hash-style=gnu -Wl,--as-needed -lpthread -lcurl -lssl -o curltest curltest.o
| curltest.o: In function `main':
| /usr/src/debug/curltest/1.0-r0/curltest.c:45: undefined reference to `curl_easy_init'
| /usr/src/debug/curltest/1.0-r0/curltest.c:47: undefined reference to `curl_easy_setopt'
| /usr/src/debug/curltest/1.0-r0/curltest.c:49: undefined reference to `curl_easy_setopt'
| /usr/src/debug/curltest/1.0-r0/curltest.c:52: undefined reference to `curl_easy_perform'
| /usr/src/debug/curltest/1.0-r0/curltest.c:64: undefined reference to `curl_easy_cleanup'
| /usr/src/debug/curltest/1.0-r0/curltest.c:56: undefined reference to `curl_easy_strerror'
| collect2: error: ld returned 1 exit status
| make: *** [Makefile:11: curltest] Error 1
| ERROR: Function failed: do_compile (log file is located at /home/alinx/work/trunk/demo_prj/build/tmp/work/cortexa9hf-neon-xilinx-linux-gnueabi/curltest/1.0-r0/temp/log.do_compile.18545)
ERROR: Task (/home/alinx/work/trunk/demo_prj/project-spec/meta-user/recipes-apps/curltest/curltest.bb:do_compile) failed with exit code '1'
NOTE: Tasks Summary: Attempted 2325 tasks of which 2320 didn't need to be rerun and 1 failed.

Summary: 1 task failed:
/home/alinx/work/trunk/demo_prj/project-spec/meta-user/recipes-apps/curltest/curltest.bb:do_compile
Summary: There were 2 ERROR messages shown, returning a non-zero exit code.
ERROR: Failed to build project

0 Kudos
Reply