Axurez
V2EX  ›  macOS

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

  •  
  •   Axurez · Apr 22, 2015 · 2679 views
    This topic created in 4079 days ago, the information mentioned may be changed or developed.

    编译命令: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 replies    2015-04-23 18:14:15 +08:00
    coderwang
        1
    coderwang  
       Apr 23, 2015
    X要运行起来,不能直接在终端里执行
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3111 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 13:50 · PVG 21:50 · LAX 06:50 · JFK 09:50
    ♥ Do have faith in what you're doing.