Linker problems in Ubuntu 11.10

2019-02-09 06:36发布

问题:

after upgrading to Ubuntu 11.10, I've found that many of my old and current developments can't be compiled anymore. I've reduced the problem to a simple example:

#include <X11/Xlib.h>

int main() {
    Display* display = XOpenDisplay(":0.0");
    XCloseDisplay(display);

    return 0;
}

Compiling it using:

g++ -lX11 test.cpp

or

g++ -c -o test.o test.cpp
g++ -lX11 -o test test.o

Causes a failure to happen:

/tmp/ccBAOpzy.o: In function `main':
test.cpp:(.text+0x11): undefined reference to `XOpenDisplay'
test.cpp:(.text+0x21): undefined reference to `XCloseDisplay'

Any ideas? I've found that some linker stuff has changed in 11.10:

https://wiki.ubuntu.com/NattyNarwhal/ToolchainTransition

But still doesn't explain these problems.

回答1:

g++ -lX11 -o test test.o

Above command is incorrect. Try this instead:

g++ test.o -lX11

Explanation of why the order matters here.

Also, you should never call your executables test on UNIX.