0

I have a C++ application with the below source code:

#include <cstdint>
#include <iostream>
#include <vector>

#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/stdx.hpp>
#include <mongocxx/uri.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>

using bsoncxx::builder::stream::close_array;
using bsoncxx::builder::stream::close_document;
using bsoncxx::builder::stream::document;
using bsoncxx::builder::stream::finalize;
using bsoncxx::builder::stream::open_array;
using bsoncxx::builder::stream::open_document;

int main(int argc, char** argv)
{
    std::cout << "\nJust to be sure!" << std::endl;

    // Making a connection to Mongo
    mongocxx::instance instance{};
    mongocxx::client client{mongocxx::uri{}};

    // Access a database
    mongocxx::database db = client["results"];

    std::cout << "\ndone." << std::endl;

    return 0;
}

I compile it using the below CMakeLists.txt file:

cmake_minimum_required(VERSION 3.7)
project(testing)

set(APP_SOURCES
    test.cpp
)

link_directories(../../installed_mongocxx/lib)
add_executable(testapp ${APP_SOURCES})
target_link_libraries(testapp mongocxx bsoncxx)

target_include_directories(testapp PUBLIC 
                            ../../installed_mongocxx/include/mongocxx/v_noabi
                            ../../installed_mongocxx/include/bsoncxx/v_noabi
                            E:/Softwares/Libraries/Boost/boost_1_64_0
)

install(TARGETS testapp 
        DESTINATION bin)

I compile the program using MSBuild on Windows 10 64bit without errors, and upon running it gives this error;

The ordinal 4694 could not be located in the dynamic library libmongoc-1.0.dll

Is there anything wrong with the C++ code or CMakeLists.txt that could be explanation of the error?

acm
  • 12,183
  • 5
  • 39
  • 68
Amani
  • 16,245
  • 29
  • 103
  • 153
  • ***Is there anything wrong with the C++ code or CMakeLists.txt that could be explanation of the error?*** I say no, this is a dll confilct. One example is using a dll that is the different than the import library. – drescherjm Jun 19 '17 at 16:30
  • @drescherjm, I also compile the libraries at the same time, so I do not see that as a possibility. – Amani Jun 19 '17 at 16:35
  • Do you have any chance of having a different copy of `libmongoc-1.0.dll` installed on your system? – drescherjm Jun 19 '17 at 16:53
  • @drescherjm, No, that is the only one. The full error message points to which library is trying to use; `The ordinal 4694 could not be located in the dynamic library E:\Projects\mongocxx\attempt_vs\installed_app\bin\libmongoc-1.0.dll`. So I'm sure it is using the one that has just been compiled. – Amani Jun 19 '17 at 16:57
  • I would try a clean build for that dll. – drescherjm Jun 19 '17 at 16:59
  • @drescherjm, did that several times with same error message. – Amani Jun 19 '17 at 17:00
  • I am out of ideas. I don't think this can be solved by changing the `CMakeLists.txt` however. – drescherjm Jun 19 '17 at 17:02
  • I notice that libmongoc and libbson are missing from the link directories and link libraries entries. Can you try adding them to see if that changes anything? – Saghm Jun 22 '17 at 21:22

2 Answers2

1

That's unlikely. The question is what .LIB file you're using to link the DLL. When you build a DLL, that also creates a small .LIB file. This is basically just a table of contents. If you mix the .LIB file from one build with the .DLL from another build, you may run into incompatibilities.

In this case, the .LIB file will be taken from ../../installed_mongocxx/lib, but the .DLL might not be. The DLL will be found at runtime, by Windows rules.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • Got it! I'm cleaning everything now and start afresh. – Amani Jun 19 '17 at 17:39
  • I did a clean build of everything. When running the executable from cmd nothing happen. When opening the executable from WinDbg (64x) got the same error message. – Amani Jun 19 '17 at 18:16
  • @Amani: That suggests you find a different DLL in the second case. [DLL search rules](https://msdn.microsoft.com/en-us/library/windows/desktop/ms682586(v=vs.85).aspx) – MSalters Jun 19 '17 at 18:18
  • I do not think so since the error message clearly point to the path of the DLL, which is the new one I created and there is no any other in my system. – Amani Jun 19 '17 at 18:25
  • At this point, I'd use [Dependency Walker](http://www.dependencywalker.com). It's been the main tool to debug DLL loading issues for 2 decades. – MSalters Jun 19 '17 at 18:29
  • Thank you, the Dependency Walker points to many missing "EXT-MS-WIN-***.DLL". – Amani Jun 19 '17 at 18:45
  • Ignore the errors about "EXT-MS-WIN-***.DLL" :https://stackoverflow.com/questions/36240215/dependency-walker-missing-dlls – drescherjm Jun 19 '17 at 18:49
1

I notice that you've been asking a number of questions related to developing with mongocxx lately that all seem related. I encourage you to either ask a question on our mongodb-user Google Group or on our Jira project, which will make it easier for us to assist you in any follow-up questions you might have without needing to have a conversation in multiple places.

(Apologies for posting this as an answer rather than a comment; StackOverflow seems to have a length limit on comments, and I couldn't fit this in one)

Saghm
  • 221
  • 1
  • 4