Undefined reference when using a lib with build and stage package

I am using following in my snapcraft.yaml

build-packages:
 - libsnapd-glib-dev
 - libglib2.0-dev

stage-packages:
  - libsnapd-glib1

Error:

/home/xyz/plt_invent.c:591: undefined reference to snapd_client_new
/home/xyz/plt_invent.c:592: undefined reference to snapd_client_get_snaps_sync

These functions are are supposed to be part of libsnapd-glib-dev which I have included in build-package. Am I missing something?

Does plt_invent.c include <snapd-glib/snapd-glib.h> and is the program linked against library libsnapd-glib?

No. I was ASSUMING that snapcraft will this do this for me since I used plugin cmake. But should have tried by doing this before.

Anyway, worked after linking the library. Thanks @dimitri

Using CMake is one thing, setting up CMake with proper entries in CMakeLists.txt is another thing. Snapcraft may call CMake but it won’t modify CMakeLists.txt files for you. Neither Cmake nor Snapcraft are able to guess which libraries you need.

Understood. It worked after adding to CMakeists.txt.

On running the code though I get:

ERROR:/home/xyz/plt_invent.c:607:main: assertion failed (error == NULL): error running snapctl: cannot get without a context (snapd-error-quark, 3)

Code snippet:

`   g_auto(GStrv) args = g_strsplit ("get;core;watchdog", ";", -1);
    g_autofree gchar *stdout_output = NULL;
    g_autofree gchar *stderr_output = NULL;
    gboolean result = snapd_client_run_snapctl_sync (client, "ABC", args, &stdout_output, &stderr_output, NULL, &error);
    g_assert_no_error (error);
    g_assert_true (result);
`

Interestingly when I tried to run snapctl directly on the device shell (snap run --shell <snap.app>) I get same error.

Understood that snapctl works only in context of a snap. When run in a shell of snap (using sudo snap run --shell .) with following code, able to get output.

char ctx[100];
snprintf(ctx, 100, "%s", getenv("SNAP_CONTEXT"));
snapd_client_set_socket_path(client, "/run/snapd.socket");
g_auto(GStrv) args = g_strsplit ("services", ";", -1);
g_auto(GStrv) args = g_strsplit ("get;core;watchdog", ";", -1);
g_autofree gchar *stdout_output = NULL;
g_autofree gchar *stderr_output = NULL;
gboolean result = snapd_client_run_snapctl_sync (client, ctx, args, &stdout_output, &stderr_output, NULL, &error);
g_assert_no_error (error);
g_assert_true (result);
printf("stdout: %s\n", stdout_output);
printf("stderr: %s\n", stderr_output);