CRAW++ is the C++ Reddit API Wrapper. CRAW++ is intended to be used as a library which can be included in a C++ project to access the Reddit API rather than making HTTP calls manually using libcurl
or some other library. CRAW++ strives to be as easy to learn and use as possible while still being feature-rich. This project exists mainly because previous attempts to create a C++ Reddit API wrapper were unmaintained and undocumented.
This project is unfinished.
This project depends on the following libraries:
libcurl
(installlibcurl4-openssl-dev
on Ubuntu/Debian)- nlohmann/json (
nlohmann-json3-dev
) - libcpr/cpr
The makefile only works with GCC. Any recent version of GCC will work. It must support C++17.
Build from source (recommended)
- Install the dependencies.
libcpr
can be installed off GitHub usingcmake
or usingvcpkg
; see libcpr/cpr for instructions.libcurl-openssl-dev
andnlohmann-json3-dev
should be included in your distribution's repositories. - Run
make
andmake install
. The Makefile will also runldconfig
for convenience.
Precompiled library
A .deb
package is available for 64-bit Debian and Debian-based operating systems (such as Ubuntu). Simply install this package in the usual apt install
fashion. That's all that needs to be done! This package was tested on Ubuntu 22.04 and Debian 11. Support is not guaranteed on any other distributions.
This package will also download, compile, and install libcpr
from its GitHub repository. If you don't want this, don't install the .deb
package.
Making the .deb
package
The .deb
package can be made using the Makefile with make debpackage
. This makes a package in the root directory that can be installed with dpkg
.
In general, it is far easier to use Windows Subsystem for Linux and develop in the Linux environment it provides than to attempt to set this up natively on Windows. It is also less prone to failure.
- Install G++. Follow Microsoft's instructions to install MingGW. Only the "prerequisites" section needs to be followed.Steps 1 and 2 can be skipped if you don't intend to use VS Code.
- Install
make
. You can do this through Chocolatey, the Windows package manager withchoco install make
, or use the "MSYS2 MSYS" program install with MinGW and runpacman -S make
. - Install dependencies.
cpr
andnlohman-json
can be installed usingvcpkg
.vcpkg
requires a compatible Visual Studio (not VS Code) instance to be install on your Windows PC. The Community edition of Visual Studio, which is free of charge, works for this. You do not have to use Visual Studio, butvcpkg
requires a file provided by it to install the dependencies. - Within your
vcpkg
folder, add\packages\nlohmann-json_x86-windows\include
andvcpkg\packages\cpr_x86-windows\include
to your include path - Compile the static library using
make
. Note thatmake install
does not work on Windows due to Windows having a different file structure. - Copy the static binary and the contents of the
include
folder to your working directory.
macOS is unsupported because I don't have a Mac to test this library with.
Documentation is automatically generated by Doxygen and is available at https://natenate60.xyz/crawpp. Please refer to the examples to get a rough idea of what this library can do.
Please note that all clients are required to authenticate using OAuth, and currently the only supported way to do this is to fetch API keys on your Reddit account. You must get API keys and then pass them to a Reddit
instance.
Here's a simple example which logs into Reddit and then makes a hello world post to r/test:
// everything you need is in craw.h
#include <crawpp/craw.h>
#include <iostream>
int main () {
CRAW::Reddit reddit = CRAW::Reddit("username",
"SUpeR_S3cuRe-Pa$$w0rd",
"client_id",
"api_secret",
"MyBotUserAgent/1.0");
// go to r/test
CRAW::Subreddit subreddit = reddit.subreddit("test");
std::cout << "r/test has " << subreddit.subscribers << "subscribers" << std::endl;
subreddit.subscribe();
CRAW::Post post = subreddit.post("This is the title!", "Hello world!");
CRAW::Comment comment = post.reply("Wow, this is a neat post! This is my comment!");
comment.del();
post.upvote();
subreddit.unsubscribe();
}
Compile this program like this:
g++ myprogram.cpp -lcrawpp -lcpr -o myprogram
Just remember to always tell the linker to link libcrawpp
and libcpr
.