2
votes

I'm fairly new to C++ but this thing has me baffled by any logic. My code is as follows:

#include "stdlib.h"
#include "syslog.h"
#include "unistd.h"
#include "sys/stat.h"
#include "X11/Xlib.h"
#include "cstdio"

void process();
void startTracker();

Display *display;
Window rootWindow;
XEvent xevent;

I have the Xlib header included and if I click on member functions in Eclipse it navigates to the definitions.

int main(int argc, char *argv[])
{
    // set logging up
    openlog("unison", LOG_CONS|LOG_PID|LOG_NDELAY, LOG_LOCAL1);

    syslog(LOG_NOTICE, "Starting Unison Handler");

    pid_t pid, sid;

    pid = fork();

    // fork failed
    if (pid < 0) {
        exit(EXIT_FAILURE);
    }

    if (pid > 0) {
        exit(EXIT_SUCCESS);
    }

    umask(0);

    sid = setsid();
    if (sid < 0) {
        exit(EXIT_FAILURE);
    }

    if (chdir("/") < 0) {
        exit(EXIT_FAILURE);
    }

    close(STDIN_FILENO);
    close(STDOUT_FILENO);
    close(STDERR_FILENO);

    startTracker();

    while (true) {
        process();
    }

    closelog();
    return(EXIT_SUCCESS);
}

Then I assign the variables for input selection

void startTracker() {
    display = XOpenDisplay(0);
    rootWindow = XRootWindow(display, 0);
    XSelectInput(display, rootWindow, PointerMotionMask);

}

void process()
{

...but when i add the &event here...

    XNextEvent(display, &xevent);
    switch (xevent.type) {
        case MotionNotify:
            syslog(
                    LOG_NOTICE,
                    "Mouse position is %dx%d",
                    xevent.xmotion.x_root, xevent.xmotion.y_root
            );
    }
}

...the whole thing falls apart.

For some reason passing the xevent as reference throws off the entire Xlib header and gives me this:

00:16:15 **** Incremental Build of configuration Debug for project unisond ****
make all 
Building file: ../unisond.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"unisond.d" -MT"unisond.d" -o "unisond.o" "../unisond.cpp"
Finished building: ../unisond.cpp

Building target: unisond
Invoking: GCC C++ Linker
g++  -o "unisond"  ./unisond.o   
./unisond.o: In function `startTracker()':
/home/ancarius/workspace/unisond/Debug/../unisond.cpp:97: undefined reference to `XOpenDisplay'
/home/ancarius/workspace/unisond/Debug/../unisond.cpp:98: undefined reference to `XRootWindow'
/home/ancarius/workspace/unisond/Debug/../unisond.cpp:99: undefined reference to `XSelectInput'
./unisond.o: In function `process()':
/home/ancarius/workspace/unisond/Debug/../unisond.cpp:105: undefined reference to `XNextEvent'
collect2: error: ld returned 1 exit status
make: *** [unisond] Error 1

00:16:15 Build Finished (took 159ms)

At the risk of getting downvoted could someone please explain what I've done wrong? I've tried everything I could think of but no luck.

1
at first glance it looks like you're not linking to the X11 lib. so the linker can't find the functions you're using.dsu

1 Answers

4
votes

It looks like you are missing the X11 library for linking.

add -lX11 to the g++ invocation.

This provides the steps required.

Right click on Project Folder> Properties> C/C++ Build > Settings > GCC C++ Linker > Libraries > add "X11"