I am trying to include a JSON-Parsing library into my smart contract. There are only two .hpp/.cpp files to link which are in the same folder as the contract. JSON and JSONValue

In my contracts header file I use

#include "JSON.h" //in JSON.h the JSONValue.h is included
...

//@abi action
void test(const std::string &pToParse);
...
EOSIO_ABI(Test, (test));

In my contracts .cpp file I call this method:

void VectorTest::test(const std::string& pToParse){
    ... 
    JSON::Parse(pToParse.c_str());
}

When I compile it with the following commands no errors are generated:

eosiocpp -o contracts/test.wast contracts/test.cpp contracts/JSON.cpp contracts/JSONValue.cpp
eosiocpp -g contracts/test.abi contracts/test.cpp

But when I try to push it I get some Runtime error that there is no apply function.

900434ms thread-0   http_plugin.cpp:405           handle_exception     ] FC Exception encountered while processing chain.push_transaction
900434ms thread-0   http_plugin.cpp:406           handle_exception     ] Exception Details: 3070002 wasm_execution_error: Runtime Error Processing WASM
Smart contract's apply function not exported; non-existent; or wrong type
    {}
    thread-0  wasm_eosio_validation.cpp:91 validate

    {"_pending_console_output.str()":""}
    thread-0  apply_context.cpp:62 exec_one

I cross checked the test.wast file and searched for apply but there is no apply function.

Am I doing something wrong or do I try to achieve something which is impossible?


EDIT

Also this command does not work:

eosiocpp -o contracts/test.wast contracts/*.cpp

It always seems like it would just include the JSON-cpp files into the WAST.

  • include your other cpp file to your main cpp file like #include "xxx.cpp" – Jimmy Guo Jul 6 at 2:23
  • Isn't this considered bad coding style in cpp? – TeeAttack42 Jul 6 at 6:11
  • Have defined the apply function yourself or have you used EOS_ABI that creates the apply function? – friedger Jul 6 at 11:35
  • I have used EOS_ABI Macro. Including the cpp files into the contract worked btw - but I still want to know how to use eosiocpp with multiple files – TeeAttack42 Jul 6 at 11:37
  • @TeeAttack42 you can't, see the system contract, it also include the cpp file – Jimmy Guo Jul 6 at 13:22
up vote 2 down vote accepted

This is a workaround based on @KemmyGuo 's comment:

You can include the additional .cpp files (which have to be in the same folder) into your contracts .cpp file.

Disclaimer: this is not a good C++ coding style, but it works for the moment.

In my case inside the test.cpp this did the trick

#include "test.h"
#include "JSON.cpp"
#include "JSONValue.cpp"

void Test::test(const std::string& pToParse){
    ... 
    JSON::Parse(pToParse.c_str());
}

Then you can run as usual

eosiocpp -o contracts/test.wast contracts/test.cpp
eosiocpp -g contracts/test.abi contracts/test.cpp

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.