• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2616
  • Last Modified:

porting linux c++ code to windows -> need windows equiv for sleep() and gettimeofday()

Hi there

I am having a hell of a time porting my linux code to windows..

I need a windows function that will get wall-time in ~milliseconds, like gettimeofday() which is found in <sys/time.h> s

and I need a sleep() function in windows which will sleep for one second or so, (in linux I used unistd.h which contains the sleep() function ).


These are just some of my problems :S

if someone could help that would be really great!!!

Thank you in advance!!!!!!!!!!
0
lost_bits1110
Asked:
lost_bits1110
  • 7
  • 6
  • 2
  • +1
2 Solutions
 
AxterCommented:
>>and I need a sleep() function in windows

Use Sleep() API function.  It has a capital first letter, as do most windows API functions.
0
 
AxterCommented:
>>like gettimeofday()

You can use GetSystemTime()
0
 
lost_bits1110Author Commented:
is GetSystemTime under windows.h??

Another problem I have is that in my includes I have <iostream.h> and <fstream.h> and <string>, but ostringstream is not recognizable anymore on the windows side, and is spitting out dozens of errors

(I've increase the points another 20)

Thank you
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
chip3dCommented:
use <iostream> and  <fstream> without ".h"
for stringstreams you need to include <sstream>
0
 
chip3dCommented:
don't forget to use the right namespace... (std::)
0
 
lost_bits1110Author Commented:
oh man
okay the problem is that *I can't* use <iostream> and <fstream> without the .h's because of another library im using with the code...

if i include the .h's then i get 102 compile errors!

so its no option becuase of the library i'm using

(god i hate porting!!)
0
 
AxterCommented:
>>okay the problem is that *I can't* use <iostream> and <fstream> without the .h's because of another library im
>>using with the code...

The *.h version is not part of the C++ standard, and therefore, it's not portable.
I recommend you avoid using any library that uses non-standard libraries.

If you really have to use this library, you could create you're own version of the headers, and have it include the standard headers.

0
 
lost_bits1110Author Commented:
are you serious!! omg this is terrible.. :'(
0
 
AxterCommented:
>>are you serious!! omg this is terrible.. :'(

Yes.
Older compilers like VC++ 6.0 still support these non-standard headers, so you could try using an older compiler.
Or you could try using the GNU 3.x compiler, which also still support sthe non-standard header.
0
 
lost_bits1110Author Commented:
well i am using VC++ 6.0, but it still doesnt work...

errors after errors after errors....

part of the problem is i'm also using a library (VTK) which has a bunch of its own redefinitions like using std::blabla in one of its classes

even if i comment out all my #includes involving input/output, i still get errors that say 'redefinition of blabla' and problems with the strstream, and the errors all point to that VTK class, or they point to my Microsft Visual Studio /INCLUDE folder

ITs insane
there has to be a way but at this point  ive tried so much and it seems hopeless :(
0
 
AxterCommented:
>>a bunch of its own redefinitions like using std::blabla in one of its classes

You should avoid mixing non-standard headers with standard headers.
When you do have to mix non-standard headers with standard headers, never use using namespace std.
When yo use using namespace std, you're putting the standard header library in the same namespace as the non-standard header library, which then causes the compiler to give you a redefinition error.
0
 
lost_bits1110Author Commented:
Thanks for your responses,

btw I found that using <strstrea.h> instead of <strstream.h> works,

and then i just use istrstream/ostrstream (iinstead of istringstream/ostringstream which is in <sstream>)
0
 
lost_bits1110Author Commented:
so is it okay though thtat I use

std::string

even though everything else is nonstandard - i.e. all my includes are wih the .h's, and nowhere do I have 'using namespace std' or anything like htat. I even have <string.h>, only when I declare an object, i have to put 'std::string variablename' ( its the only place where I have std:: in my code)

0
 
BrianGEFF719Commented:
>>is GetSystemTime under windows.h??

Yes , so is the Sleep() Api, also the Sleep api takes milliseconds, not seconds.

So Sleep(1000); // sleep for 1 second.


Brian.
0
 
AxterCommented:
>>and nowhere do I have 'using namespace std' or anything like htat.

How about the third party libraries you're using.  Do any of them have using namespace in their headers?
0
 
lost_bits1110Author Commented:
Hi,

actually yes, they have a library where it says:

using std::dec;
00045 using std::hex;
00046 using std::setw;
00047 using std::setfill;
00048 using std::setprecision;
00049 using std::cerr;
00050 using std::cout;
00051 using std::cin;
00052 using std::ios;
00053 using std::endl;
00054 using std::ends;
00055 using std::ostream;
00056 using std::istream;
00057 using std::ostrstream;
00058 using std::istrstream;
00059 using std::strstream;
00060 using std::ofstream;
00061 using std::ifstream;
00062 using std::fstream;

This is the problem basically
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 7
  • 6
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now