Page 1 of 1

Why can't linker find gettimeofday?

Posted: Fri Aug 01, 2014 9:11 am
by Heliotopos
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:

Code: Select all

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:

Code: Select all

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.

Thanks

Re: Why can't linker find gettimeofday?

Posted: Fri Aug 01, 2014 10:28 am
by rnixon
Is it from a C lib or C++ lib? If C, are you using extern 'C'?

Re: Why can't linker find gettimeofday?

Posted: Fri Aug 01, 2014 10:56 am
by dciliske
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.

-Dan

Re: Why can't linker find gettimeofday?

Posted: Fri Aug 01, 2014 11:11 am
by Heliotopos
Both projects use C++. I added an extern "C" declaration like the following, but still get the same linker errors.

Code: Select all

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.

Re: Why can't linker find gettimeofday?

Posted: Fri Aug 08, 2014 8:55 am
by Heliotopos
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.

Re: Why can't linker find gettimeofday?

Posted: Tue Apr 28, 2015 11:19 am
by adamstg
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().

Re: Why can't linker find gettimeofday?

Posted: Wed Apr 27, 2016 12:21 am
by adolfainsley8
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???