undefined reference to mongo::curTimeMillis64()

35 views
Skip to first unread message

Emanuele

unread,
Apr 23, 2016, 8:56:15 AM4/23/16
to mongodb-user
Hi,

someone can help me with this issue?

i have this sample code:

#include <iostream>
#include <mongo/util/time_support.h>

using namespace std;

int main()
{
    cout
<< mongo::curTimeMillis64() << endl;
   
return 0;
}

and compile with:

g++ -I/tmp/include prova.cpp -o prova -L/tmp/lib -lmongoclient

the result is:

/tmp/ccH0vDvx.o: In function `main':
prova.cpp:(.text+0x5): undefined reference to `
mongo::curTimeMillis64()'
collect2: error: ld returned 1 exit status

I use the 26compat of mongo cxx driver and i have also check with nm the symbols within the library:

nm /tmp/lib/libmongoclient.so | grep curTime
00000000000ea510 t _ZN5mongo13curTimeMicrosEv
00000000000ea4f0 t _ZN5mongo15curTimeMicros64Ev
00000000000ea440 t _ZN5mongo15curTimeMillis64Ev

everything seems to be ok but the compile fails. Any suggestions?

Thanks

Andrew Morrow

unread,
Apr 23, 2016, 9:45:12 AM4/23/16
to mongod...@googlegroups.com

Hi -

Please see comments below:

On Fri, Apr 22, 2016 at 8:21 AM, Emanuele <carb...@gmail.com> wrote:
Hi,

someone can help me with this issue?

i have this sample code:

#include <iostream>
#include <mongo/util/time_support.h>

using namespace std;

int main()
{
    cout
<< mongo::curTimeMillis64() << endl;
   
return 0;
}


Unfortunately, due to the provenance of 26compat and legacy drivers as originally part of the server code, you cannot include its headers individually like this.

The only headers you may directly include are mongo/bson/bson.h and mongo/dbclient.h. These facade headers define macros that are needed to make the other headers actually work.

If you really just need the time_support.h header, you can pick that up via bson.h. But, presumably you want to do more than just use BSON, so you should include the dbclient.h header.


 
and compile with:

g++ -I/tmp/include prova.cpp -o prova -L/tmp/lib -lmongoclient


This looks fine.


 
the result is:

/tmp/ccH0vDvx.o: In function `main':
prova.cpp:(.text+0x5): undefined reference to `
mongo::curTimeMillis64()'
collect2: error: ld returned 1 exit status
 
I use the 26compat of mongo cxx driver and i have also check with nm the symbols within the library:

First, is there a particular reason you are using the 26compat driver? The newer legacy-1.1.x releases have a huge number of improvements and bug fixes over the older 26compat driver. If at all possible, I recommend upgrading to at least that. Even better however would be to switch to using the new 3.0.1 C++11 driver from the master branch. If C++11 is not an option, I'd recommend using the plain MongoDB C driver. The 26compat and legacy drivers are headed for end-of-life in the not too distant future.


 

nm /tmp/lib/libmongoclient.so | grep curTime
00000000000ea510 t _ZN5mongo13curTimeMicrosEv
00000000000ea4f0 t _ZN5mongo15curTimeMicros64Ev
00000000000ea440 t _ZN5mongo15curTimeMillis64Ev


The fact that the 't' next to the curTimeMillis64 is lowercase indicates that the symbol has local visibility - it isn't actually exported from libmongoclient.so, so you can't call it.

Unlike some of the other functions in time_support.h, curTimeMillis64 isn't tagged with MONGO_CLIENT_API, so it isn't part of the public interface to the driver.


 
everything seems to be ok but the compile fails. Any suggestions?

I checked and curTimeMillis64 isn't exported on the legacy branch either. So, this is arguably a bug, and if you really need to call curTimeMillsi64, please feel free to file a bug here: https://jira.mongodb.org/browse/CXX. Note that any fix will likely only be made to the legacy branch, not the 26compat branch.

Your other option is to link to the static version of libmongoclient from either branch, which does not enforce symbol visibility. If you do that, you must add STATIC_LIBMONGOCLIENT to the compile definitions when including the mongo driver headers.

Finally, your other option is to upgrade to one of the other, newer drivers. If you can use C++11, I strongly recommend using the new C++11 driver on the master branch. If not, consider using the C driver.

Please let me know if there is anything else I can do to help.

Thanks,
Andrew

Emanuele

unread,
Apr 27, 2016, 8:53:44 AM4/27/16
to mongodb-user
Hi Andrew,

sorry for delay but i haven't received email notification about the new response. Many thanks for your detailed description.

Unfortunately, i have an old version of my library client that use old construct like ScopedDbConnection that was removed. Previously, i used the libmongoclient builded from source that contains client and server code together.

At the moment i can not work to align my library with the most recent branch of libmongoclient and i have searched a rapid replacement.

I prefer to work to schedule the align to the most recent branch with C++11 that open a bug to JIRA.

For solving my problem i have replaced: 

unsigned long long mongo::curTimeMillis64()

with:

Date_t mongo::jsTime()

because in my case the time obtained from curTimeMillis64 would be used to build a Date_t struct.

Thanks,
Emanuele

Andrew Morrow

unread,
Apr 27, 2016, 1:17:51 PM4/27/16
to mongod...@googlegroups.com


For solving my problem i have replaced: 

unsigned long long mongo::curTimeMillis64()

with:

Date_t mongo::jsTime()

because in my case the time obtained from curTimeMillis64 would be used to build a Date_t struct.



That seems like a very reasonable workaround.

 

Reply all
Reply to author
Forward
0 new messages