1

I'm trying to compile the example screensaver simplesquares.c (from here). I have vroot.h, and this is the command I'm using:

gcc -lX11 -o simplesquares simplesquares.c

These are the errors returned:

/tmp/cc0FvxcU.o: In function `VirtualRootWindowOfScreen':
simplesquares.c:(.text+0x55): undefined reference to `XInternAtom'
simplesquares.c:(.text+0x82): undefined reference to `XQueryTree'
simplesquares.c:(.text+0x108): undefined reference to `XGetWindowProperty'
simplesquares.c:(.text+0x14c): undefined reference to `XFree'
/tmp/cc0FvxcU.o: In function `main':
simplesquares.c:(.text+0x17b): undefined reference to `XOpenDisplay'
simplesquares.c:(.text+0x1c8): undefined reference to `XCreateGC'
simplesquares.c:(.text+0x201): undefined reference to `XSetForeground'
simplesquares.c:(.text+0x295): undefined reference to `XFillRectangle'
simplesquares.c:(.text+0x2e3): undefined reference to `XClearWindow'
simplesquares.c:(.text+0x2ef): undefined reference to `XFlush'
collect2: error: ld returned 1 exit status

What is happening here, and how can I fix it?

blimpse
  • 126
  • 2
  • 5
  • 14

1 Answers1

3

Statically linked libraries should be at the end of the command line.

gcc -o simplesquares simplesquares.c -lX11

From http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html:

It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, ‘foo.o -lz bar.o’ searches library ‘z’ after file foo.o but before bar.o. If bar.o refers to functions in ‘z’, those functions may not be loaded.

shanet
  • 7,246
  • 3
  • 34
  • 46