What is ChaiScript
ChaiScript is an easy-to-use embedded scripting language for modern C++. It provides a simple scripting language and interface for modern C++(ChaiScript needs C++14 or above). It is a header only library. Therefor it is simple to deploy on almost any developing environment and only requires a C++11 enabled compiler to work.
Note that I code in Linux. So the compilation and settings in this blog will be mostly written for Linux.
The pros and cons of ChaiScript
Pros
- A minimal language
- Only needs a C++ compiler to build and run
- Written in modern C++
- Straight forward to use
- Cross Platform – It works on Windows, LInux, FreeBSD, OS X, Haiku OS, etc..
- It’s Freein both ways! (It costs nothing and it’s a libre sofware)
Cons
- A minimal language
- Takes a bit longer than usual program to compile
- No JIT. It only has a interpolator
- Written in modern C++ (won’t work with OLD compiler)
Installing ChaiScript
You need git, CMake and a building system (GNU Make, Ninnja, MSVC, etc..) to build ChaiScript’s test. I’m using GNU Make for all platforms.
On *nix (Linux/OS X/BSD/etc..)
It’s simple. just clone the repository, cmake, then make and make install.
git clone https://github.com/ChaiScript/ChaiScript cd ChaiScript cmake . make -j3 sudo make install
If you don’t want to wait for it to compile the tests and stuff. You can directly copy ChaiScript’s headers to your system’s include directory (Since that is what all ChaiScript is).
sudo cp include/ /usr/local/include/
However, running make will give you a interactive scripting shell by executing ./chai
for you to experiment with. It might be worth it to compile everything.
On Windows
I recommend to copy ChaiScript’s header manually to your compiler’s include directory.
Our first ChaiScript program.
Here is the Hello World for ChaiScript
//The ChaiScript header #include <chaiscript/chaiscript.hpp> int main() { //Create a ChaiScript inrerpolator chaiscript::ChaiScript chai; //Execute a line of code //The syntax R"(any string here)" introduced in C++11. Its called //Raw String Literal. Any escape charector or " and 's inside //the brackets have no effect and is taken as it is. chai.eval(R"(puts("Hello ChaiScript\n"))"); return 0; }
Compile this with the following command if you are on Linux or OS X or Windows with MSYS/Cywing/MinGW.
c++ ./chaihelloworld.cpp -o chaihelloworld -ldl -pthread -std=c++14
If you are developing using MSVC. Go and enable C++14 in your project settings and you should be good to go.
Then run it as normal. ./chaihelloworld
on Linux/Mac. It should show Hello ChaiScript after you run it as the picture shows.
How it works
First, we include the ChaiScript header.
#include <chaiscript/chaiscript.hpp>
Second, we create the interpolator
chaiscript::ChaiScript chai;
Last, we do something with the freshly created interpolator.
chai.eval(R"(puts("Hello ChaiScript\n"))");
In this case, we call chai.eval
to execute puts("Hello ChaiScript\n")
in ChaiScript. Note that because we are using the Raw String Literal. The \n
at the end is passed as it is to ChaiScript. Then the ChaiScript interpolator sees it, convert it into the new line character we all know. Then the puts
function prints it out.
This concludes our first tutorial ! In the next tutorial we will be looking briefly into the ChaiScript language itself.
Hi, I’m a little confused. Why would I want a scripting language in C++? It’s also possible I don’t understand the use case here. Say I had an application and wanted to add a scripting language to it, could I then use this?
LikeLike
Hello, The main use case for a scripting language inside C++ is to allow 3rd party programmers to write code for your application without linking to it or it’s base library.
For example. VOCALOID 3/4 supports Lua for devs to write specialized tools for musicians. And WOW uses Lua to allow addons.
Yes, that is the exact use case for languages like ChaiScript and Lua.
LikeLike