0

I am not able to get the X11/Xlib.h header file included properly with CMake on CLion. Below is the CMakeLists.txt file:

cmake_minimum_required(VERSION 3.15)
project(X11 C)

set(CMAKE_C_STANDARD 11)
add_compile_options(-Wall -lX11)

add_executable(X11 main.c)

And the main.c file:

#include <stdio.h>
#include <stdlib.h>

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


int main(int argc, char **argv) {

    Display *dpy = XOpenDisplay(NULL);

    return EXIT_SUCCESS;
}

The error is undefined reference to 'XOpenDisplay'. The -Wall flag works fine and I get the unused variable warning for this snippet. Lastly, I am able to compile main.c from the command line without any problem:

gcc main.c -lX11
RayaneCTX
  • 573
  • 4
  • 13

1 Answers1

1

You are correct that XOpenDisplay is in libX11.a and linked with -lX11.

But maybe your libX11.a is not in the standard directories searched by the compiler. On my system (FreeBSD) it is in /usr/local/lib and is linked with -L/usr/local/lib -lX11.

So, find the directory your libX11.a lives in and add -L/path/to/directory to the link command.

I don't know much about CMake, but adding link options to something named add_compile_options does not sound right. Is there something like a add_link_options directive? -Wall is for compilation, but -L and -l are for linking.

Jens
  • 69,818
  • 15
  • 125
  • 179
  • Thank you for the insight. The correct command as shown in the linked post is `target_link_libraries()` and adding it after CMake adds the executable. – RayaneCTX Apr 10 '20 at 15:26