c++ - 错误 : "undefined reference to" while compiling c++

标签 c++ linker-errors undefined-reference

我在一个小的 C++ 应用程序中工作,我正在尝试使用 xdotool (libxdo: https://github.com/jordansissel/xdotool)。

我使用“make”命令构建了 xdotool,并将 libxdo.so 和 libxdo.so.3 放入/usr/lib。和 xdo.h 到/usr/local/include。

我正在尝试使用以下方法编译我的应用程序:

g++ -I /usr/local/include/ -L /usr/lib/ LinuxTest.cpp -lXtst -lX11 -lxdo

但是我得到这个错误:

undefined reference to `xdo_new(char const*)'
undefined reference to `xdo_move_mouse_relative(xdo const*, int, int)'

这是我的源代码:

#include <iostream>
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/X.h>
#include <unistd.h>
#include <X11/extensions/XTest.h>
#include <xdo.h>

using namespace std;

#define KEYCODE XK_Tab
int mapa[2048];
void hook();

xdo_t* xdoMain;

int main() {
    for (int i=0;i<2048;i++){
       mapa[i]=0;
    }
    xdoMain = xdo_new(NULL);
    xdo_move_mouse_relative(xdoMain,200,200);
    hook(); //do some things using X11
    return 0;
}

最佳答案

我猜这是因为 xdo 是一个 C 库。
您正在链接和构建 C++ 应用程序。

因此您的编译器认为 xdo_new() 是一个 C++ 名称错位的函数。但实际上它已链接到 libxdo 中。作为 C 名称损坏的函数。

我会试试这个:

extern "C" {
#include <xdo.h>
}

您基本上是在告诉编译器将 xdo 中的所有名称都视为 C 函数声明。只要没有类,这就应该有效(如果有类,那么我的假设一开始就不正确)。

关于c++ - 错误 : "undefined reference to" while compiling c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30293012/

相关文章:

c++ - 对 'inflateInit2_' 的 undefined reference

c++ - 使用 CImg : LNK1181: cannot open file "m.lib" on windows 7 x64

c++ - 链接到 Lua 5.2.4 的问题,即使在内联编译源代码时也是如此

c++ - 无法访问静态方法 (C++)

c++ - c++ 的 makefile 错误

c++ - 为某事获取错误 "...does not have a name type"

c++ - 我在链接 SDL 库时遇到问题

c++ - C++中变量的声明

c++ - 模板化类的非模板化接口(interface) C++

c++ - 什么是 undefined reference /未解析的外部符号错误以及如何修复它?