What do I need to do to link with xlib?
Asked Answered
A

5

25

I am using GCC, what switches do I need to add to link with Xlib? After searching, all I could find was -lX11, but that gave me ld: library not found for -lX11

I am using a mac (10.6), but I would not like anything that is Mac specific.

Absorber answered 31/12, 2009 at 1:15 Comment(0)
C
44

You can usually use pkg-config to determine the flags you need to pass:

gcc my-program.c $(pkg-config --cflags --libs x11) -o my-program
Conversable answered 31/12, 2009 at 1:23 Comment(5)
Hey! I didn't know that was there. And I've always liked that kind of facility when individual programs supply it (e.g. root-config with root.cern.ch).Peripeteia
Hmmm...mine seems to be supplied by fink (on Mac OS 10.5).Peripeteia
+1 : I love the answers when someone asks for a fish and gets a fishing rod in return :)Stibine
I'd like to point out that this answer didn't work for me on ubuntu (gcc 4.6.1). It places the -lX11 argument before the -o argument and since gcc searches object files and libraries in the order specified I still get 'undefined reference' errors. See also: GCC documentationRetractor
@rakete Fixed the order. The libraries need to come after the .c file, not after the -o argument, so I've left that at the end, just moved the .c file up before the library flags.Conversable
P
6
$ locate libX11
/Developer/SDKs/MacOSX10.4u.sdk/usr/X11R6/lib/libX11.6.2.dylib
/Developer/SDKs/MacOSX10.4u.sdk/usr/X11R6/lib/libX11.6.dylib
/Developer/SDKs/MacOSX10.4u.sdk/usr/X11R6/lib/libX11.a
/Developer/SDKs/MacOSX10.4u.sdk/usr/X11R6/lib/libX11.dylib
/Developer/SDKs/MacOSX10.5.sdk/usr/X11/lib/libX11.6.2.0.dylib
/Developer/SDKs/MacOSX10.5.sdk/usr/X11/lib/libX11.6.dylib
/Developer/SDKs/MacOSX10.5.sdk/usr/X11/lib/libX11.dylib
/usr/X11/lib/libX11.6.2.0.dylib
/usr/X11/lib/libX11.6.dylib
/usr/X11/lib/libX11.dylib
/usr/X11/lib/libX11.la

I'd try

gcc [...] -L/usr/X11/lib -lX11 [...]

to set the search path for libraries.

Peripeteia answered 31/12, 2009 at 1:20 Comment(0)
S
5

You'd be surprised, but sometimes -L/usr/X11R6/lib -lX11 is the answer -_-.

Stibine answered 31/12, 2009 at 1:21 Comment(0)
F
2

You also need -L/usr/X11/lib. It's not exactly Mac-specific, but you will find that the location of these libs will vary a bit from system to system.

Forego answered 31/12, 2009 at 1:19 Comment(0)
G
1

Ubuntu 12.10 (And probably others) require sudo apt-get install libx11-dev.

Gospodin answered 8/12, 2012 at 20:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.