TL;DR
https://github.com/ukarlsson/eos-clion-example
Update (2018-03-05): The CMakeLists.txtnow supports building the wastbinary. No need to interact with eosiocpp anymore, just press Ctrl+F9in CLion and the build should be ready in a few seconds!
This tutorial describes how to set up a development environment for EOS contracts targeting Dawn 2.0 or Dawn 3.0 with the CLion IDE
In order to effectively write C++ code, a good IDE is a must. JetBrains offers CLion, which is a decent IDE for C++, that supports all the "code insight" features that one would expect from a modern IDE. CLion is a commercial product, but they also offer a free trial version.
https://www.jetbrains.com/clion/
CLion uses CMake for building project, and thus, in order to start writing EOS contracts with CLion, we need a CMakeLists.txt file that instructs CLion how to perform the build. The CMakeLists.txt for building EOS contracts requires a few special configurations since we must use the wasm32 cross compiler.
In addition to making CLion correctly resolve its dependencies, enabling the "code inisights" features, the CMakeLists.txt will contain instructions for building both the wast file that is necessary for injecting the contract into the blockchain. This will means that the CMakeLists.txt effectively replaces eosiocpp, that is traditionally used to build contracts.
In this guide we will start with the "Hello World" skeleton provided in my repository, and first build it manually, and then import it into CLion.
$ git clone https://github.com/ukarlsson/eos-clion-example
Cloning into 'eos-clion-example'...
remote: Counting objects: 22, done.
remote: Compressing objects: 100% (15/15), done.
remote: Total 22 (delta 6), reused 19 (delta 3), pack-reused 0
Unpacking objects: 100% (22/22), done.
Now enter the directory eos-clion-example and note the following structure:
CMakeLists.txt- the CMake build instructionssrc- theCPPfiles for constractsinclude- theHPPfiles for contracts
The CMakeLists.txt assumes the default locations for EOS and its dependencies. If these components are in custom locations, the CMakeLists.txt must be modified accordingly. In order to switch between EOS Dawn 2.0 and EOS Dawn 3.0, change the value of the variable EOSIO_DAWN. The name of the project can also be changed by changing the project(...) statement inCMakeLists.txt, it is currently set to hello.
The next step is to test building the contract with CMake. This build hello.wast binary. Thus, create a build directory, enter it, and then execute cmake and finally execute make:
$ mkdir -p build
$ cd build
$ cmake -DCMAKE_TOOLCHAIN_FILE=../Toolchain.cmake ..
-- The CXX compiler identification is Clang 4.0.1
-- Check for working CXX compiler: /home/ukarlsson/opt/wasm/bin/clang++
-- Check for working CXX compiler: /home/ukarlsson/opt/wasm/bin/clang++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/ukarlsson/eos-clion-example/build
$ make
Scanning dependencies of target link
[ 25%] Building CXX object CMakeFiles/link.dir/src/hello.cpp.o
[ 50%] Linking CXX shared library liblink.so
[ 50%] Built target link
Scanning dependencies of target wast
[ 75%] Generating hello.s
[100%] Generating hello.wast
[100%] Built target wast
Scanning dependencies of target assembly
[100%] Built target assembly
The output above indicates that the build was successful. The final output of the build is is the hello.wast binary.
Now, we are ready to open our project in CLion! Select File ➜ Import Project... and then Open Project.
There are a few important adjustments that need to be made to the settings in order for the build to succeed. Select File ➜ Settings ➜ Build, Execution, Deployment ➜ CMake and make the following adjustments:
- Set
Build typetoRelease - Set
CMake optionsto-DCMAKE_TOOLCHAIN_FILE=Toolchain.cmake
The project should build now build in CLion, and the "code insight" features should function, as indicated in the screenshot below.
In this document we have outlined how to make development of EOS contracts less cumbersome by utilizing the CLion IDE. I hope this has been useful, and please do not hesitate to give some reward in that case!
The code for this example can be found here:
https://github.com/ukarlsson/eos-clion-example
Thanks!