Skip to content

Instantly share code, notes, and snippets.

@kristopherjohnson
Last active January 2, 2024 04:55
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kristopherjohnson/b31bb38cf6485b83f654 to your computer and use it in GitHub Desktop.
Save kristopherjohnson/b31bb38cf6485b83f654 to your computer and use it in GitHub Desktop.
Simple example of using the readline library from C++
CXXFLAGS=-I/usr/local/include --std=c++11
LDFLAGS=-L/usr/local/lib -lreadline
rltest: rltest.cpp
clean:
- /bin/rm rltest
// Simple test of readline
#include <iostream>
#include "readline/readline.h"
#include "readline/history.h"
using namespace std;
int main(int argc, char** argv)
{
const char *line;
while ((line = readline("? ")) != nullptr) {
cout << "[" << line << "]" << endl;
if (*line) add_history(line);
free(line);
}
return 0;
}
@distrakt
Copy link

distrakt commented Jan 2, 2018

Just what I needed, thank you!

Changed slightly to work on Mac OS X 10.12:

#include
#include <readline/readline.h>
#include <readline/history.h>
using namespace std;
int main(int argc, char** argv)
{
const char *line;
while ((line = readline("? ")) != nullptr) {
cout << "[" << line << "]" << endl;
if (*line) add_history(line);
free((void *)line);
}
return 0;
}

And compiled directly with g++ rl.cpp -lreadline

@demon90s
Copy link

I love this code, thanks very much

@RaidenTaisha
Copy link

This code is very helpful, thanks for this example!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment