Pinguino Girl

Hi, I'm new to C++ and I am writing a project in it that I want to build on both Windows CE and VxWorks, so I am compiling the project in both MS Visual Studio and Wind River Workbench.

There are a number of places in the app where I want to get the current date and time (number of seconds since 1/1/1970) but I can't figure out how to make it work! From what I can tell, time(time_t * timer) is the way to go, but while this compiles fine for VxWorks, when I compile it for Windows I get a strange linker error which says that there are unresolved externals in the function that contains the call to time().

Help! (Thanks in advance!)

Rachel



Re: Smart Devices Native C++ Development time function

einaros

All you do is include time.h, and do something similar to the following snippet

time_t x;
time(&x);






Re: Smart Devices Native C++ Development time function

Pinguino Girl

That's exactly what I want to do--here is essentially what I'm doing:

#include "time.h"

void foo(void)
{
time_t curr_time;
time(&curr_time);
}

or I was wondering if this was an option:

void foo(void)
{
time_t curr_time = time(NULL);
}

Either way I get the same error:

error LNK2019: unresolved external symbol time referenced in function "protected: void __cdecl foo(void)"

It seems like it should be simple, what is going on





Re: Smart Devices Native C++ Development time function

nobugz

The preprocessor plays games with time_t and time(). They get redefined to __time64_t and _time64(). What exactly are the undefined symbols you get

Also make sure the the project property Linker, Input, Ignore all default libraries is set to No.






Re: Smart Devices Native C++ Development time function

Pinguino Girl

The property setting is as you suggested, and as for the undefined symbol, do you mean unresolved symbol All the error message says is "unresolved external symbol time...". (Is there some way to get more information about the error that you're wanting to know ) The problem is with the function call, because I can declare the time_t variables without a problem, it is only when I add the function call that I get the failure.

(Thanks for you help, I'm really tearing my hair out over this)





Re: Smart Devices Native C++ Development time function

nobugz

There is no time() function in the CRT, just _time32 and _time64. Is there any chance that you're using Wind River's time.h header file instead of Microsoft's In case of doubt, set the /showIncludes compile option.






Re: Smart Devices Native C++ Development time function

Pinguino Girl

Using _time64 allowed the project to compile in VS, but will not compile in WB with this call. I wish MS was more POSIX-compliant! Do you know of any options for me that will work for both Otherwise I can try defining a macro that will be defined differently depending on the platform, but this would not be ideal.



Re: Smart Devices Native C++ Development time function

einaros

Apparently, the old time functions arent implemented in the runtime library for Windows CE. You could resort to Win32 methods like GetLocalTime and GetTimeZoneInformation instead for your Visual Studio build.






Re: Smart Devices Native C++ Development time function

einaros

As an alternative, you could use the Dinkumware time implementation in both builds.




Re: Smart Devices Native C++ Development time function

nobugz

VS2005 is POSIX compliant as long has you use their header file. They are just trying to make sure that the Y2K038 problem won't hurt you. Hey, it is only 32 year away. I'll be retired by then though...

Anyhoo, please explain why you have to include Wind River's time.h instead of MSFT's.






Re: Smart Devices Native C++ Development time function

Pinguino Girl

I am using the VS2005 time.h header file. But if it is POSIX-compliant, why won't it recognize structs like "timespec" and functions like "clock_gettime" which are part of the POSIX API



Re: Smart Devices Native C++ Development time function

nobugz

Oops, don't get cranky with me. I just said that it implements the POSIX function time(). They obviously chose not to implement clock_gettime(). I don't know why but that is not the point. If you include MSFT's version of time.h, you should not get an undefined, oops, unresolved symbol when you link. Use /showIncludes and post the output and we'll help you out.






Re: Smart Devices Native C++ Development time function

einaros

nobugz wrote:
Oops, don't get cranky with me. I just said that it implements the POSIX function time(). They obviously chose not to implement clock_gettime(). I don't know why but that is not the point. If you include MSFT's version of time.h, you should not get an undefined, oops, unresolved symbol when you link. Use /showIncludes and post the output and we'll help you out.

As I've pointed out, the time method isn't implemented in the runtime library for Windows CE.






Re: Smart Devices Native C++ Development time function

Pinguino Girl

I wasn't trying to be rude at all, I was just asking--I do appreciate your help. What is MSFT The time.h I am using is the one provided for Windows Mobile, since I am using CE. So, this version is undoubtedly different from the full framework version and perhaps that is the discrepency here, as einaros notes time() is not implemented in this version. Because as I understand, this time.h provides _time64() which will not work in VxWorks and the VxWorks time.h provides time() and clock_gettime() which will not work in CE.

I will look into this Dinkumware suggestion by einaros as well.





Re: Smart Devices Native C++ Development time function

Avery Simonsen

I'm not sure it's completely clear for readers of this thread what the conclusive answer to this problem is. I have looked into it and ran into a thread where (someone claiming to be) a member of the CE team explained that time.h does indeed contain definitions of POSIX like time-functions, but they are not implemented anywhere, so anyone attempting to link with them will get a linker error.

I found the following implementation of time_t time(time_t *t) based on the time functions that do exist on CE. I haven't verified the code myself, but at a glance it looks sound enough. Just place this snippet in your code and Robert's your fathers brother.

time_t time( time_t *inTT )
{
SYSTEMTIME sysTimeStruct;
FILETIME fTime;
ULARGE_INTEGER int64time;
time_t locTT = 0;

if ( inTT == NULL ) {
inTT = &locTT;
}

GetSystemTime( &sysTimeStruct );
if ( SystemTimeToFileTime( &sysTimeStruct, &fTime ) ) {
memcpy( &int64time, &fTime, sizeof( FILETIME ) );
/* Subtract the value for 1970-01-01 00:00 (UTC) */
int64time.QuadPart -= 0x19db1ded53e8000;
/* Convert to seconds. */
int64time.QuadPart /= 10000000;
*inTT = (time_t)int64time.QuadPart;
}
return *inTT;
}