Getting problem with timing in C in visual studio 2010 - c

I have a function for getting system time. The function is defined as follows.
int getSystemTime(struct timeval tv, void * tz);{
DWORD milliseconds;
milliseconds = timeGetTime();
tv->tv_sec = milliseconds / 1000;
tv->tv_usec = (milliseconds % 1000) * 1000;
return 0;
}
Precisely following are the problems:
1.error: identifier DWORD is undentified .
2.error: identifier timeGetTime() is undefined.
3.error: identifier suseconds_t is undefined.
I tried to include windef.h where DWORD is defined. But the problem is, I got the error like:
1. error: identifier PCONTEXT is undefined.
The header file for time included is time.h. Here the timeval defined is:
#ifndef _WINSOCK_H
struct timeval {
time_t tv_sec;
suseconds_t tv_usec;
};
Could you please tell me what shall I do to make this function run in windows environment?
[EDIT]
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_WINDOWS_H
#include <windows.h>
#endif
#ifdef HAVE_MMSYSTEM_H
#include <mmsystem.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>

You need to include windows.h at the top of your C file.

Related

Conflicting types error when trying to build - types are the same

Error when trying to compile. Its the basic code and everything looks fine. Tried to rebuild, freshen, update, re-resolve and nothing helps.
Description Resource Path Location Type conflicting types for
‘skirt’ domains.c /domains line 21 C/C++ Problem
#include <time.h>
#include <stdio.h>
#include <math.h>
#include <stdint.h>
typedef long long int64;
int main()
{
struct timespec iter_start, iter_end;
clock_gettime(CLOCK_MONOTONIC, &iter_start); // iteracijai pirmai
printf("hello");
clock_gettime(CLOCK_MONOTONIC, &iter_end);
int64 duration = skirt(iter_end, iter_start);
return(0);
}
int64 skirt(const struct timespec after, const struct timespec before)
{
return ((int64)after.tv_sec - (int64)before.tv_sec) * (int64)1000000
+ ((int64)after.tv_nsec - (int64)before.tv_nsec) / 1000;
}
You have to declare the function before using it. Worked like a charm.

error: expected declaration specifiers or '…' before string constant

Does anybody know what is wrong with this piece of code? i can't see to find the issue among the comparable questions.
The code is written in C, and i keep getting this error. I do add -D SET_MIN_TEMP=5 -D Set_MAX_TEMP=30 to the gcc compile line to make sure the ifndefs should be false...
#ifndef CONFIG_H
#define CONFIG_H
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdint.h>
#ifndef RUN_AVG_LENGTH
#define RUN_AVG_LENGTH 5
#endif
#ifndef SET_MIN_TEMP
printf("please set SET_MIN_TEMP \n");
#endif
#ifndef SET_MAX_TEMP
printf("please set SET_MAX_TEMP \n");
#endif
typedef uint16_t sensor_id_t;
typedef uint16_t room_id_t;
typedef double sensor_value_t;
typedef time_t sensor_ts_t; // UTC timestamp as returned by time() - notice that the size of time_t is different on 32/64 bit machine
typedef struct {
sensor_id_t id;
sensor_value_t value;
sensor_ts_t ts;
} sensor_data_t;
typedef struct {
sensor_id_t sensor_id;
room_id_t room_id;
double running_avg[5];
sensor_ts_t timestamp;
} sensor_node_t;
#endif // CONFIG_H
You can not use a function call (printf) outside a function. You should take a look at #error if you want to report errors at compilation...
See here

Discover available clock types at compile time

[Ubuntu 14.04, 3.16.0-34-generic Kernel, GCC 4.8.4, Clang 3.5.0]
I'm writing some elapsed time performance routines for an application I have, and I would like to do it in a cross-platform manner.
I would like to write it in such a way that the right clock type is selected during compile, rather than run-time (which I can do by testing for failures and using fallbacks).
The clock_getres(2) man page states:
On POSIX systems on which these functions are available, the symbol _POSIX_TIMERS is defined in <unistd.h> to a value greater than 0. The symbols _POSIX_MONOTONIC_CLOCK, _POSIX_CPUTIME, _POSIX_THREAD_CPUTIME
indicate that CLOCK_MONOTONIC, CLOCK_PROCESS_CPUTIME_ID, CLOCK_THREAD_CPUTIME_ID are available. (See also sysconf(3).)
I have included in my code , , and but my conditional compile statements don't recognise the symbol _POSIX_MONOTONIC_CLOCK. It always prints the 'gettimeofday' message. I've tried GCC and Clang but get the same result.
My code below is incomplete (and incorrect) and I would appreciate some assistance on how to do this right.
#include <unistd.h>
#include <features.h>
#include <stdio.h> // printf (otherwise forward declaration warning)
#include <stdlib.h> // NULL
#include <sys/time.h>
long long get_utime(void)
{
struct timeval tv;
if (gettimeofday(&tv, NULL) == -1) return -1LL;
long long t = (long long) tv.tv_usec +
(long long) tv.tv_sec * 1000000LL;
return t;
}
int main() {
union time{
struct timespec tSpec;
long long tLL;
} myT;
#ifdef CLOCK_MONOTONIC_RAW
clock_gettime(CLOCK_MONOTONIC_RAW, &myT.tSpec);
printf("CLOCK_MONOTONIC_RAW\n");
#elif _POSIX_MONOTONIC_CLOCK
clock_gettime(CLOCK_MONOTONIC, &myT.tSpec);
printf("CLOCK_MONOTONIC\n");
#else
myT.tLL = get_utime();
printf("gettimeofday\n");
#endif // CLOCK_MONOTONIC_RAW
return 0;
}
I'm not using any configure or auto-configure software.
Also, a comment about the relative speeds of CLOCK_MONOTONIC & CLOCK_MONOTONIC_RAW would be nice. I understand the difference and their limitations.
You aren’t declaring the headers and feature-test macros that the man page says you need. This works:
#define _POSIX_C_SOURCE 200809L // <- This was missing.
#define _XOPEN_SOURCE 700 // <- This is optional.
#include <unistd.h>
#include <stdio.h> // printf (otherwise forward declaration warning)
#include <stdlib.h> // NULL
#include <sys/time.h>
#include <time.h> // <- This was missing.
long long get_utime(void)
{
struct timeval tv;
if (gettimeofday(&tv, NULL) == -1) return -1LL;
long long t = (long long) tv.tv_usec +
(long long) tv.tv_sec * 1000000LL;
return t;
}
int main() {
union time{
struct timespec tSpec;
long long tLL;
} myT;
#ifdef CLOCK_MONOTONIC_RAW
clock_gettime(CLOCK_MONOTONIC_RAW, &myT.tSpec);
printf("CLOCK_MONOTONIC_RAW\n");
#elif _POSIX_MONOTONIC_CLOCK
clock_gettime(CLOCK_MONOTONIC, &myT.tSpec);
printf("CLOCK_MONOTONIC\n");
#else
myT.tLL = get_utime();
printf("gettimeofday\n");
#endif // CLOCK_MONOTONIC_RAW
return 0;
}

How to split this into header and source files?

I have some C code I'd like to split into a header file and a source file:
#ifndef BENCHMARK_H
#define BENCHMARK_H
#ifdef WIN32
#include <windows.h>
double get_time()
{
LARGE_INTEGER t, f;
QueryPerformanceCounter(&t);
QueryPerformanceFrequency(&f);
return (double)t.QuadPart/(double)f.QuadPart;
}
#else
#include <sys/time.h>
#include <sys/resource.h>
double get_time()
{
struct timeval t;
struct timezone tzp;
gettimeofday(&t, &tzp);
return t.tv_sec + t.tv_usec*1e-6;
}
#endif
#endif
What would be the proper format of the resulting benchmark.h and benchmark.c?
I know the header file should contain function declarations, while the source file should be where the actual function definitions reside. Would this following code be correct? Namely, should the #ifdef WIN32 directive be in both files as I have it below? Or should it all be in the .c file?
benchmark.h
#ifndef BENCHMARK_H
#define BENCHMARK_H
#ifdef WIN32
#include <windows.h>
#else
#include <sys/time.h>
#include <sys/resource.h>
#endif
double get_time();
#endif
benchmark.c
#ifdef WIN32
double get_time()
{
LARGE_INTEGER t, f;
QueryPerformanceCounter(&t);
QueryPerformanceFrequency(&f);
return (double)t.QuadPart/(double)f.QuadPart;
}
#else
double get_time()
{
struct timeval t;
struct timezone tzp;
gettimeofday(&t, &tzp);
return t.tv_sec + t.tv_usec*1e-6;
}
#endif
Together, a header file and a c file form a "code module" (or if you will: an ADT, a class etc).
The header file is always to be regarded as the user interface of your code, where the "user" is the programmer who is going to use your module. It shall never contain any code or variable definitions, period.
While the c file contains the actual implementation, which is of no interest to the user, and should not be of any concern to them. The c file should use private encapsulation and everything that the user need not know should be in that file.
The above is how you design C programs, or any program in any language. This is not subjective, it is not opinion-based, it is the only way. If you are doing your program design differently, you are doing it wrong.
As for your specific program, it should be designed in the following way:
benchmark.h
#ifndef BENCHMARK_H
#define BENCHMARK_H
double get_time (void);
/* documentation about how this function is used should be put here */
#endif
benchmark.c
#include "benchmark.h"
/*** Include files ***/
#ifdef WIN32
#include <windows.h>
#else
#include <sys/time.h>
#include <sys/resource.h>
#endif
/*** Other stuff, for example constants, typedefs, static file scope variables ***/
/*** function definitions ***/
#ifdef WIN32
double get_time (void)
{
LARGE_INTEGER t, f;
QueryPerformanceCounter(&t);
QueryPerformanceFrequency(&f);
return (double)t.QuadPart/(double)f.QuadPart;
}
#else
double get_time (void)
{
struct timeval t;
struct timezone tzp;
gettimeofday(&t, &tzp);
return t.tv_sec + t.tv_usec*1e-6;
}
#endif
Note that double get_time() means "function that accepts any parameter" in C. That is poor style, use void instead. C and C++ are different in this regard. In C++, func() and func(void) mean the same thing.
I would simplify it to this, the only thing needed in the header file is the function prototype.
benchmark.h
double get_time();
benchmark.c
#ifdef WIN32
#include <windows.h>
#include "benchmark.h"
double get_time()
{
LARGE_INTEGER t, f;
QueryPerformanceCounter(&t);
QueryPerformanceFrequency(&f);
return (double)t.QuadPart/(double)f.QuadPart;
}
#else
#include <sys/time.h>
#include <sys/resource.h>
#include "benchmark.h"
double get_time()
{
struct timeval t;
struct timezone tzp;
gettimeofday(&t, &tzp);
return t.tv_sec + t.tv_usec*1e-6;
}
#endif

resolving redefinition of timespec in time.h

I am writing a program which includes both /usr/include/linux/time.h and /usr/include/stdlib.h.
The problem is:
stdlib.h includes /usr/include/time.h, which defines 'struct timespec', and /usr/include/linux/time.h also defines one. This introduces a compilation error of redefinition.
I've examined the definitions of 'struct timespec' in these two header files:
in /usr/include/time.h:
struct timespec
{
__time_t tv_sec; /* Seconds. */
long int tv_nsec; /* Nanoseconds. */
};
in /usr/include/linux/time.h:
struct timespec {
__kernel_time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};
It seems that these definitions are indeed equivalent, but I can't prove it.
My question is: is there a robust way to resolve this redefinition?
Links to discussions on this problem are also highly appreciated. Thanks.
One way to resolve the double-definition error is to rename one of these definitions:
#include <time.h>
#define timespec linux_timespec
#include <linux/time.h>
#undef timespec
And then assert at compile time that both definitions have the same layout:
typedef int assert_same_size[sizeof(struct linux_timespec) == sizeof(timespec) ? 1 : -1];
typedef int assert_same_alignment[__alignof(struct linux_timespec) == __alignof(timespec) ? 1 : -1];
typedef int assert_same_tv_sec[offsetof(struct linux_timespec, tv_sec) == offsetof(struct timespec, tv_sec) ? 1 : -1];
typedef int assert_same_tv_nsec[offsetof(struct linux_timespec, tv_nsec) == offsetof(struct timespec, tv_nsec) ? 1 : -1];
I got the same error in Ecliepse Neon IDE and i resolved it by adding -DHAVE_STRUCT_TIMESPEC in C/C++ Build -> Settings -> GCC C++ Compiler -> Miscellaneous -> Others flag

Resources