Using Eclipse IDE for EOS smart-contracts
Recently we've been trying to find an efficient method to work with EOS smart-contracts. For two reasons. Firstly, we wanted to be able to thoroughly examine the way the examples supplied by EOS developers are constructed. Secondly, we wanted to have a tool which will allow us to play with our own attempts at creating smart-contracts.
Not smart enough
Working with complex code in a plain text editor is not an option for us. We are not smart enough to deal with complex smart-contracts without extra help.
We needed a tool which understands the structure of the code, so it can present it to us in all different aspects and show us all the intricate relationships inside the code. Also, we needed a tool which will point out errors and produce warnings in our newly created code, so that we can spot and fix them before attempting to send it to the WebAssembly compiler.
In other words, what we needed was an IDE (Integrated Development Environment).
Here comes Eclipse
And after several attempts with various tools we've discovered that Eclipse CDT (C/C++ Development Tooling) can do the job quite well.
Importing EOS smart-contracts into Eclipse is a trivial task. It just worked without any extra efforts on our part. It turns out Eclipse code indexer engine is smart enough to correctly parse the latest C++14 standard which is being used by EOS devs.
As expected, in Eclipse the demo smart-contracts have become much clearer.
We could see the entire structure of the code:
We could select any method and see all its references:
We could see the entire call hierarchy of a given method:
And last but not least, when working with a new code we could utilize the powerful refactoring tools offered by Eclipse.
How to do it?
Firstly, you need to create a copy of EOS repository on your computer:
git clone https://github.com/eosio/eos --recursive
Then you need to make sure you have Java 8 or 9 installed:
If not, you can get it here.
And finally you need to download Eclipse CDT, which is a special edition dedicated to C++ development.
Where are the demo contracts?
Inside the repository, demo smart-contracts are located in the
contracts folder. There are several of them, here is a list of those which do work:
Additionally you will need the header files contained in the
contracts/eoslib folder. Demo contracts are dependent on them.
Use symbolic links
In order not to mess up your local EOS repository with Eclipse workspace files, we suggest creating Eclipse workspace outside the repository and then using symbolic links. This way you can easily make sure the demo contracts are always up to date by running
git pull --recursive, while you can keep the Eclipse stuff outside of the repository.
To create a symbolic link you need to execute this command:
-- on Windows:
mklink /D path-to-destination-folder path-to-source-folder
-- on Ubuntu:
ln -s path-to-source-folder path-to-destination-folder
path-to-source-folder with the path to a smart-contract example in your EOS repository, and
path-to-destination-folder with a path to a smart-contract example in your Eclipse workspace.
You need to run a separate command like this for every demo contract. For example this command creates a symbolic link for the currency demo contract:
mklink /D X:\Workspaces\EOS\WebAssembly\Contracts\src\currency X:\Workspaces\EOS\eos\contracts\currency
Can we achieve more?
Switching to Eclipse was a big improvement for us but we wondered if we could push it one step further - compile the code right inside Eclipse, so that we have the code 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 discover syntactic errors (as well as some semantic onones), 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 is quite tricky. We will describe it in a separate post. So stay tuned for more on this subject.