Richard Knop
Richard Knop

Reputation: 83755

clock_t does not work on Linux?

This is what I have been using in WIndows:

#include <ctime>
#include <iostream>

int main( void )
{
    using namespace std;

    clock_t lastT;
    lastT = clock();

    cin.get();
    cin.get();
    return 0;
}

In Linux I get the error:

'clock_t' was not declared in this scope

Is there some other data type for this in Linux?

I am compiling it in Anjuta IDE by clicking Run.

Upvotes: 2

Views: 5138

Answers (3)

Richard Knop
Richard Knop

Reputation: 83755

Ok. I have solved it by using:

int lastT;

instead. The rest works the same way.

Upvotes: 1

stefanB
stefanB

Reputation: 79930

I copy/paste/compile and I don't get any issue on linux.

> uname -a
Linux xxxhappy 2.6.16.46-0.12-bigsmp #1 SMP Thu May 17 14:00:09 UTC 2007 i686
i686 i386 GNU/Linux

See man 3 clock for more info.

If your file is named main.cpp you can compile it from command line:

g++ -o main.o -c -g -Wall main.cpp
g++ -o app main.o

or in one step:

g++ -o app main.cpp

Your executable will be named app, you can name it whatever you want.

Upvotes: 2

Adam Rosenfield
Adam Rosenfield

Reputation: 400672

Your IDE/compiler is not compliant. The C++ standard requires the <ctime> header to be identical to the C99 header <time.h>, except that symbols are placed in the std namespace (C++03, §17.4.1.2/4). C99 §7.23.1/3 requires <time.h> to declare clock_t to be an arithmetic type capable of representing time.

So, if your implementation does not declare clock_t, it is not compliant with the C++ standard.

Upvotes: 4

Related Questions