如何在C中链接Wayland头文件?

问题描述

我开始为 Wayland 编码。我一直在关注很多教程,但我仍然坚持编译。我写了这个非常简单的代码

#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;
}

然后,我尝试用 gcc -o client -lwayland-client client.c
编译它 但是,编译失败并显示以下错误消息:

/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

这看起来很奇怪,因为系统中有一个文件 /usr/include/wayland-client-core.h,它有这些声明:

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

在这里做错了什么?

解决方法

您需要将命令行更改为:

gcc client.c -o client -lwayland-client

或:

gcc client.c -lwayland-client -o client

或:

gcc -o client client.c -lwayland-client

基本上,.c 文件(或 .o 文件)需要出现在库链接选项之前。