How to Copy to Clipboard with C++

Excellent for creating those GUI buttons: ▣ Copy / ✔ Copied!

How to Copy to Clipboard with C++

There are several C++ libraries to do this. But each one has its own peculiarities, however, we will mention the majority at the end of this article.

In this example we will see how to use Clip.

Clip is a cross-platform C++ library (Windows, macOS and GNU/Linux) for copying/pasting content from the clipboard.


The simplest way to use is:

1. Create an example project and enter it:

mkdir -p myproject
cd myproject

2. Clone the Clip for your project:

git clone

3. Create a basic C++ file that, after running the binary, will copy the string:


#include "clip/clip.h"

int main(){
   clip::set_text("This text will be copied to the clipboard");
   return 0;

4. Create a CMake to build the binary:


cmake_minimum_required(VERSION 2.8)
add_executable(a.out main.cpp)
target_link_libraries(a.out clip)

Change the CMake version to your version, I put this version for compatibility reasons, but there will probably be a warning when running it, but it will work!

5. Build

cmake . -B build
cd build && make

When running the binary, try typing: Ctrl + v somewhere else and notice that the content was copied!

NOTE: This may not work in window managers, you need to configure files for compatibility, despite the xclip command work normally!

But, I tested it on GNOME and Windows and it worked perfectly!

For more information, visit the Clip repository.

And as I said, there are many options for it, including: