Why can't linker find gettimeofday?

Topics for the Eclipse Environment
Post Reply
Heliotopos
Posts: 27
Joined: Thu Jun 27, 2013 8:30 am

Why can't linker find gettimeofday?

Post 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
rnixon
Posts: 833
Joined: Thu Apr 24, 2008 3:59 pm

Re: Why can't linker find gettimeofday?

Post by rnixon »

Is it from a C lib or C++ lib? If C, are you using extern 'C'?
User avatar
dciliske
Posts: 623
Joined: Mon Feb 06, 2012 9:37 am
Location: San Diego, CA
Contact:

Re: Why can't linker find gettimeofday?

Post 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
Dan Ciliske
Project Engineer
Netburner, Inc
Heliotopos
Posts: 27
Joined: Thu Jun 27, 2013 8:30 am

Re: Why can't linker find gettimeofday?

Post 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.
Heliotopos
Posts: 27
Joined: Thu Jun 27, 2013 8:30 am

Re: Why can't linker find gettimeofday?

Post 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.
adamstg
Posts: 6
Joined: Fri Mar 06, 2015 8:16 am

Re: Why can't linker find gettimeofday?

Post 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().
adolfainsley8
Posts: 1
Joined: Wed Apr 27, 2016 12:19 am

Re: Why can't linker find gettimeofday?

Post 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???
Post Reply