my timer works in VS but fails using g++

Dear programmers!

I am having an issue with writing a timer in gnu. My code is:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>

using namespace std;

int main()
{
/* timer */
int t_start = clock();
time_t start = time(0);

int a = 1, b = 2;

int c;

c = a + b;

cout << c << endl;

/* timer */
int t_end = clock();
time_t end = time(0);

/* screen printout */
char str[26];
ctime_s(str, sizeof str, &start);
cout << " " << str;
ctime_s(str, sizeof str, &end);
cout << " " << str;
cout << " Total computing time: " << (t_end - t_start) / CLOCKS_PER_SEC
<< " sec used." << endl << endl;

return 0;
}

It works fine in Visual Studio Community 2015 on Windows, but it fails using gnu on Linux. The error is:

nsi@nsi-ThinkPad-T540p:~/Documents/UNDEX/timer$ g++ -o example example.cpp
example.cpp: In function ‘int main()’:
example.cpp:30:34: error: ‘ctime_s’ was not declared in this scope
ctime_s(str, sizeof str, &start);
^
Any idea how I need to modify so that it works in both Linux gnu and VS? Many thanks!

nsi
Don't use ctime_s(). Use clock().
Better yet, use the standard APIs in <chrono>.

Thanks very much!
Topic archived. No new replies allowed.