Getting problem with using struct timeval
Hi all,
I am us ing MS visual studio 2010 for compiling the C source code. While compiling I am getting the problem with header file time.h.The problem is , I get the error
"undefined reference to the suseconds_t" and some other syntactical error on the structure.
But the the structure timeval is defined as follows.
#ifndef _WINSOCK_H
struct timeval {
time_t tv_sec;
suseconds_t tv_usec;
};
I am passing this structure in the function named getTimeofDay(struct timeval *tv, struct timezone *tz) to get the elapse time. This function is defined
int getTimeof Day(struct timeval *tv, struct timezone *tz){
DWORD milliseconds;
milliseconds = timeGetTime();
tv->tv_sec =milliseconds/100;
tv->tv_usec =(milliseconds%1000)*1000;
return 0;
}
Once i change the DWORD with int long and time_t and suseconds_t with long , it compiles. Could you please let me know how can I get rid of this problem.
My operating system is Windows XP.
Re: Getting problem with using struct timeval
In Windows SDK, timeval is defined as
Code:
struct timeval {
long tv_sec;
long tv_usec;
};
See MSDN documentation: timeval Structure.
Re: Getting problem with using struct timeval
Do you think i need to enable SDK environment and need to include winsock2.h instead of time.h? Do you have any idea ?
Re: Getting problem with using struct timeval
There is no such a type suseconds_t in Windows. So you must either change it (or the whole structure) to something that exists in Windows, or #define (or typedef) it.
PS: you are not the first who has got this problem; see http://connect.microsoft.com/VisualS...six-conformant
Re: Getting problem with using struct timeval
The problem is all about timing behviour.My source file includes time.h, which defines the structure timeval. The definition of timeval in the time.h is:
struct timeval {
time_t tv_sec;
suseconds_t tv_usec;
};
Do I need to change the include file?
I would like to be more precise in my problem. Once I load my source file into the visual studio 2010, I get the following error messages:
1.error: identifier DWORD is undentified .
2. error: identifier timeGetTime() is undefined.
3: error: identifier suseconds_t is undefined.
I am getting trouble with this for some days. Please let me know, how to overcome this problem?
Re: Getting problem with using struct timeval
Quote:
Originally Posted by
__bairagi
... Once I load my source file into the visual studio 2010, I get the following error messages:
1.error: identifier DWORD is undentified .
2. error: identifier timeGetTime() is undefined.
3: error: identifier suseconds_t is undefined.
I am getting trouble with this for some days. Please let me know, how to overcome this problem?
1. http://www.codeguru.com/forum/showthread.php?t=219341
3. See my previous post.
Re: Getting problem with using struct timeval
Hi VictorN
Thanks for the reply. Yes you are right, I got rid of those error but i am encountering other errors like
rror C2011: 'timeval' : 'struct' type redefinition
I hope you could help me again.Thanks in advance.
Re: Getting problem with using struct timeval
Quote:
Originally Posted by
__bairagi
...
I hope you could help me again.Thanks in advance.
Sorry, I cannot.
Just because you didn't show you code...
Re: Getting problem with using struct timeval
Hi,
The surprising thing is that I am getting error in header file.so,the snippet of code where the error has occured
struct timeval {
time_t tv_sec;
suseconds_t tv_usec;
};
in the header file time.h and the exact error is:
1>C:\Program Files\Microsoft Visual Studio 10.0\VC\include\sys/time.h(16): error C2011: 'timeval' : 'struct' type redefinition
Re: Getting problem with using struct timeval
What did you #include?
Which #if/#else/#endif do you have?
Re: Getting problem with using struct timeval
Here are my includes.
#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>
Re: Getting problem with using struct timeval
Quote:
Originally Posted by
__bairagi
Here are my includes.
#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>
Great! :thumb: :D
And if you meant that we here in the Forum already know how you defined all these:
Quote:
HAVE_CONFIG_H
HAVE_WINDOWS_H
HAVE_MMSYSTEM_H
and what the "config.h" is
...
then you were wrong. :cool:
Re: Getting problem with using struct timeval
Quote:
Originally Posted by
__bairagi
The surprising thing is that I am getting error in header file.so,the snippet of code where the error has occured
struct timeval {
time_t tv_sec;
suseconds_t tv_usec;
};
in the header file time.h and the exact error is:
1>C:\Program Files\Microsoft Visual Studio 10.0\VC\include\sys/time.h(16): error C2011: 'timeval' : 'struct' type redefinition
Hmm.. The strange thing is that in my VS2010 installation time.h file is in
\Microsoft Visual Studio 10.0\VC\include
and NOT in
\Microsoft Visual Studio 10.0\VC\include\sys
Also, it doesn't have your definition of timeval; it has:
Code:
/*
* Structure used in select() call, taken from the BSD file sys/time.h.
*/
struct timeval {
long tv_sec; /* seconds */
long tv_usec; /* and microseconds */
};
Re: Getting problem with using struct timeval
Hello VladimirF,
Thanks for your reply. Actually I am newbie in MS visual studio. I have source files which i want to compile in MSVS. I may be missing some basic stuffs, so please let me know what are the stupids I am doing. I have been trying to solve it since few days, but not able to do.
Re: Getting problem with using struct timeval
It looks like some "cross-platform" source code wrtitten by a *nix-guy.
suseconds_t is a *NIX (linux) specific type.
You have to add the right preprocessor definitions in the project properties in order to choose the right headers for Windows.
For example, probably HAVE_WINDOWS_H has to be defined.
May be others but without seeing all the project code / properties, none can even guess.
Besides.
Are you using VS Express Edition? If yes, have you installed the right SDK?