[CXX-2374] LNK2019: unresolved external symbol "__declspec(dllimport) using MongoDB in static mode Created: 19/Sep/21  Updated: 22/Jun/22  Resolved: 21/Sep/21

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

Type: Task Priority: Unknown
Reporter: Dariusz D Assignee: Ezra Chung
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Hey

 

Ive build mongo C in static mode. 2x, 1x for debug, and 1x for debug-release.

Between each build, I copied the generated files to a new folder as the debug files do not come with "d" suffix which then overwrites other files.

 

With these files, I then configures mongo CXX to use static mongo C files and asked it to produce both Static & Shared libraries.

 

That has been compiled. For this, I made 2 vs projects. 1 for debug and 1 for debug-release so to not confuse configs...

I linked each project to their proper mongo C .lib files.

So I end up with

Mongo C & Mongo CXX in debug

Mongo C & Mongo CXX in debug release.

I've then included the resulting lib files in my cmake project using if statement to determine if I'm in debug or not >

if (CMAKE_BUILD_TYPE MATCHES Debug)
    set(3rd_libs_MONGO
            ${MONGODB_ROOT}lib/bsoncxxd.lib
            ${MONGODB_ROOT}lib/bson-static-1.0d.lib
            ${MONGODB_ROOT}lib/mongoc-static-1.0d.lib
            ${MONGODB_ROOT}lib/bsoncxx-staticd.lib
            ${MONGODB_ROOT}lib/mongocxx-staticd.lib
            )
else ()
    set(3rd_libs_MONGO
            ${MONGODB_ROOT}lib/bsoncxx.lib
            ${MONGODB_ROOT}lib/bson-static-1.0.lib
            ${MONGODB_ROOT}lib/mongoc-static-1.0.lib
            ${MONGODB_ROOT}lib/bsoncxx-static.lib
            ${MONGODB_ROOT}lib/mongocxx-static.lib
            )
endif ()
target_link_libraries(${PROJECT_NAME} ${3rd_libs_MONGO})

After that when I try to run the app I get > 

main.cpp.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl mongocxx::v_noabi::options::client_session::client_session(void)" (__imp_??0client_session@options@v_noabi@mongocxx@@QEAA@XZ) referenced in function "void __cdecl arrayTests(void)" (?arrayTests@@YAXXZ)

and 300 more errors like this.

 

This project was working with previous dynamic libraries, .dll, from last year. But as I was updating mongo I figures I might as well go with static & recompile it all.

How can I "fix" this issue? 

Regards

Dariusz

 

Win10x64

vs 17+ 



 Comments   
Comment by Ezra Chung [ 21/Sep/21 ]

Thank you for reporting this question, dariusz1989@gmail.com.

We recommend following the instructions to build the C++ driver as static libraries and refer to the static CMake project example given by the C++ driver for how to integrate the static libraries into your CMake project.

To elaborate, rather than explicitly enumerating the static libraries in target_link_libraries(), we recommend using find_package(mongocxx) and the mongo::mongocxx_static target to link against the C++ driver as demonstrated in the static CMake project example. This will specify essential compile definitions such as MONGOCXX_STATIC (the absence of which is causing the __declspec(dllimport) errors) as well as handle the linking of all necessary dependencies. Additionally, rather than renaming the static libraries to avoid conflicts between build configurations, we recommend installing the libraries for each build configuration into a dedicated directory using CMAKE_INSTALL_PREFIX (or a multi-config generator) and conditionally provide the appropriate directory to CMAKE_PREFIX_PATH instead:

if (CMAKE_BUILD_TYPE MATCHES DEBUG)
    list(APPEND CMAKE_PREFIX_PATH "${MONGODB_ROOT}Debug/lib/cmake")
else ()
    list(APPEND CMAKE_PREFIX_PATH "${MONGODB_ROOT}RelWithDebInfo/lib/cmake")
endif ()
 
find_package(mongocxx REQUIRED)
 
target_link_libraries(${PROJECT_NAME} PRIVATE mongo::mongocxx_static)

As this is neither a bug nor a feature request, we will close this issue. For further assistance, please create a post in our community forum.

Generated at Wed Feb 07 22:05:43 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.