通过引用传递变量时出现"Undefined reference"错误

"Undefined reference" error when passing variable by reference

本文关键字:Undefined reference 错误 引用 变量      更新时间:2023-10-16

我对C++还很陌生,但这件事让我对任何逻辑都感到困惑。我的代码如下:

#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;

我包含了Xlib头,如果我单击Eclipse中的成员函数,它就会导航到定义。

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);
}

然后我为输入选择分配变量

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

但是当我加上&此处的活动。。。

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

整个事情分崩离析。

出于某种原因,将xevent作为引用传递会抛出整个Xlib标头,并给出以下内容:

00:16:15***配置的增量生成项目uniond的调试****使全部正在生成文件:/unisond.cpp调用:GCC C++编译器g++-O0-g3-Wall-c-f消息长度=0-MMD-MP-MF"unisond.d"-MT"unisond.d"-o"unisond.cpp"已完工建筑:/unisond.cpp建筑目标:unimond调用:GCC C++链接器g++-o"未绑定"/unisond.o./unisond.o:在函数"startTracker()"中:/home/ancarius/workspace/unisond/Debug//unisond.cpp:97:对"XOpenDisplay"的未定义引用/home/ancarius/workspace/unisond/Debug//unisond.cpp:98:对"XRootWindow"的未定义引用/home/ancarius/workspace/unisond/Debug//unisond.cpp:99:对"XSelectInput"的未定义引用./unisond.o:在函数"process()"中:/home/ancarius/workspace/unisond/Debug//unisond.cpp:105:对"XNextEvent"的未定义引用collect2:错误:ld返回1退出状态make:***[unisond]错误100:16:15构建完成(耗时159ms)

冒着被否决的风险,有人能解释一下我做错了什么吗?我想的都试过了,但没有成功。

看起来缺少用于链接的X11库。

-lX11添加到g++调用中。

这提供了所需的步骤。

右键单击项目文件夹>属性>C/C++构建>设置>GCC C++链接器>库>添加"X11"