0

I am trying to link a Wayland C++ application with g++ and got stuck. It should be possible to link to the Wayland libraries from C++ since the headers (wayland-core.h, wayland-client.h etc.) correctly put the API calls into an extern "C" scope.

I am using Ubuntu 20.04 LTS. Wayland is enabled and all libraries are installed:

 $ sudo apt-get update
 $ sudo apt-get install libwayland-client0 libwayland-dev libdrm-dev
 $ sudo apt-get install wayland-protocols

I can compile a simple program that connects to the wl_display singleton but linking is not possible:

 $ pkg-config --cflags --libs wayland-client
 -lwayland-client
 $ pkg-config --variable=libdir wayland-client
 /usr/lib/x86_64-linux-gnu
 $ g++ -std=c++17 -pthread -Wall -lwayland-client -L/usr/lib/x86_64-linux-gnu -o hello hello.cpp
 hello.cpp:(.text+0x1d): undefined reference to `wl_display_connect'
 /usr/bin/ld: hello.cpp:(.text+0x5c): undefined reference to `wl_display_disconnect'

There is another thread but they are linking a C program which works on my system too. Is it not possible to link C++ for some reason?

Thank you.

Andreas Spindler
  • 7,568
  • 4
  • 43
  • 34
  • 2
    The order of the link options matter. Try putting `-lwayland-client` at the end of the command. – kaylum Mar 23 '22 at 09:06
  • Only an idea: Have wayland headers also forget to have C++ guards like `extern "C" {}`? There is so much fully broken with wayland... – Klaus Mar 23 '22 at 09:06
  • @Klaus See above. Yes. – Andreas Spindler Mar 23 '22 at 09:08
  • Ubs.. sorry, to fast, to early, lack of coffee ?? :-) – Klaus Mar 23 '22 at 09:10
  • @kaylum Played with the position of `-lwayland-client` but still no luck. – Andreas Spindler Mar 23 '22 at 09:12
  • Also tried on my system with `pkg-config`. Even if I also have installed all and everything for wayland development, pkg-config reports an empty list for flags AND libs. As said: I have only very bad experience with wayland. Starts by such things like your question and end up by all the missing features like touch screen calibration or multiple multi touch support, pure remote session functionality, safety aspects... In fact: We only use X11 for production! It is much to early to go with wayland, even if the project has decades in history :-) – Klaus Mar 23 '22 at 09:15
  • @AndreasSpindler if it was linked correctly, let check the symbol in wayland library `nm -gD | grep wl_display_disconnect` – long.kl Mar 23 '22 at 09:33
  • @long.kl This is the strange part. In */usr/lib/x86_64-linux-gnu/libwayland-client.so* all functions are listed, including of course `wl_display_connect` and `wl_display_disconnect`. – Andreas Spindler Mar 23 '22 at 09:37
  • @AndreasSpindler maybe like Klaus said, extern "C" didn't work. you can verify it with `g++ -std=c++17 -pthread -Wall -lwayland-client -L/usr/lib/x86_64-linux-gnu -c -o hello.o hello.cpp && nm hello.o ` to see what g++ did with the hello.cpp. to verity `wl_display_disconnect` is demangled or not . – long.kl Mar 23 '22 at 09:52
  • Fixed it. This works: `g++ -o hello hello.cpp -lwayland-client`. The trick is to put `-l` last. Using gcc 9.4 btw. – Andreas Spindler Mar 23 '22 at 10:05
  • Besides removing unnecessary options do you now agree that it is the order of the link options that matter here? – kaylum Mar 23 '22 at 10:08
  • @kaylum The options are not unnecessary for me (except maybe `-L`). I just removed them for clarity. I agree that the link options matter but I was not aware that `-l` has to come after `-o`. – Andreas Spindler Mar 23 '22 at 10:10
  • 1
    It doesn't have to come after `-o`. It has to come after the object(s) that uses it. So in your case after `hello.cpp`. For example, this should still work `g++ hello.cpp -lwayland-client -o hello`. See the duplicate post for more details. – kaylum Mar 23 '22 at 10:13
  • @kalym Yes. It does. Thank you. Haven't wrote a plain cc call for a while and used to much cmake I guess. – Andreas Spindler Mar 23 '22 at 10:15

0 Answers0