I'm trying to add UnitTest++ to an existing project in NBEclipse 2.5.3, and the library builds successfully from source. However the existing project fails to link with these errors:
C:\Foo\UnitTest++\libUnitTest++.a(TimeHelpers.o): In function `UnitTest::Timer::GetTimeInMs() const':
C:\Foo\UnitTest++/src/Bar/TimeHelpers.cpp:23: undefined reference to `gettimeofday'
C:\Foo\UnitTest++\libUnitTest++.a(TimeHelpers.o): In function `UnitTest::Timer::Start()':
C:\Foo\UnitTest++/src/Bar/TimeHelpers.cpp:16: undefined reference to `gettimeofday'
collect2: ld returned 1 exit status
The compiler didn't seem to have a problem picking up the forward declaration from sys/time.h. The definition appears to be in the C standard library:
nburn/gcc-m68k/m68k-elf/lib $ nm libc.a | grep gettime
lib_a-gettimeofday.o:
00000000 T _gettimeofday_r
U gettimeofday
U _gettimeofday_r
I even tried explicitly including libc.a in the linker libraries page under Settings in my project properties - which seems unnecessary, but I can't figure out why ld can't find that function. All the pieces seem to be in place. I've seen a couple somewhat-related posts but nobody seems to have an answer.
Ahh, yes, this does look like a C vs. C++ issue. Create an 'extern "C"' declaration for the function. Also, if you're not aware of it, you should take a quick look at the topic of C++ name mangling.
extern "C" {
int gettimeofday(struct timeval *tv, void *tz);
}
My friend helpfully pointed out that the 'U' designation in the nm output means the label is referenced but undefined, which explains why the linker isn't finding it in that library. I still don't know where it's defined though, if at all.
I never did find gettimeofday(); no idea why a forward declaration would be included in time.h but no definition provided in the standard library.
UnitTest++ was using gettimeofday() for a simple timer, so I just rewrote that bit using time() and difftime(). If anyone else runs into this problem that could be a workaround.
Heliotopos wrote:I never did find gettimeofday(); no idea why a forward declaration would be included in time.h but no definition provided in the standard library.
UnitTest++ was using gettimeofday() for a simple timer, so I just rewrote that bit using time() and difftime(). If anyone else runs into this problem that could be a workaround.
i'm just going to bump this thread in the hopes that someone found a solution.
I wanted to get more precision when getting the time, and ran into the same issue, i'll have to use time().