通过引用传递变量时出现"Undefined reference"错误
"Undefined reference" error when passing variable by reference
我对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"
相关文章:
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- Visual Studio Code "undefined reference to `WinMain@16'"
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 为什么创建友元类的实例会导致"undefined reference to"错误?
- Tensorflow c++ api undefined reference to 'tflite::D efaultErrorReporter()'
- 为什么在使用 SDL2 时仍然收到'undefined reference'链接器错误?
- OpenCV undefined reference to 'cv::imread(cv::String const&, int)'
- Libcurl c++ "undefined reference to" (Windows/MinGW/g++)
- 尝试使用 extern "C" 调用 C 中的C++方法,得到"undefined reference to"对象的链接器错误
- build error : undefined reference to `yyFlexLexer::yyFlexLex
- 安卓 NDK 制造。数百"undefined reference error"
- 当我尝试进行TPC-E测试时,实用程序抱怨"undefined reference"
- 声明和定义函数静态会产生"undefined reference to function_name()"
- C++ : "undefined reference to"可能是因为CMake文件
- 包含<iostream>时"Undefined reference"错误
- CMake 'undefined reference' CERN-ROOT 的错误
- 生成错误:"cannot find target for file"和"undefined reference"
- AppData\Local\Temp\cc59LXDc.o:test1.cpp: undefined reference to 'constructNewObjectOfClass
- YAML-cpp 编译错误:"undefined reference to YAML::LoadFile"
- 添加 cpp11 插件时出现错误消息"Undefined reference to boost (...)"