0

I have properly installed the XQuarts.app but have the linking phase problem.
Here is the simple code.

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xresource.h>

int main() {
    Display* dis = XOpenDisplay(NULL);
    return 0;
}

When i compile it from XQuartz.app like this

g++ -c main.cpp -I/opt/X11/include

However it outputs:

Undefined symbols for architecture x86_64: "_XOpenDisplay", referenced from: _main in main.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) make: *** [default] Error 1

My mac version:

Yosemite 10.10.2

I googled a lot but couldn't find any useful help. Thanks in advance.

1 Answers1

1

You aren't linking against libX11. Add the following flags to the linker command:

-L/opt/X11/lib -lX11

Not critical, but worth noting: You do not need to use xterm (the terminal in XQuartz) to build or run X11 applications.

  • It works. Can you elaborate more about "Why" this works ? It would be extremely helpful. – Igor Tarlinskii May 23 '17 at 00:08
  • The `XOpenDisplay` function is part of libX11. If you don't link against that library, the linker has nowhere to get that function from. –  May 23 '17 at 00:09
  • And why doesn't this line of code `#include ` automatically induces compiler to link this library ? – Igor Tarlinskii May 23 '17 at 00:15
  • Because that's not how compilers work. An `#include` gives instructions to the preprocessor; linker flags are used at a much later stage. –  May 23 '17 at 00:19
  • @duskwuff For what it's worth, MSVC provides a `#pragma lib` that embeds a library name in compiled object files that then gets added to the libraries specified on the linker command line. Though personally, I've usually found that to be more of a misfeature than a feature... – Daniel Schepler May 23 '17 at 00:40