Compiling EOS smart-contracts in Eclipse using LLVM/Clang

in #eos7 years ago (edited)

In our previous post we described how to use Eclipse IDE to examine the demo smart-contracts delivered by EOS devs. In this post we present how to go one step further: compile the code right inside Eclipse and have it additionally verified by a C++ compiler.

The reason we would want this is that the code indexer is only able to discover syntactic errors (as well as some semantic ones), while the compiler would offer us the ultimate verification: everything except run-time errors.

As EOS is using the experimental C++14 standard, setting up a C++ compiler capable of this task requires some additional steps.


Set up LLVM compiler

First you need to grab the very latest version of C++ LLVM compiler. Download and install LLVM 5.0.0. We recommend using the pre-built binaries, but if you want you can build it from source.

If you're on Windows the installer will set everything up for you. If you're on Linux you'll need to add the bin folder of your LLVM/Clang to your PATH configuration on your own.

Add LLVM support to Eclipse

Open Eclipse CDT and navigate to Help > Install New Software and then select Work with: --All available sites--.

Once the list loads, expand Programming Languages and select C/C++ LLVM-Family Compiler Build Support. Then proceed with the plug-in installation and re-launch Eclipse if prompted.

Configure Eclipse

  • In the top menu bar, navigate to Window > Preferences.
  • Select C/C++ > LLVM in the menu tree on the left.
  • Fill in the LLVM installation folder field by entering the path to your bin folder of your LLVM installation. In Windows it will be something like C:\Program Files\LLVM\bin and on Linux the path will depend on the location you've chosen when setting up LLVM.

Create a new project

Now we are ready to create a static library project in Eclipse. Navigate to File > New > C++ Project and then:

  • Enter the name of your project.
  • On the left-hand side expand the Static Library option and choose Empty Project.
  • If you're on Windows your choice for the toolchain on the right-hand side will be LLVM with Clang (Windows) (MinGW) . If you're on Linux, choose LLVM with Clang (Linux).

Then proceed to the next step, accept the default settings and click Finish.

Import the source code

Add a new source folder to your project by right-clicking it and choosing New > Source Folder. Call it whatever you want, we recommend the name src.

Import the source code of the demo smart-contracts to the newly created source folder by either coping them from EOS repository or by creating symbolic links, as described in our previous post. We recommend the latter as it will allow you to keep track of the updates made by EOS devs.

NOTE: Make sure that you also copy or link the eoslib folder, as all demo-smart contracts depend on the C++ header files located there.

Update include directories

The source folder of your project needs to be part of the include directories.

Navigate to Project > Properties and then select C/C++ General > Paths and Symbols on the tree menu. In the Includes tab click Add and check both Add to all configurations and Add to all languages. Then click Workspace... and browse to the source folder of your project.

Fix the source code

As the last step before attempting to compile the code, you'll need to make a small correction in the eoslib/types.h file. Open this file and replace those two lines:

typedef unsigned _int128    uint128t;
typedef _int128             int128t;

with those two lines:

typedef __uint128_t          uint128_t;
typedef __int128_t           int128_t;

NOTE: if you've included the test_api folder in your project's source, you'll need to make similar corrections in those two files: test_api.hpp (line 44) and test_math.hpp (lines 26 & 27).

Why do you need those corrections? Apparently there are some compiler incompatibilities regarding the naming convention for the newly introduced C++ type called int128. If anyone can explain the background of the problem in more details and/or suggest a more permanent fix that could be turned into a pull request for EOS repository, please do so in the comments.

Build all

Now you should be able to build the following demo smart-contracts: currency, exchange, infinite, proxy, simpledb, storage.

To build the project just run Project > Build All . It should take a couple of seconds. When it's finished make sure you don't get any errors in the Eclipse console - there will be a couple of warnings, but there should be no errors.


Did you guys have to do a TON of clean up to the files to get the compile? I am getting complains about all kinds of stuff.

I have a setup for working with EOS contracts in the CLion IDE here outlined here:

It can also build the wast and abi files directly in the IDE.