X11 development

Discussion in 'Mac Programming' started by stijnschoor, Jul 2, 2008.

  1. macrumors newbie

    Joined:
    Jun 7, 2008
    #1
    Hello

    Check this code
    Code:
    #include        <stdio.h>
    #include        <X11/Xlib.h>
    #include        <X11/Xutil.h>
    
    Display * mydisplay;
    Window mywindow;
    GC mygc;
    XEvent myevent;
    KeySym mykey;
    XSizeHints myhint;
    int     myscreen;
    unsigned long   myforeground,
                    mybackground;
    XFontStruct * font_struct;
    
    /* declarations */
    char    window_title[] = {"Hello World"};
    
    main (argc, argv)
    int     argc;
    char  **argv;
    {
        int     i;
        char    text[10];
        int     done;
     XColor  greencolor;
     XColor  rgb;
    
        mydisplay = XOpenDisplay ("");
        myscreen = DefaultScreen (mydisplay);
    
        /* default pixel values */
        mybackground = WhitePixel (mydisplay, myscreen);
        myforeground = BlackPixel (mydisplay, myscreen);
    
        /* default program-specified window position and size */
        myhint.x = 10;
        myhint.y = 10;
        myhint.width = 250;
        myhint.height = 150;
        myhint.flags = PPosition | PSize;
    
        /* window creation */
        mywindow = XCreateSimpleWindow (mydisplay,
         DefaultRootWindow (mydisplay),
         myhint.x, myhint.y, myhint.width,
     myhint.height,
         5, myforeground, mybackground);
        XSetStandardProperties (mydisplay, mywindow, window_title, window_title, 
    None, argv, argc, &myhint);
    
        /* GC creation and initialization */
        mygc = XCreateGC (mydisplay, mywindow, 0, 0);
        XSetBackground (mydisplay, mygc, mybackground);
        XSetForeground (mydisplay, mygc, myforeground);
        XAllocNamedColor(mydisplay, DefaultColormap(mydisplay, myscreen),
       "MediumForestGreen", &greencolor, &rgb);
    
        /* font initialization */
        font_struct = XLoadQueryFont (mydisplay, "*courier-bold-r-normal--14*");
        XSetFont (mydisplay, mygc, font_struct -> fid);
    
        /* input event selection */
        XSelectInput (mydisplay, mywindow,
         ButtonPressMask | KeyPressMask | ExposureMask);
    
        /* window mapping */
        XMapRaised (mydisplay, mywindow);
        XFlush (mydisplay);
    
        /* main event-reading loop */
        done = 0;
        while (done == 0) {
    
     /* read the next event */
     XNextEvent (mydisplay, &myevent);
     switch (myevent.type) {
    
     /* repaint window on expose events */
         case Expose: 
     XSetForeground(mydisplay, mygc, myforeground);
     XDrawString (mydisplay, mywindow, mygc, 10, 60, "Hello, World", 12);
     XDrawRectangle(mydisplay, mywindow, mygc, 5, 47, 150, 15);
     if (myevent.xexpose.count == 0)
     XFlush (mydisplay);
     break;
    
     /* process mouse-button presses */
         case ButtonPress: 
     done = 1;
     break;
    
     /* process keyboard input */
         case KeyPress: 
     i = XLookupString (&myevent, text, 10, &mykey, 0);
     if (i == 1 && text[0] == "q")
     done = 1;
     break;
    
         default: 
     break;
    
     } /* switch (myevent.type) */
        }   /* while (done == 0) */
    
        /* termination */
        XFreeGC (mydisplay, mygc);
        XDestroyWindow (mydisplay, mywindow);
        XCloseDisplay (mydisplay);
        exit(0);
    }
    When I run this program I get the error: ZeroLink: unknown symbol '_XOpenDisplay'

    I think that there is a library missing. Any Unix developers out there who can help me?
     
  2. macrumors 6502a

    yeroen

    Joined:
    Mar 8, 2007
    Location:
    Cambridge, MA
    #2
    The obvious question to ask is what are your link flags you're passing to the compiler?

    You should have (at least) the following:

    -L/usr/X11R6/lib -lX11

    or, if you're using Xcode, put usr/X11R6/lib under 'Library Search Paths' and -lX11 under 'Other Linker Options'.

    I'd also turn off ZeroLink to ensure your program doesn't core due to load errors at runtime.
     
  3. thread starter macrumors newbie

    Joined:
    Jun 7, 2008
    #3
    Thank you for the reply,

    But now I've got another error:

    /usr/bin/ld: Undefined symbols:
    _XAllocNamedColor
    _XCloseDisplay
    _XCreateGC
    _XCreateSimpleWindow
    _XDestroyWindow
    _XDrawRectangle
    _XDrawString
    _XFlush
    _XFreeGC
    _XLoadQueryFont
    _XLookupString
    _XMapRaised
    _XNextEvent
    _XOpenDisplay
    _XSelectInput
    _XSetBackground
    _XSetFont
    _XSetForeground
    _XSetStandardProperties
    collect2: ld returned 1 exit status

    Do you know how to solv that?
     
  4. thread starter macrumors newbie

    Joined:
    Jun 7, 2008
    #4
    btw I'am using Mac OS X 10.4
     
  5. macrumors G4

    Joined:
    Jan 5, 2006
    Location:
    Redondo Beach, California
    #5
    Try reading the man page for the missing function for example

    "man XSelectInput"

    Then it will tell you XSelectInput an XLIB functon which means you likely need to link in xlib.

    The other trick if you are truly desperate to figure out which library contains the symbol you need is the use the "strings" command on a bunch of libraries and grep for the symbol name. We call this method "BF&BI" (brute fore and bloody ignorance) It works well.

    The "-k" option works well with "man" if you need to search the man pages.
     
  6. macrumors 6502a

    yeroen

    Joined:
    Mar 8, 2007
    Location:
    Cambridge, MA
    #6

    'strings' is good. 'nm' is better.

    Try compiling this outside Xcode. That is on the terminal, do:

    gcc -o myprogram myprogram.c -I/usr/X11R6/lib -lX11

    (note gcc, not g++. are you trying to compile this a C++ program in Xcode? Does your main file have a .cpp extension?)
     

Share This Page