1

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>
thetna
  • 6,903
  • 26
  • 79
  • 113

1 Answers1

5

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

Mikola
  • 9,176
  • 2
  • 34
  • 41
  • I mentioned to forget that **windows.h** is also included in the header file of the that function.I will put the includes and directives in the post. – thetna Jun 27 '11 at 18:11
  • Second dumb question: Are you using CMake? If so, did you remember to run it first? – Mikola Jun 27 '11 at 18:15
  • No i am not running CMake . I am new to this visual studio. I am just creating a console project like **File-> project from existing source-> visual c++-> console and done.** And i am building the source. Could you please let me know if i am doing something stupid and need to learn first? – thetna Jun 27 '11 at 18:21
  • 1
    That macro HAVE_WINDOWS_H is typically part of the CMake build system. Is this a project you downloaded from somewhere? If so, you may want to take a look at this: http://www.cmake.org/cmake/help/runningcmake.html – Mikola Jun 27 '11 at 18:34
  • I have some source code which I want to compile it in microsoft visual studio 2010 only.I think you could help me to know what are the things that i need to keep in mind before compiling it. Could you please tell me what are the changes I have to do so that it will be suitable to run microsoft visual studio? – thetna Jun 27 '11 at 18:40
  • Is this a project you downloaded? If so, does it come with a file called CMakeLists.txt? – Mikola Jun 27 '11 at 18:44
  • Yes this is the project I downloaded. But it does not contain any CMAKELists.txt.Rather it has a makefile for UNIX and the code runs fine in gcc.I want to compile it in MS visual studio. – thetna Jun 27 '11 at 18:48
  • Second dumb question: Does it have an autoconf ./configure script? If so, you could try building it in MSYS first to initialize all the configure scripts, then go back and add the necessary flags to your visual studios project to get it to work. – Mikola Jun 27 '11 at 18:52
  • No , it does not contain any **autoconf,./configure**. There are parser files,header and source files only.Do you think I won't be able to build it in MS visual studio? – thetna Jun 27 '11 at 18:57
  • Probably, but I suppose that to give more information I would have to see the project. Do you have a link to the relevant source packages (ie is this a public open source repository)? That might help in trying to diagnose this. – Mikola Jun 27 '11 at 18:59
  • Since the codes are not public, i am exteremly sorry I can't share the link. I would like to add some more information about the compiling. I have been trying to compile it in different configurations. Once i tried to replace **DWORD by long int** and replaced **t_time and t_suseconds** by **long** in the header file **"time.h"**, it compiles although it shows linking error. So after seeing this I got some kind of hope that I can compile it in MS visual studio and the problem is with header file inclusion and incompatibility in time function.Can you inference something from this information? – thetna Jun 27 '11 at 19:05
  • 1
    The only thing I can infer is that the headers are not getting properly included, probably due to a configure script not having been run properly. What you could try is this: go through that config header and manually expand some of the configure macros. For example, remove the HAVE_WINDOWS_H wrapper, and just directly include the windows.h. This ought to get it to build, but you might get problems later on; and if you have to merge some changes back into the mainline after this modification your team will hate you for it. – Mikola Jun 27 '11 at 19:09
  • @Mikola thanks a lot for your help. I will try the same.May be i will be writing you here again :) – thetna Jun 27 '11 at 19:11
  • @thetna: Ok, good luck! I hope you sort out your problem, whatever it is. If you got the code from someone you work with, you may want to try asking them for help configuring just to make sure you don't mess anything up too badly. – Mikola Jun 27 '11 at 19:13
  • @Mikola,i think your idea is going to help for me. I got rid of those errors i have mentioned in the post. But i am getting others erros like **error C2011: 'timeval' : 'struct' type redefinition**. I knew the reason of error for it also. The reason is it includes both **winsock.h and time.h** and **timeval** is defined in both of these headers. May i need to create another thread or search for it. Incase if you could suggest, please let me know how can i get rid of those errors? – thetna Jun 27 '11 at 19:31
  • Yeah, it might be good to fork to a separate discussion, this one is getting a bit too long. – Mikola Jun 27 '11 at 19:35