V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Axurez
V2EX  ›  macOS

本地编译的 X11 程序运行提示“Cannot open display”

  •  
  •   Axurez · 2015-04-22 17:06:18 +08:00 · 2134 次点击
    这是一个创建于 3306 天前的主题,其中的信息可能已经有所发展或是发生改变。

    编译命令:gcc hello-x.c -L/opt/X11/lib -lX11 -o hello-x
    源码(来自 Tutorial http://rosettacode.org/wiki/Window_creation/X11#C):

    #include <X11/Xlib.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void) {
        Display *d;
        Window w;
        XEvent e;
        char *msg = "Hello, World!";
        int s;
    
        d = XOpenDisplay(NULL);
        if (d == NULL) {
            fprintf(stderr, "Cannot open display\n");
            exit(1);
        }
    
        s = DefaultScreen(d);
        w = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, 100, 100, 1,
                                        BlackPixel(d, s), WhitePixel(d, s));
        XSelectInput(d, w, ExposureMask | KeyPressMask);
        XMapWindow(d, w);
    
        while (1) {
            XNextEvent(d, &e);
            if (e.type == Expose) {
                XFillRectangle(d, w, DefaultGC(d, s), 20, 20, 10, 10);
                XDrawString(d, w, DefaultGC(d, s), 10, 50, msg, strlen(msg));
            }
            if (e.type == KeyPress)
                break;
        }
    
        XCloseDisplay(d);
        return 0;
    }
    
    1 条回复    2015-04-23 18:14:15 +08:00
    coderwang
        1
    coderwang  
       2015-04-23 18:14:15 +08:00
    X要运行起来,不能直接在终端里执行
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3601 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 82ms · UTC 10:46 · PVG 18:46 · LAX 03:46 · JFK 06:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.