Getting problem with using struct timeval
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 28

Thread: Getting problem with using struct timeval

  1. #1
    Join Date
    Jun 2011
    Posts
    19

    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.

  2. #2
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,051

    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.
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

  3. #3
    Join Date
    Jun 2011
    Posts
    19

    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 ?

  4. #4
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,480

    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
    Victor Nijegorodov

  5. #5
    Join Date
    Jun 2011
    Posts
    19

    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?

  6. #6
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,480

    Re: Getting problem with using struct timeval

    Quote Originally Posted by __bairagi View Post
    ... 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.
    Victor Nijegorodov

  7. #7
    Join Date
    Jun 2011
    Posts
    19

    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.

  8. #8
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,480

    Re: Getting problem with using struct timeval

    Quote Originally Posted by __bairagi View Post
    ...
    I hope you could help me again.Thanks in advance.
    Sorry, I cannot.
    Just because you didn't show you code...
    Victor Nijegorodov

  9. #9
    Join Date
    Jun 2011
    Posts
    19

    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

  10. #10
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,480

    Re: Getting problem with using struct timeval

    What did you #include?
    Which #if/#else/#endif do you have?
    Victor Nijegorodov

  11. #11
    Join Date
    Jun 2011
    Posts
    19

    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>

  12. #12
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,480

    Re: Getting problem with using struct timeval

    Quote Originally Posted by __bairagi View Post
    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!
    And if you meant that we here in the Forum already know how you defined all these:
    HAVE_CONFIG_H
    HAVE_WINDOWS_H
    HAVE_MMSYSTEM_H
    and what the "config.h" is
    ...
    then you were wrong.
    Victor Nijegorodov

  13. #13
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,527

    Re: Getting problem with using struct timeval

    Quote Originally Posted by __bairagi View Post
    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 */
    };
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...

  14. #14
    Join Date
    Jun 2011
    Posts
    19

    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.

  15. #15
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,051

    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?
    Last edited by ovidiucucu; June 28th, 2011 at 12:53 AM.
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center