Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

stijnschoor

macrumors newbie
Original poster
Jun 7, 2008
6
0
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?
 
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.
 
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?
 
Thank you for the reply,

Do you know how to solv that?

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.
 
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.


'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?)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.