2

I want to copy a string to clipboard, after it being generated randomly. I could find the windows way of doing it (Windows.h) but how can I do it for linux?

An extract of my code:

void generate_and_copy_to_clipboard() {
    std::string output = "some-generate-string";
    // how can I copy `output` into the clipboard?
}
Enlico
  • 23,259
  • 6
  • 48
  • 102
wcyat
  • 67
  • 1
  • 2
  • 11
  • which windowing manager are You using? – Deumaudit Sep 06 '21 at 13:33
  • terminal, it's not gui (I am using wsl, do I have to use a desktop environment?) – wcyat Sep 06 '21 at 13:36
  • 1
    In case your window manager use GTK, you may take a look at [this](https://stackoverflow.com/questions/52204996/how-do-i-use-clipboard-in-gtk). And in case of Qt, take a look at [this](https://github.com/Arian8j2/ClipboardXX/blob/44b74b296642cf00eeaa4b7d3440b6688c25f2db/include/ClipboardXX.hpp#L88) – Deumaudit Sep 06 '21 at 13:41
  • Basically, copy [xclip.c](https://github.com/milki/xclip/blob/master/xclip.c#L275). I would go with `popen("xclip -sel clip"` and just writing data there - the end. – KamilCuk Sep 06 '21 at 14:11
  • But that's in C... (and I don't really understand C) – wcyat Sep 06 '21 at 14:19
  • 1
    it might help https://stackoverflow.com/questions/6436257/how-do-you-copy-paste-from-the-clipboard-in-c – Alessandro Teruzzi Sep 06 '21 at 14:35
  • 1
    You could use a cross-platform library like [GLFW](https://www.glfw.org/) which provides [clipboard functionality](https://www.glfw.org/docs/latest/input_guide.html#clipboard) among many other features. – janekb04 Sep 06 '21 at 17:34
  • Hi, I could not compile in visual studio: `unresolved external symbol glfwSetClipboardString referenced in function "void __cdecl generate(void)" (?generate@@YAXXZ)` – wcyat Sep 07 '21 at 15:33

0 Answers0