4

I am using time functions within my smart contracts and I'm seeing differences between eosiocpp and g++. The below compiles and executes with no problem using g++.

  time_t rawtime;
  tm * timeinfo;
  time(&rawtime);

  timeinfo = localtime (&rawtime);
  printf ("Current local time and date: %s", asctime(timeinfo));

However, when using eosiocpp, I get the following compilation errors. My includes are the same.

eosride.cpp:150:9: error: redefinition of 'rawtime' with a different type: 'time (&)' (aka 'unsigned long &') vs 'time_t' (aka 'long')
  time(&rawtime);
        ^
eosride.cpp:148:10: note: previous definition is here
  time_t rawtime;
         ^
eosride.cpp:150:9: error: declaration of reference variable 'rawtime' requires an initializer
  time(&rawtime);
        ^~~~~~~
eosride.cpp:152:14: error: use of undeclared identifier 'localtime'
  timeinfo = localtime (&rawtime);

What are the differences between eosiocpp and g++? Why would some basic code compile with g++ but not with eosiocpp?

5

eosiocpp compiles and links against the versions of the std-c and std-c++ libraries that are customized for the EOSIO platform. Some parts of those standard libraries that do not make sense for smart contracts (like sockets and filesystem access) are not present.

In particular time and localtime have been removed because deterministic execution require EOSIO to break the semantics of these standards. Your best recourse is to attempt to use our intrinsics for time access:

https://github.com/EOSIO/eos/blob/21842461970c71bfdfa95f896fe9a23ec0f86b30/contracts/eosiolib/system.h#L58-L72

These will give you a view of "time" that is deterministic within the context of blockchain execution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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