简体   繁体   English

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

[英]How to link Wayland header files in C?

I am starting out with coding for Wayland.我开始为 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然后,我尝试用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:这看起来很奇怪,因为系统中有一个文件/usr/include/wayland-client-core.h ,它有以下声明:

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

What am I doing wrong here?我在这里做错了什么?

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.基本上,.c 文件(或.o 文件)需要出现在库链接选项之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM