Need some help linking a compiled static version of mongocxx to my c++ project in a wsl instance. The project is supposed to be a shared library that can be linked to other libs/exes.
I’m using the c version 1.23.4 and c++ version 3.7.1 of the drivers available from the releases page.
The parameters used to build them were the following:
c:
cmake . -DCMAKE_BUILD_TYPE=Release -DBUILD_VERSION=1.23.4 -DENABLE_MONGODB_AWS_AUTH=OFF -B./lib
cmake --build ./lib --config Release --parallel
cmake --install ./lib --prefix ./libInstall --config Release
c++:
cmake . -DCMAKE_BUILD_TYPE=Release -DBUILD_VERSION=3.7.1 -DCMAKE_PREFIX_PATH=…/mongo-c-driver-1.23.4 -B./lib -DBSONCXX_POLY_USE_BOOST=1 -DBoost_INCLUDE_DIR=/home/USER/boost/1_82_0 -DBUILD_SHARED_AND_STATIC_LIBS=ON
cmake --build ./lib --config Release --parallel
sudo cmake --build ./lib --target install
This generated my desired .a files to be used.
Now, onto my personal project, which is basically a simple create instance and list all documents.
I’ve tried linking the libs in my CMakeLists.txt as follows:
cmake_minimum_required(VERSION 3.22.1)
project(TestMongo VERSION 1.0.0.1)
include(GNUInstallDirs)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_WARN_DEPRECATED OFF)
set(CMAKE_VERBOSE_MAKEFILE=ON)
set(BSONCXX_STATIC_PATH ${CMAKE_SOURCE_DIR}/../mongo-cxx-driver-r3.7.1/lib/install/lib/libbsoncxx-static.a)
set(MONGOCXX_STATIC_PATH ${CMAKE_SOURCE_DIR}/../mongo-cxx-driver-r3.7.1/lib/install/lib/libmongocxx-static.a)
add_library(TestMongo SHARED main.cpp)
target_link_libraries(TestMongo PUBLIC ${MONGOCXX_STATIC_PATH})
target_link_libraries(TestMongo PUBLIC ${BSONCXX_STATIC_PATH})
target_include_directories(TestMongo PRIVATE ${CMAKE_SOURCE_DIR}/../mongo-cxx-driver-r3.7.1/lib/install/include/bsoncxx/v_noabi)
target_include_directories(TestMongo PRIVATE ${CMAKE_SOURCE_DIR}/../mongo-cxx-driver-r3.7.1/lib/install/include/mongocxx/v_noabi)
target_include_directories(TestMongo PRIVATE ${CMAKE_SOURCE_DIR}/../boost/1_82_0)
target_compile_options(TestMongo PUBLIC -fPIC -Wall -Wno-comment -Wunknown-pragmas -Wno-deprecated)
install(TARGETS TestMongo)
add_executable(TestMongoExe executable.cpp)
target_include_directories(TestMongoExe PRIVATE ./)
target_link_libraries(TestMongoExe PRIVATE TestMongo)
and also linking with:
add_library(MONGOCXX_STATIC STATIC IMPORTED)
set_property(TARGET MONGOCXX_STATIC PROPERTY
IMPORTED_LOCATION ${MONGOCXX_STATIC_PATH})
add_library(BSONCXX_STATIC STATIC IMPORTED)
set_property(TARGET BSONCXX_STATIC PROPERTY
IMPORTED_LOCATION ${BSONCXX_STATIC_PATH})
target_link_libraries(TestMongo PUBLIC BSONCXX_STATIC)
target_link_libraries(TestMongo PUBLIC MONGOCXX_STATIC)
Both cases generate the following errors:
/usr/bin/ld: …/mongo-cxx-driver-r3.7.1/lib/install/lib/libmongocxx-static.a(uploader.cpp.o): warning: relocation against _ZN8mongocxx7v_noabi16gridfs_exceptionD1Ev' in read-only section
.text.unlikely’
/usr/bin/ld: …/mongo-cxx-driver-r3.7.1/lib/install/lib/libbsoncxx-static.a(json.cpp.o): relocation R_X86_64_PC32 against symbol `_ZTVN7bsoncxx7v_noabi9exceptionE’ can not be used when making a shared object; recompile with -fPIC
Any ideas on how to fix this? Linking the shared objects worked fine but not the static ones. The suggestion to recompile with -fPIC doesn’t seem relevant in this case.
Thanks in advance!