【问题标题】:"Undefined reference" error when passing variable by reference通过引用传递变量时出现“未定义的引用”错误
【发布时间】:2013-09-04 23:20:15
【问题描述】:

我对 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()
{

...但是当我在此处添加 &event 时...

    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 **** 项目 unisond 配置调试的增量构建 **** 做所有 构建文件:../unisond.cpp 调用:GCC C++ 编译器 g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"unisond.d" -MT"unisond.d" -o "unisond.o" "../unisond.cpp" 完成的建筑:../unisond.cpp 建设目标:统一 调用:GCC C++ 链接器 g++ -o "unisond" ./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:在函数“进程()”中: /home/ancarius/workspace/unisond/Debug/../unisond.cpp:105:未定义对“XNextEvent”的引用 collect2:错误:ld 返回 1 个退出状态 make: *** [unisond] 错误 1 00:16:15 构建完成(耗时 159 毫秒)

冒着被否决的风险,有人可以解释一下我做错了什么吗?我已经尝试了我能想到的一切,但没有运气。

【问题讨论】:

  • 乍一看,您似乎没有链接到 X11 库。所以链接器找不到你正在使用的函数。

标签: c++ eclipse gcc


【解决方案1】:

您似乎缺少用于链接的 X11 库。

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

This 提供所需的步骤。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多