Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
277 views
in Technique[技术] by (71.8m points)

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
");
        return 1;
    }
    fprintf(stdout, "Connection established!
");
    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?

question from:https://stackoverflow.com/questions/65919172/how-to-link-wayland-header-files-in-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...