[CXX-118] Mongo C++ BSON code will not compile / link properly when using BSONArrayBuilder Created: 29/May/12  Updated: 11/Sep/19  Resolved: 10/Mar/14

Status: Closed
Project: C++ Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Mark Ponthier Assignee: Unassigned
Resolution: Done Votes: 1
Labels: cxxmove, driver, legacy-cxx, windows
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows and RedHat 5



 Description   

When we build the demo project everything works fine...but when we change the demo to use the BSONArrayBuilder, we get the following link errors (one Windows and similar link errors on Linux)
------------------------------------------------------------

Error	1	error LNK2001: unresolved external symbol "private: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const * const mongo::BSONObjBuilder::numStrs" (?numStrs@BSONObjBuilder@mongo@@0QBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@B)	C:\Build\Dev3\firescope-agent\thirdparty\mongodb-src\bson\bsondemo\bsondemo.obj	bsondemo
Error	2	error LNK2001: unresolved external symbol "private: static bool mongo::BSONObjBuilder::numStrsReady" (?numStrsReady@BSONObjBuilder@mongo@@0_NA)	C:\Build\Dev3\firescope-agent\thirdparty\mongodb-src\bson\bsondemo\bsondemo.obj	bsondemo
Error	3	error LNK1120: 2 unresolved externals	C:\Build\Dev3\firescope-agent\thirdparty\mongodb-src\bson\bsondemo\Debug\bsondemo.exe	1	1	bsondemo

We found the definitions for numStrs and numStrsReady in oid.cpp, but when we include that file, we then get the following link errors
------------------------------------------------------------

Error	1	error LNK2019: unresolved external symbol "void __cdecl mongo::asserted(char const *,char const *,unsigned int)" (?asserted@mongo@@YAXPEBD0I@Z) referenced in function "private: static struct mongo::OID::MachineAndPid __cdecl mongo::OID::genMachineAndPid(void)" (?genMachineAndPid@OID@mongo@@CA?AUMachineAndPid@12@XZ)	C:\Build\Dev3\firescope-agent\thirdparty\mongodb-src\bson\bsondemo\oid.obj	bsondemo
Error	2	error LNK2019: unresolved external symbol "public: static unsigned __int64 __cdecl mongo::Security::getNonceDuringInit(void)" (?getNonceDuringInit@Security@mongo@@SA_KXZ) referenced in function "private: static struct mongo::OID::MachineAndPid __cdecl mongo::OID::genMachineAndPid(void)" (?genMachineAndPid@OID@mongo@@CA?AUMachineAndPid@12@XZ)	C:\Build\Dev3\firescope-agent\thirdparty\mongodb-src\bson\bsondemo\oid.obj	bsondemo
Error	3	error LNK2019: unresolved external symbol "public: static unsigned __int64 __cdecl mongo::Security::getNonce(void)" (?getNonce@Security@mongo@@SA_KXZ) referenced in function "public: void __cdecl mongo::OID::init(void)" (?init@OID@mongo@@QEAAXXZ)	C:\Build\Dev3\firescope-agent\thirdparty\mongodb-src\bson\bsondemo\oid.obj	bsondemo
Error	4	error LNK2019: unresolved external symbol "class boost::system::error_category const & __cdecl boost::system::generic_category(void)" (?generic_category@system@boost@@YAAEBVerror_category@12@XZ) referenced in function "void __cdecl boost::system::`dynamic initializer for 'posix_category''(void)" (??__Eposix_category@system@boost@@YAXXZ)	C:\Build\Dev3\firescope-agent\thirdparty\mongodb-src\bson\bsondemo\oid.obj	bsondemo
Error	5	error LNK2019: unresolved external symbol "class boost::system::error_category const & __cdecl boost::system::system_category(void)" (?system_category@system@boost@@YAAEBVerror_category@12@XZ) referenced in function "void __cdecl boost::system::`dynamic initializer for 'native_ecat''(void)" (??__Enative_ecat@system@boost@@YAXXZ)	C:\Build\Dev3\firescope-agent\thirdparty\mongodb-src\bson\bsondemo\oid.obj	bsondemo
Error	6	error LNK1120: 5 unresolved externals	C:\Build\Dev3\firescope-agent\thirdparty\mongodb-src\bson\bsondemo\Debug\bsondemo.exe	1	1	bsondemo

If we include the jsobj.h file instead of bson.h on Linux, then we get a whole new set of boost link errors. Can you get us a working demo project or directions which will build a BSONArrayBuilder object like the following
------------------------------------------------------------

    mongo::BSONObjBuilder topLevel;
    {
        topLevel.append("\"ip\"","10.0.22.163");
        topLevel.append("\"~collection\"", "cloud_monitoring");
        topLevel.append("\"fsedition\"", "SMONGO");
 
        mongo::BSONArrayBuilder items (topLevel.subarrayStart("cloud_attribute_results"));
        {
            mongo::BSONObjBuilder item (items.subobjStart());
            item.append("dly", 6);
            item.append("er",  "error string in the event of an error collecting the attribute");
            item.append("ere", 5);
            item.append("lc", 4);
            item.append("lv", "string representation of the collected value");
            item.append("pt", 3);
            item.append("op", "operation");
            item.append("adesc", "attribute description");
            item.append("atyp", 2);
            item.append("vtyp", 1);
            item.done();
 
            mongo::BSONObjBuilder item2 (items.subobjStart());
            item2.append("dly", 10);
            item2.append("er",  "error string in the event of an error collecting the attribute");
            item2.append("ere",  20);
            item2.append("lc", 30);
            item2.append("lv", "string representation of the collected value");
            item2.append("pt", 40);
            item2.append("op", "operation");
            item2.append("adesc", "attribute description");
            item2.append("atyp", 50);
            item2.append("vtyp", 60);
            item2.done();
 
            mongo::BSONObjBuilder item20 (items.subobjStart());
            item20.append("dly", 100);
            item20.append("er",  "error string in the event of an error collecting the attribute");
            item20.append("ere",  200);
            item20.append("lc", 300);
            item20.append("lv", "string representation of the collected value");
            item20.append("pt", 400);
            item20.append("op", "operation");
            item20.append("adesc", "attribute description");
            item20.append("atyp", 500);
            item20.append("vtyp", 600);
            item20.done();
        }
        items.done();
    }
    mongo::BSONObj finished (topLevel.obj());



 Comments   
Comment by Andrew Morrow (Inactive) [ 10/Mar/14 ]

Closing per submitter request.

Comment by Mark Ponthier [ 05/Mar/14 ]

Please close/resolve. This is an old ticket.

Comment by Tad Marshall [ 11/Apr/13 ]

This is on the "wish list" for version 2.5, and we have a couple of proposals for fixing it. Some of the BSON code has been targeted for refactoring, so we just need this fix to not get in the way of that work.

Comment by Kiran Kashalkar [ 11/Apr/13 ]

Any word on the fix for this?
The BSONObjBuilder and BSONArrayBuilder don't seem to be working in code due to these link errors, creating a major inconvenience.

I am using Mongo 2.4

Comment by Tad Marshall [ 04/Jun/12 ]

This can't be fixed without some changes to code organization (involving numStrs) that we can't make in the 2.2 timeframe. Setting "fix by version" to "2.3 desired" so we can revisit this after 2.2 ships.

Comment by Tad Marshall [ 01/Jun/12 ]

I was able to get your code to compile by putting two lines in bson/bsonobjbuilder.h (starting at line 600 in today's code) under #ifdef:

        static std::string numStr( int i ) {
#ifdef MONGO_EXPOSE_MACROS
            if (i>=0 && i<100 && numStrsReady)
                return numStrs[i];
#endif
            StringBuilder o;
            o << i;
            return o.str();
        }

The numStrs/numStrsReady stuff is just an optimization and, as you observed, using it starts to drag in the rest of MongoDB. The MONGO_EXPOSE_MACROS switch separates "client" code from "server" code, so this #ifdef keeps this code out of your build. I'll see if we can make this change or something like it to the master codebase.

Generated at Wed Feb 07 21:58:15 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.