0

Here's the program:

timer.cpp:

#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
#include <time.h>

int main( int argc, char** argv) {

    #define BILLION  1E9
    struct timespec requestStart, requestEnd;

    // Calculate time taken by a request                                                                                                                                        
    clock_gettime(CLOCK_REALTIME, &requestStart);

    //function_call();      // time this                                                                                                                                                    

    clock_gettime(CLOCK_REALTIME, &requestEnd);

    // Calculate time it took                                                                                                                                                   
    double accum = ( requestEnd.tv_sec - requestStart.tv_sec )
        + (( requestEnd.tv_nsec - requestStart.tv_nsec ) / BILLION);
    cout << accum << endl;   
}

On Mac OSX 10.8.5 I have access to two compilers: Apple LLVM version 4.2 (clang-425.0.28), invoked as c++, and gcc version 4.2.1 (wow, that's old), invoked as g++.

c++ timer.cpp:

error: use of undeclared identifier 'CLOCK_REALTIME'
clock_gettime(CLOCK_REALTIME, &requestStart);

g++ timer.cpp:

timer.cpp: In function ‘int main(int, char**)’:
timer.cpp:15: error: ‘CLOCK_REALTIME’ was not declared in this scope
timer.cpp:15: error: ‘clock_gettime’ was not declared in this scope

Am I omitting anything, or am I simply working with archaic compilers?

UPDATE I've verified that this solution works with the stock c++ and g++ compilers and libraries on OSX 10.8.5.

Community
  • 1
  • 1
Chap
  • 3,649
  • 2
  • 46
  • 84
  • 2
    It is not a matter of compilers; but of libraries, includes, and perhaps non-posix compliance of your MacOSX. BTW, you probably could compile [GCC](http://gcc.gnu.org/) 4.8 or [CLANG](http://clang.llvm.org/) 3.3 on your machine (from the source code). – Basile Starynkevitch Sep 14 '13 at 19:41
  • 1
    possible duplicate of [clock\_gettime alternative in Mac OS X](http://stackoverflow.com/questions/5167269/clock-gettime-alternative-in-mac-os-x). Perhaps you might consider installing Linux on your hardware. – Basile Starynkevitch Sep 14 '13 at 19:44
  • I thought OSX claimed bragging rights for being 100% POSIX compliant. Be that as it may, I'm going to look into installing GCC 4.8. And I agree, I can probably get everything else I need from the the original thread you linked to. – Chap Sep 14 '13 at 20:08
  • Installing a newer GCC is useful, but won't solve the lack of `clock_gettime`... – Basile Starynkevitch Sep 14 '13 at 20:46
  • @Basile: ...as I just discovered compiling on Linux Mint, running under VirtualBox on OSX. So is Linux-on-VirtualBox-on-OSX similarly hobbled? – Chap Sep 14 '13 at 20:58

0 Answers0