EOSIO / eos

An open source smart contract platform
https://developers.eos.io/manuals/eos
MIT License
11.27k stars 3.6k forks source link

eosio-cpp warning suggests to use a non-existing #include #7093

Closed Dexaran closed 5 years ago

Dexaran commented 5 years ago

I was trying to compile a contract with the following includes:

#include <eosio/eosio.hpp>
#include <eosio/asset.hpp>
#include <eosio/contract.hpp>
#include <eosiolib/crypto.h>
#include <string>

The compiler raised a warning with the following text: #warning "<eosiolib/crypto.h> is deprecated use <eosio/crypto.h>. however the text should suggest to use <eosio/crypto.hpp> instead because the include <eosio/crypto.h> do not exist.

include

johndebord commented 5 years ago

@Dexaran Hey, this is more of a eosio-cdt issue, but I'm glad to help either way. So the warning you are getting is actually in good form.

Let me explain: There are two main tools to work with when developing smart contracts using EOSIO's contract development toolkit: eosio-cc and eosio-cpp.

#include <eosio/eosio.hpp>

struct [[eosio::contract]] test : public eosio::contract {
   using contract::contract;
   [[eosio::action]] void act()
   { }
};

Given this minimal smart contract with a no-op action, we can see that it includes one file; #include <eosio/eosio.hpp>. But you might ask, where in the world is that file? On my mac I can see that these includes can be found in the directory: /usr/local/Cellar/eosio.cdt/1.6.1/opt/eosio.cdt/include/.

In here you will find five directories:

total 8
drwxr-xr-x  28 john.debord    staff     896 2019-03-22 19:03 boost
drwxr-xr-x   3 john.debord    staff      96 2019-03-22 19:03 eosio
drwxr-xr-x  41 john.debord    staff    1312 2019-04-09 15:13 eosiolib
drwxr-xr-x  59 john.debord    staff    1888 2019-03-22 19:03 libc
drwxr-xr-x 127 john.debord    staff    4064 2019-03-22 19:03 libcxx

You might be thinking, "Oh, so the files that get included are in directory eosio/". That's actually not the case. If we look at the directory structure, we can see that this directory is where the files for testing and running smart contracts natively reside.

/usr/local/Cellar/eosio.cdt/1.6.1/opt/eosio.cdt/include $ ll
total 8
drwxr-xr-x  28 john.debord    staff     896 2019-03-22 19:03 boost
drwxr-xr-x   3 john.debord    staff      96 2019-03-22 19:03 eosio
drwxr-xr-x  41 john.debord    staff    1312 2019-04-09 15:13 eosiolib
drwxr-xr-x  59 john.debord    staff    1888 2019-03-22 19:03 libc
drwxr-xr-x 127 john.debord    staff    4064 2019-03-22 19:03 libcxx
/usr/local/Cellar/eosio.cdt/1.6.1/opt/eosio.cdt/include $ tree eosio/
eosio/
└── native
    ├── crt.hpp
    ├── intrinsics.hpp
    ├── intrinsics_def.hpp
    ├── native
    │   └── eosio
    │       ├── crt.hpp
    │       ├── intrinsics.hpp
    │       ├── intrinsics_def.hpp
    │       └── tester.hpp
    └── tester.hpp

3 directories, 8 files
/usr/local/Cellar/eosio.cdt/1.6.1/opt/eosio.cdt/include $ 

The directories that we're primarily concerned with are in the directory eosiolib/. If we follow that directory structure we can see that the ones we are particularly interested in are the subdirectories capi/, contracts/, core/, and native/.

/usr/local/Cellar/eosio.cdt/1.6.1/opt/eosio.cdt/include/eosiolib $ ls
action.h              contracts       eosio.hpp        permission.hpp         serialize.hpp  transaction.h
action.hpp            core            fixed_bytes.hpp  print.h                singleton.hpp  transaction.hpp
asset.hpp             crypto.h        ignore.hpp       print.hpp              stdlib.hpp     types.h
binary_extension.hpp  crypto.hpp      multi_index.hpp  privileged.h           symbol.hpp     varint.hpp
capi                  datastream.hpp  name.hpp         privileged.hpp         system.h       
chain.h               db.h            native           producer_schedule.hpp  system.hpp     
contract.hpp          dispatcher.hpp  permission.h     public_key.hpp         time.hpp       
/usr/local/Cellar/eosio.cdt/1.6.1/opt/eosio.cdt/include/eosiolib $ tree
.
├── action.h
├── action.hpp
├── asset.hpp
├── binary_extension.hpp
├── capi
│   └── eosio
│       ├── action.h
│       ├── chain.h
│       ├── crypto.h
│       ├── db.h
│       ├── permission.h
│       ├── print.h
│       ├── privileged.h
│       ├── system.h
│       ├── transaction.h
│       └── types.h
├── chain.h
├── contract.hpp
├── contracts
│   └── eosio
│       ├── action.hpp
│       ├── contract.hpp
│       ├── dispatcher.hpp
│       ├── eosio.hpp
│       ├── multi_index.hpp
│       ├── permission.hpp
│       ├── privileged.hpp
│       ├── producer_schedule.hpp
│       ├── singleton.hpp
│       ├── system.hpp
│       └── transaction.hpp
├── core
│   └── eosio
│       ├── asset.hpp
│       ├── binary_extension.hpp
│       ├── check.hpp
│       ├── crypto.hpp
│       ├── datastream.hpp
│       ├── fixed_bytes.hpp
│       ├── ignore.hpp
│       ├── name.hpp
│       ├── print.hpp
│       ├── rope.hpp
│       ├── serialize.hpp
│       ├── symbol.hpp
│       ├── time.hpp
│       └── varint.hpp
├── crypto.h
├── crypto.hpp
├── datastream.hpp
├── db.h
├── dispatcher.hpp
├── eosio.hpp
├── fixed_bytes.hpp
├── ignore.hpp
├── multi_index.hpp
├── name.hpp
├── native
│   └── eosio
│       ├── crt.hpp
│       ├── intrinsics.hpp
│       ├── intrinsics_def.hpp
│       └── tester.hpp
├── permission.h
├── permission.hpp
├── print.h
├── print.hpp
├── privileged.h
├── privileged.hpp
├── producer_schedule.hpp
├── public_key.hpp
├── serialize.hpp
├── singleton.hpp
├── stdlib.hpp
├── symbol.hpp
├── system.h
├── system.hpp
├── time.hpp
├── transaction.h
├── transaction.hpp
├── types.h
└── varint.hpp

8 directories, 74 files
/usr/local/Cellar/eosio.cdt/1.6.1/opt/eosio.cdt/include/eosiolib $ 

So, when you're developing a smart contract and include the file: #include <eosio/eosio.hpp>, you're actually including the file: /usr/local/Cellar/eosio.cdt/1.6.1/opt/eosio.cdt/include/eosiolib/contracts/eosio/eosio.hpp.

And when you include the file: #include <eosiolib/eosio.hpp>, you're actually including: /usr/local/Cellar/eosio.cdt/1.6.1/opt/eosio.cdt/include/eosiolib/eosio.hpp. Note that this former include has been deprecated and not encouraged; hence the warnings.

Now let's get back to your warning which is found in file: /usr/local/Cellar/eosio.cdt/1.6.1/opt/eosio.cdt/include/eosiolib/crypto.h.

/**
 *  @file
 *  @copyright defined in eos/LICENSE
 */
#pragma once
#include "types.h"

#warning "<eosiolib/crypto.h> is deprecated use <eosio/crypto.h>. If you are using C++ the .h header files will be removed from inclusion entirely in v1.7.0"

// ...

This warning is actually in good form because the file /usr/local/Cellar/eosio.cdt/1.6.1/opt/eosio.cdt/include/eosiolib/crypto.h should never be included in the first place when using C++ and the contract development toolkit; instead, the file /usr/local/Cellar/eosio.cdt/1.6.1/opt/eosio.cdt/include/eosiolib/core/eosio/crypto.hpp should have been included because it provides C++ wrappers over the C functions that would be called otherwise.

This particular warning is meant for someone working on a project in C; where including /usr/local/Cellar/eosio.cdt/1.6.1/opt/eosio.cdt/include/eosiolib/capi/eosio/crypto.h is found perfectly fine when using the eosio-cc compiler. Note that this confusion is only temporary, though.

Hopefully that clears up how the new directory structure works, as well as how the deprecations will take full effect post eosio.cdt v1.7.0.

ekkis commented 5 years ago

@johndebord thank you for the excellent explanation. having just upgraded to 1.6.1 it seems that every reference to eosiolib in an include needs to be replaced with eosio though this is not stated in the release notes or anywhere. how does eosio-cpp know what directories to look for the includes in? (and what directories do I now need in my c_cpp_properties.json in VSCode?

lovesuper commented 4 years ago

You need to use:

#include <eosio/crypto.hpp>