user1705183
user1705183

Reputation: 33

How do you include clock_t on an Arduino?

I am trying to use a timer library that I created my self, it uses clock_t in it. When I compile the sketch i keep getting errors. This is the error I keep getting error: 'clock_t' in namespace 'std' does not name a type.

Is it even possible to use clock_t. The library I wrote worked perfectly on a c++ compiler, but not here.

I am new to this Arduino stuff, any help will be nice. Thanks in advance.

Upvotes: 3

Views: 1068

Answers (2)

Hailei
Hailei

Reputation: 42153

Arduino links against AVR Libc, and most of the things that look similar to C standard library come from it.

As far as I know, there is no clock_t in AVR Libc. I searched among the source files of the library, and looked around the documentations, but didn't find it. There is no time.h in AVR Libc, either.

And according to Arduino FAQ:

...the Arduino language is merely a set of C/C++ functions that can be called from your code. Your sketch undergoes minor changes (e.g. automatic generation of function prototypes) and then is passed directly to a C/C++ compiler (avr-g++). All standard C and C++ constructs supported by avr-g++ should work in Arduino. ...

And in AVR-libc FAQ:

Can I use C++ on the AVR?

However, there's currently no support for libstdc++, the standard support library needed for a complete C++ implementation. This imposes a number of restrictions on the C++ programs that can be compiled. Among them are:

  • Obviously, none of the C++ related standard functions, classes, and template classes are available.

So, Arduino doesn't provide C++ standard library for you - so ctime (C++) is not available as well as time.h (C). That's to say, most likely you cannot use clock_t in Arduino environment.

Upvotes: 2

bames53
bames53

Reputation: 88215

I'm not familiar with Arduino, but in C++ you have to #include <ctime>. It's possible that in the C++ implementation where clock_t worked for you ctime was included indirectly via another header whereas Arduino's implementation of that other header did not include ctime.

This is an issue with C++; the headers included in other standard headers are not defined by the standard, so different implementation may produce different results when you fail to directly include the correct headers simply because each implementation uses different indirect includes. To avoid this you should be careful to always directly include any standard header that you use anything from. This means you need to be knowledgeable about which standard headers provide which standard facilities. http://en.cppreference.com/w/ can help you.

Upvotes: 3

Related Questions