EOS contracts: Development with the CLion IDE

in #eos6 years ago (edited)

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 instructions
  • src - the CPP files for constracts
  • include - the HPP files 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 type to Release
  • Set CMake options to -DCMAKE_TOOLCHAIN_FILE=Toolchain.cmake

intellij-eos-setup.png

The project should build now build in CLion, and the "code insight" features should function, as indicated in the screenshot below.

intellij-eos2.png

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!

Sort:  

Great!!! it's really save me time. could I translate to chinese?

I'll try to add in wasm complication support as well, it could be good to wait a day or so until I finish the article. Please do not forget to upvote if you like the content :)

I have updated the tutorial now, feel free to translate it, but please also link back to this article :)

sure, already upvote.

Got below error when run the make command:

build ➤ cmake -DCMAKE_TOOLCHAIN_FILE=../Toolchain.cmake ..
-- The CXX compiler identification is Clang 4.0.1
-- Check for working CXX compiler: /usr/local/wasm/bin/clang++
-- Check for working CXX compiler: /usr/local/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: /Users/someone/workspace/eos-clion-example/build
build ➤ make
Scanning dependencies of target link
[ 25%] Building CXX object CMakeFiles/link.dir/src/hello.cpp.o
[ 50%] Linking CXX shared library liblink.dylib
clang-4.0: error: -emit-llvm cannot be used when linking
make[2]: *** [liblink.dylib] Error 1
make[1]: *** [CMakeFiles/link.dir/all] Error 2
make: *** [all] Error 2

I have updated the Github repository for Dawn 3.x please try again!

I met the same problem with you, did you solve it?

I have updated the Github repository for Dawn 3.x, please give it a try!

not able to make it working ... tried many times ... guess a more detailed guide is required.

well, i suppose there are some issues to sort out. you can ping me on telegram at my handle @ukarlsson if you would like more help

so to tell you that your blog and the code is not in sync. whatever you are trying to tell us here in the post it is not at all in sync with the repository so its very hard for the people to follow.

EOSIO_DAWN does not exist in the cmakelist file. How can I switch back to DAWN 2.0?

It does not support DAWN 2.0 anymore, only the latest version of EOS

Congratulations @ukarlsson! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

Click here to view your Board

Do not miss the last post from @steemitboard:

Carnival Challenge - Collect badge and win 5 STEEM
Vote for @Steemitboard as a witness and get one more award and increased upvotes!

It happens more and more often, whenever I'm looking for good tutorial I end up on steem network.
Good job man!

Congratulations @ukarlsson! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Do not miss the last post from @steemitboard:

Use your witness votes and get the Community Badge
Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.30
TRX 0.12
JST 0.034
BTC 64513.75
ETH 3146.11
USDT 1.00
SBD 3.95