Every developer needs a good IDE which is set up for the development process of his project. This is why we created a quick tutorial on how to setup your VS Code and/or CLion for EOS dApp Development.
We’re also created a few scripts for VS Code which will automate some of the commands you use in the terminal.
Visual Studio Code Setup
In the first place install a few VS Code extensions if you don’t have them yet. They will be very helpful through your dApp development:
- C/C++ – IntelliSense, debugging, and code browsing for VS Code
- CMake – CMake language support for Visual Studio Code
- CMake Tools – Extended CMake support in Visual Studio Code
- WebAssembly – Syntax highlight for WebAssembly textual representation
When we’re developing EOSIO dApps we’re writing our code in .hpp and .cpp files. However, this is a very small part of the whole process. Most of the time we’ll need to generate some other files which we’ll use to deploy the contract on the blockchain, to unit test it and etc. This is where CMake comes in handy.
CMake is a command line tool used to control the software compilation process. It makes the whole thing very easy once it is set properly inside your IDE.
Now that we’re going to use the CMake tool we should make some changes in our project structure. We will reuse the skeleton of the EOSIO project as it has everything we need. Of course with some small changes by us.
We have a picture which shows the new project structure. Let’s take a look.
In the first place, we have the build folder. This is where all the build content is placed. Every generated file you will use is there. Next is CMakeModules which contains some helpful cmake modules with custom functions for the compilation process.
The contracts is our core folder. This is where we’re going to place our smart contracts. Currently, eosiolib, libc++ and musl are there by default as there are used for the compilation. Next in the row are externals & libraries. Both folders contain libraries which are used to make the whole compilation process much easier.
The last important thing from the project structure is the configuration files – CMakeLists.txt. Each directory has its own CMakeLists.txt file with commands.
You can find the new project structure with all folders and scripts inside our repo here
Let’s take a look at some of the configuration files as you need to know how to work with them.
- CMakeLists.txt (4)
This is the main configuration file which set up the compilation process. You should know that when you’re developing your dApp you need to set the project name. Version and language are optional.
2. CMakeLists.txt (3)
The second configuration file is inside the contracts folder. Every new smart contract should be added as a subdirectory in this configuration. It’s important to don’t forget this step as the contract won’t compile. CMake wouldn’t know about it.
3. CMakeLists.txt (2)
Each smart contract has its own configuration file. Here you need to take a note that each contract has a different TARGET, basically, it’s the name of the folder.
Now when we have the new project structure, we have to make custom commands which will compile and build everything we have done. But how? Fortunately, VS Code has some pretty cool thing called Tasks. It helps us to automate every command to just a few clicks.
Tasks in VS Code
First, we have to generate the tasks.json file which will contain our custom commands. Press ⇧+⌘+P to open the command palette in VS Code and then type “Tasks” and select “Configure Task“.
As next step select “Create tasks.json file from template” and then “Others”
VS Code will create a folder called “.vscode” and inside it, you can find the tasks.json. Now we need to add our commands. Copy and paste the code below into tasks.json:
We have created three custom commands named CMake, Build and Generate ABI. They execute three shell scripts – compile.sh, build.sh & generate.sh. The first two scripts basically do the same except that build.sh compile & build at the same time. Probably most of the time you’ll use the second one.
On the other hand the third script – generate.sh (Generate ABI) is used to generate the abi of the smart contract. It is needed during building to generate some of the files. You must execute the command inside the folder where your contract is. Select for example the .cpp file and run it.
Awesome! We’re almost ready with the VS Code set up. To make the whole thing even easier we’re going to create shortcuts for our commands. While you’re still in VS Code go to Preferences – Keyboard Shortcuts. A shortcuts window will open – find and open the keybindings.json (it’s on the top):
Once keybindings.json is opened we’ll create our shortcuts. For our commands, we have chosen “cmd+e“, “cmd+r” and “cmd+i” but you can choose others. This is the json you have to add:
Once you’ve done everything you are now ready to develop EOS dApps on VS Code 🙂
Setting up CLion is really easy compared to VS Code. When you load the skeleton in CLion the IDE automatically create all build files inside the cmake-build-debug folder. Once it is ready you can perform the actual build using ⌘+F9 shortcut. And this is everything you should do, too easy right?
However, if you want to set additional settings for CMake you can do that from Preferences – Build, Execution, Deployment:
More information on configuring CMake in CLion you can find in the official documentation of the IDE. It’s really good!
Ready to start developing EOS dApps? Check our tutorials:
1. First Steps in EOS Blockchain Development