-
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?
-
Re: Getting problem with using struct timeval
Hi,
Probably you are right and you are right in the sense these code are cross platform code.
I have been using visual studio 2010 professional. So far I know , i think the SDK installed is right one. I just removed those macro directives and included the header files like :
#include <WinSock2.h>
#include <Windows.h>
#include <stdio.h>
Now, what I am getting error is :
1>C:\Program Files\Microsoft SDKs\Windows\v7.0A\include\winnt.h(4277): error C2040: 'CONTEXT' : '_CONTEXT' differs in levels of indirection from 'binding *'
1>C:\Program Files\Microsoft SDKs\Windows\v7.0A\include\winuser.h(5341): error C2365: 'INPUT' : redefinition; previous definition was 'enumerator'
1>c:\documents and settings\xyz\desktop\abc\test.h(72) : see declaration of 'INPUT'
1>C:\Program Files\Microsoft Visual Studio 10.0\VC\include\sys/time.h(16): error C2011: 'timeval' : 'struct' type redefinition
1>C:\Program Files\Microsoft SDKs\Windows\v7.0A\include\WinSock2.h(176) : see declaration of 'timeval'
Could you please know what Shall i do to overcome those error?
-
Re: Getting problem with using struct timeval
Quote:
Originally Posted by
__bairagi
... So far I know , i think the SDK installed is right one. I just removed those macro directives and included the header files like :
Code:
#include <WinSock2.h>
#include <Windows.h>
#include <stdio.h>
Now, what I am getting error is :
Code:
...
1>C:\Program Files\Microsoft Visual Studio 10.0\VC\include\sys/time.h(16): error C2011: 'timeval' : 'struct' type redefinition
1>C:\Program Files\Microsoft SDKs\Windows\v7.0A\include\WinSock2.h(176) : see declaration of 'timeval'
Why are you using two definitions of timeval: one in the <WinSock2.h> and another in C:\Program Files\Microsoft Visual Studio 10.0\VC\include\sys/time.h ? :confused:
-
Re: Getting problem with using struct timeval
I am not so sure about it. I have not included <sys/time.h> in the header file and I have no idea why it is showing the inclusion of <sys/time.h>. Could you please let me know what exactly I have to do get rid of this?
-
Re: Getting problem with using struct timeval
First try to comment out (if you, of course, don't use sockets!) the
Code:
#include <WinSock2.h>
-
Re: Getting problem with using struct timeval
Hi,
once I remove winsock2.h I get the following errors:
>C:\Program Files\Microsoft Visual Studio 10.0\VC\include\sys/time.h(20): error C2061: syntax error : identifier 'suseconds_t'
1>C:\Program Files\Microsoft Visual Studio 10.0\VC\include\sys/time.h(21): error C2059: syntax error : '}'
1>C:\Program Files\Microsoft Visual Studio 10.0\VC\include\sys/time.h(39): error C2079: 'it_interval' uses undefined struct 'timeval'
1>C:\Program Files\Microsoft Visual Studio 10.0\VC\include\sys/time.h(40): error C2079: 'it_value' uses undefined struct 'timeval'
-
Re: Getting problem with using struct timeval
Quote:
Originally Posted by
__bairagi
once I remove winsock2.h I get the following errors:
Code:
>C:\Program Files\Microsoft Visual Studio 10.0\VC\include\sys/time.h(20): error C2061: syntax error : identifier 'suseconds_t'
1>C:\Program Files\Microsoft Visual Studio 10.0\VC\include\sys/time.h(21): error C2059: syntax error : '}'
1>C:\Program Files\Microsoft Visual Studio 10.0\VC\include\sys/time.h(39): error C2079: 'it_interval' uses undefined struct 'timeval'
1>C:\Program Files\Microsoft Visual Studio 10.0\VC\include\sys/time.h(40): error C2079: 'it_value' uses undefined struct 'timeval
'
Where does this file C:\Program Files\Microsoft Visual Studio 10.0\VC\include\sys/time.h come from? :confused:
The "standard" VC2010 installation does not contain such a file! (and VladimirF already mentioned it).
There is C:\Program Files\Microsoft Visual Studio 10.0\VC\include\time.h
and there is C:\Program Files\Microsoft Visual Studio 10.0\VC\include\sys/timeb.h...
Could you show *your* version of time.h file?
-
Re: Getting problem with using struct timeval
Hi,
Thanks for your reply.I think my VS contains some header files of cygwin. Following is the version it is showing. Do you think it is error in installation of visual studio? or I did some mistakes during creating the project.
/* time.h -- An implementation of the standard Unix <sys/time.h> file.
Written by Geoffrey Noer <[email protected]>
Public domain; no rights reserved. */
Actually I did following to create project.
File->project from external source-> vc++ -> use visual studio for console->ok
-
Re: Getting problem with using struct timeval
Quote:
Originally Posted by
__bairagi
Hi,
Thanks for your reply.I think my VS contains some header files of cygwin. Following is the version it is showing. Do you think it is error in installation of visual studio? or I did some mistakes during creating the project.
I think (and am pretty sure) that you did some mistakes during creating the project.
Quote:
Originally Posted by
__bairagi
/* time.h -- An implementation of the standard Unix <sys/time.h> file.
Written by Geoffrey Noer <
[email protected]>
Public domain; no rights reserved. */
Well, what does this Unix file have to do with the Windows CRT? :confused:
Quote:
Originally Posted by
__bairagi
Actually I did following to create project.
File->project from external source-> vc++ -> use visual studio for console->ok
I don't see any problem here.
-
Re: Getting problem with using struct timeval
Hello VictorN,
Thanks a lot for your help.It really worked for my after removing the the header file <sys/time.h>. But still I am bugging with the error which is the last one amongst many errors. Following is the error and of course I am expecting another help from you.
1>C:\Program Files\Microsoft SDKs\Windows\v7.0A\include\winnt.h(4277): error C2040: 'CONTEXT' : '_CONTEXT' differs in levels of indirection from 'binding *'
1>C:\Program Files\Microsoft SDKs\Windows\v7.0A\include\winuser.h(5341): error C2365: 'INPUT' : redefinition; previous definition was 'enumerator'
-
Re: Getting problem with using struct timeval
-
Re: Getting problem with using struct timeval
I found the solution for these kind of error messages.
1. For the error C2040 in my case, CONTEXT in my source code was defined as structure . Same Keyword in the
header file winnt.h it is defined as struct pointer. So this conflict was creating error.
2.Similar was the case for for error c2365.
Thanks everybody for your help.
-
Re: Getting problem with using struct timeval
Quote:
Originally Posted by
__bairagi
I found the solution for these kind of error messages.
1. For the error C2040 in my case, CONTEXT in my source code was defined as structure . Same Keyword in the
header file winnt.h it is defined as struct pointer. So this conflict was creating error.
2.Similar was the case for for error c2365.
Thanks everybody for your help.
It isn't wise to create constants and names that could be easily duplicated by another header. Words such as CONTEXT, FILE, TEXT, SECURITY, etc. should not be used for your own definitions.
Instead YOURAPP_CONTEXT or something that is almost impossible for it to be duplicated, and at the same time, makes sense.
Regards,
Paul McKenzie
-
Re: Getting problem with using struct timeval
To overcome those error, I just renamed those Keywords into another name appending some number like CONTEXT1, which gives meaning to my source code and does not conflict with header files as well.