cwayland

How to link Wayland header files in C?


I am starting out with coding for Wayland. I have been following many tutorials, but I am still stuck at compiling. I wrote this very simple code:

#include<stdio.h>
#include<wayland-client-core.h>

int main(int argc, char *argv[]) {
    struct wl_display *display = wl_display_connect(NULL);
    if(!display) {
        fprintf(stderr, "Failed to connect to Wayland display\n");
        return 1;
    }
    fprintf(stdout, "Connection established!\n");
    getchar();
    wl_display_disconnect(display);
    return 0;
}

Then, I tried to compile it with gcc -o client -lwayland-client client.c
But, compilation fails with this error message:

/usr/bin/ld: /tmp/ccfXeOS8.o: in function `main':
client.c:(.text+0x19): undefined reference to `wl_display_connect'
/usr/bin/ld: client.c:(.text+0x7c): undefined reference to `wl_display_disconnect'
collect2: error: ld returned 1 exit status

This looks strange because there is a file /usr/include/wayland-client-core.h in the system, which has got these declarations:

struct wl_display *wl_display_connect(const char *name);
void wl_display_disconnect(struct wl_display *display);

What am I doing wrong here?


Solution

  • You need to change the command line to:

    gcc client.c -o client -lwayland-client
    

    or:

    gcc client.c -lwayland-client -o client
    

    or:

    gcc -o client client.c -lwayland-client
    

    Basically, the .c file (or .o file) needs to appear before the library linking options.