对‘XOpenDisplay’未定义的引用

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/qq_32768743/article/details/89066716

解决
需要链接X11

代码

#include <stdio.h>
#include <X11/Xlib.h>
/*
* include <locale.h> or the non−standard X substitutes
* depending on the X_LOCALE compilation flag
*/
#include <X11/Xlocale.h>
#include <cstdlib>

int main(int argc, char *argv[]) {
    Display *dpy;
    XIM im;
    char *program_name = argv[0];
    /*
    * The error messages in this program are all in English.
    * In a truly internationalized program, they would not
    * be hardcoded; they would be looked up in a database of
    * some sort.
    */
    if (setlocale(LC_ALL, "") == nullptr) {
        fprintf(stderr, "%s: cannot set locale", program_name);
        exit(1);
    }
    if ((dpy = XOpenDisplay(nullptr)) == nullptr) {
        fprintf(stderr, "%s: cannot open Display", program_name);
        exit(1);
    }
    if (!XSupportsLocale()) {
        fprintf(stderr, "%s: X does not support locale %s", program_name, setlocale(LC_ALL, NULL));
        exit(1);
    }
    if (XSetLocaleModifiers("") == nullptr) {
        fprintf(stderr,"%s: Warning: cannot set locale modifiers.",program_name);
    }
    /*
    * Connect to an input method.
    * In this example, we don’t pass a resource database
    */
    if ((im = XOpenIM(dpy, nullptr, nullptr, nullptr)) == nullptr) {
        fprintf(stderr,"%s: Couldn’t open input method",program_name);
        exit(1);
    }
    printf("Done!");
    return 0;
}

配置

cmake_minimum_required(VERSION 3.13)
project(XlibDemo)
set(TARGET_NAME XlibDemo)
set(CMAKE_CXX_STANDARD 17)

add_executable(${TARGET_NAME} main.cpp)
target_link_libraries(${TARGET_NAME} X11)

猜你喜欢

转载自blog.csdn.net/qq_32768743/article/details/89066716