【问题标题】:Linking error in XCode when using Xlib使用 Xlib 时 XCode 中的链接错误
【发布时间】:2013-12-06 13:47:21
【问题描述】:

当我尝试在 XCode(版本 5)中构建一个小型 C++ X11/Xlib 程序时,出现以下“Apple Mach-O Linker (Id) Error”。

错误:

代码:

#include <iostream>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <string>

using namespace std;

int main(int argc, const char * argv[])
{
    Display *d;
    Window w;
    XEvent e;
    string msg = "Hello, World!";
    int s;

    d = XOpenDisplay(NULL);
    if (d == NULL) {
        fprintf(stderr, "Cannot open display\n");
        exit(1);
    }

    s = DefaultScreen(d);
    w = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, 100, 100, 1,
                            BlackPixel(d, s), WhitePixel(d, s));
    XSelectInput(d, w, ExposureMask | KeyPressMask);
    XMapWindow(d, w);

    while (1) {
        XNextEvent(d, &e);
        if (e.type == Expose) {
            XFillRectangle(d, w, DefaultGC(d, s), 20, 20, 10, 10);
            XDrawString(d, w, DefaultGC(d, s), 10, 50, msg.c_str(), (int)msg.length());
        }
        if (e.type == KeyPress)
            break;
    }

    XCloseDisplay(d);
    return 0;
}

我已按照another post 此链接器中的建议将 /opt/X11/include/opt/X11/lib 添加到构建设置中的标头搜索路径中但是仍然出现错误。

我目前运行的是 OSX 10.8.5,并且我已经安装了 XQuartz

我只是缺少构建设置,还是可能需要安装一些我忽略的东西?

任何帮助将不胜感激!

【问题讨论】:

    标签: c++ xcode macos x11 xlib


    【解决方案1】:

    原来我收到这些错误是因为我没有在 XCode 中正确设置标志。

    要解决此问题,请转到项目的构建设置并将标志添加到“其他链接器标志”:

    -I/usr/X11R6/include -L/usr/X11R6/lib -lX11
    

    【讨论】:

      【解决方案2】:

      这只是意味着您没有链接库。请查看构建日志并查看是否正在链接库。

      如果没有,则链接它。如果它仍然链接,那么它可能没有功能。

      【讨论】:

      • 在哪里可以查看构建日志?
      • 左窗格中有一个按钮显示日志导航器。
      • 文件编译正常,但链接不正确。你知道如何链接 Xlib 库吗?
      • 编译工作如下:'cc -o "main" "main".cpp -I/usr/X11R6/include -L/usr/X11R6/lib -lX11'。我尝试将标志“-I/usr/X11R6/include -L/usr/X11R6/lib -lX11”添加到 Xcode 中的其他 C 标志中,但我仍然遇到错误。你知道我如何将类似该命令的内容转换为 XCode 构建设置吗?
      • 转到 Xcode 中的构建设置,然后到 Linking->Other Linker Flags 并添加这些选项。
      猜你喜欢
      • 1970-01-01
      • 2015-06-07
      • 2014-12-01
      • 2012-09-10
      • 1970-01-01
      • 2011-06-27
      • 1970-01-01
      • 1970-01-01
      • 2015-06-29
      相关资源
      最近更新 更多