Linker 致命错误:';X11/Xlib.h';找不到文件

Linker 致命错误:';X11/Xlib.h';找不到文件,linker,x11,xlib,Linker,X11,Xlib,我已经安装了XQuartz。 我使用g++编译: g++ -o -lX11 -I/opt/X11/include window2.cc 错误 Undefined symbols for architecture x86_64: "_XCreateWindow", referenced from: _main in window2-dXb9bZ.o "_XFlush", referenced from: _main in window2-dXb9bZ.o "_XMapWindow",

我已经安装了XQuartz。 我使用g++编译:

g++ -o -lX11 -I/opt/X11/include window2.cc
错误

Undefined symbols for architecture x86_64:

"_XCreateWindow", referenced from:
  _main in window2-dXb9bZ.o
 "_XFlush", referenced from:
  _main in window2-dXb9bZ.o
"_XMapWindow", referenced from:
  _main in window2-dXb9bZ.o
"_XOpenDisplay", referenced from:
  _main in window2-dXb9bZ.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ld: library not found for -lX11
clang: error: linker command failed with exit code 1 (use -v to see invocation)
如果我这样编译:

g++ window2.cc -o window -lX11 -I/opt/X11/include
g++ -o window window.cc -I/usr/X11R6/include -L/usr/X11R6/lib -lX11
错误

Undefined symbols for architecture x86_64:

"_XCreateWindow", referenced from:
  _main in window2-dXb9bZ.o
 "_XFlush", referenced from:
  _main in window2-dXb9bZ.o
"_XMapWindow", referenced from:
  _main in window2-dXb9bZ.o
"_XOpenDisplay", referenced from:
  _main in window2-dXb9bZ.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ld: library not found for -lX11
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我确信Xlib.h在/opt/X11/include中

代码:

#包括
#包括
int main()
{
//打开显示器。
显示*d=XOpenDisplay(0);
如果(d)
{
//创建窗口
窗口w=XCreateWindow(d,DefaultRootWindow(d),0,0,200,
100,0,CopyFromParent,CopyFromParent,
CopyFromParent,0,0);
//显示窗口
XMapWindow(d,w);
XFlush(d);
//睡得足够长,可以看到窗户。
睡眠(10);
}
返回0;
}
我如何解决这个问题?提前感谢

试试看

cc -I /opt/X11/include/ test.c -L /opt/X11/lib -lX11

问题解决了。如果有人感兴趣: 您必须这样编译:

g++ window2.cc -o window -lX11 -I/opt/X11/include
g++ -o window window.cc -I/usr/X11R6/include -L/usr/X11R6/lib -lX11

标题与问题中给出的错误有什么关系?我认为您必须安装X11头(根据您的标题)和X11库(根据您的问题)。这可能会对您有所帮助:如果您使用macports,您可以安装xorg-libx11,在LD_LIBRARY_路径上有什么?我在/opt/X11/include中有Xlib.h