c++ - g++ cannot static link libmongcxx(r3.0.2) but dynamic link works -


i use example code mongodb site show problem here. os: archliux, c++ link g++

[dean@dell_xps_13 ~]$ c++ --version c++ (gcc) 6.2.1 20160830 copyright (c) 2016 free software foundation, inc. free software; see source copying conditions. there no warranty; not merchantability or fitness particular purpose.

code in test.cc file

#include <iostream>  #include <bsoncxx/builder/stream/document.hpp> #include <bsoncxx/json.hpp>  #include <mongocxx/client.hpp> #include <mongocxx/instance.hpp>  int main(int, char**) {   mongocxx::instance inst{};   mongocxx::client conn{mongocxx::uri{}};    bsoncxx::builder::stream::document document{};    auto collection = conn["testdb"]["testcollection"];   document << "hello" << "world";    collection.insert_one(document.view());   auto cursor = collection.find({});    (auto&& doc : cursor) {     std::cout << bsoncxx::to_json(doc) << std::endl;   } } 

first compile object file:

c++ -g -std=c++11 -i../include -i/usr/include/bsoncxx/v_noabi -i/usr/include/mongocxx/v_noabi -wall -o test.o -c ./test.cc

link without -static works:

c++ test.o -o test -static-libgcc -static-libstdc++ -l/usr/lib -lpthread -lmongocxx -lbsoncxx -lboost_log -lboost_log_setup -lboost_system -lboost_thread -lboost_filesystem

this can run , print out messages:

[dean@dell_xps_13 mongo-cxx-driver-r3.0.2]$ ./test {     "_id" : {         "$oid" : "58218e821b489308ae4411d1"     },      "hello" : "world" } 

now error -static option

c++ test.o -o test -static-libgcc -static-libstdc++ -static -l/usr/lib -lpthread -lmongocxx -lbsoncxx -lboost_log -lboost_log_setup -lboost_system -lboost_thread -lboost_filesystem

many error messages show below:

/usr/lib/libmongocxx.a(client.cpp.o): in function mongocxx::v_noabi::client::client(mongocxx::v_noabi::uri const&, mongocxx::v_noabi::options::client const&)': /home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x28): undefined reference tomongoc_client_new_from_uri' /home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x10c): undefined reference mongoc_client_destroy' /usr/lib/libmongocxx.a(client.cpp.o): in function mongocxx::v_noabi::client::operator=(mongocxx::v_noabi::client&&)': /home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x190): undefined reference mongoc_client_destroy' /usr/lib/libmongocxx.a(client.cpp.o): in function mongocxx::v_noabi::client::~client()': /home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x1c3): undefined reference mongoc_client_destroy' /usr/lib/libmongocxx.a(client.cpp.o): in function mongocxx::v_noabi::client::read_concern(mongocxx::v_noabi::read_concern)': /home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x214): undefined reference mongoc_client_set_read_concern' /usr/lib/libmongocxx.a(client.cpp.o): in function mongocxx::v_noabi::client::read_concern() const': /home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x243): undefined reference mongoc_client_get_read_concern' /home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x24b): undefined reference tomongoc_read_concern_copy' /home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x27c): undefined reference mongoc_read_concern_destroy' /home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x2a3): undefined reference tomongoc_read_concern_destroy' /usr/lib/libmongocxx.a(client.cpp.o): in function mongocxx::v_noabi::client::read_preference(mongocxx::v_noabi::read_preference)': /home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x3a4): undefined reference tomongoc_client_set_read_prefs' /usr/lib/libmongocxx.a(client.cpp.o): in function mongocxx::v_noabi::client::read_preference() const': /home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x3c3): undefined reference tomongoc_client_get_read_prefs' /home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x3cb): undefined reference mongoc_read_prefs_copy' /home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x3fc): undefined reference tomongoc_read_prefs_destroy' /home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x423): undefined reference mongoc_read_prefs_destroy' /usr/lib/libmongocxx.a(client.cpp.o): in function mongocxx::v_noabi::client::uri() const': /home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x463): undefined reference mongoc_client_get_uri' /home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x46b): undefined reference tomongoc_uri_copy' /home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x49c): undefined reference mongoc_uri_destroy' /home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x4c3): undefined reference tomongoc_uri_destroy' /usr/lib/libmongocxx.a(client.cpp.o): in function mongocxx::v_noabi::client::write_concern(mongocxx::v_noabi::write_concern)': /home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x504): undefined reference tomongoc_client_set_write_concern' /usr/lib/libmongocxx.a(client.cpp.o): in function `mongocxx::v_noabi::client::write_concern() const':

....

/usr/lib/libbsoncxx.a(oid.cpp.o): in function bsoncxx::v_noabi::oid::to_string[abi:cxx11]() const': /home/dean/work/github/mongo-cxx-driver/src/bsoncxx/oid.cpp:(.text+0x139): undefined reference tobson_oid_to_string' /usr/lib/libbsoncxx.a(oid.cpp.o): in function bsoncxx::v_noabi::oid::get_time_t() const': /home/dean/work/github/mongo-cxx-driver/src/bsoncxx/oid.cpp:(.text+0x1ea): undefined reference tobson_oid_get_time_t' /usr/lib/libbsoncxx.a(oid.cpp.o): in function bsoncxx::v_noabi::oid_compare(bsoncxx::v_noabi::oid const&, bsoncxx::v_noabi::oid const&)': /home/dean/work/github/mongo-cxx-driver/src/bsoncxx/oid.cpp:(.text+0x24f): undefined reference tobson_oid_compare' /usr/lib/libbsoncxx.a(oid.cpp.o): in function bsoncxx::v_noabi::operator<(bsoncxx::v_noabi::oid const&, bsoncxx::v_noabi::oid const&)': /home/dean/work/github/mongo-cxx-driver/src/bsoncxx/oid.cpp:(.text+0x2af): undefined reference tobson_oid_compare' /usr/lib/libbsoncxx.a(oid.cpp.o): in function bsoncxx::v_noabi::operator>(bsoncxx::v_noabi::oid const&, bsoncxx::v_noabi::oid const&)': /home/dean/work/github/mongo-cxx-driver/src/bsoncxx/oid.cpp:(.text+0x30f): undefined reference tobson_oid_compare' /usr/lib/libbsoncxx.a(oid.cpp.o): in function bsoncxx::v_noabi::operator<=(bsoncxx::v_noabi::oid const&, bsoncxx::v_noabi::oid const&)': /home/dean/work/github/mongo-cxx-driver/src/bsoncxx/oid.cpp:(.text+0x36f): undefined reference tobson_oid_compare' /usr/lib/libbsoncxx.a(oid.cpp.o): in function bsoncxx::v_noabi::operator>=(bsoncxx::v_noabi::oid const&, bsoncxx::v_noabi::oid const&)': /home/dean/work/github/mongo-cxx-driver/src/bsoncxx/oid.cpp:(.text+0x3cf): undefined reference tobson_oid_compare' /usr/lib/libbsoncxx.a(oid.cpp.o):/home/dean/work/github/mongo-cxx-driver/src/bsoncxx/oid.cpp:(.text+0x42f): more undefined references `bson_oid_compare' follow collect2: error: ld returned 1 exit status

the -static flag forces linker accept static libraries , not shared libraries. in other words not require dependency on dynamic libraries @ runtime in order run. mongocxx has dependencies. achieve static linking requires archive (.a) versions of libraries exist on system.

the possible issue order of static libraries in linker command line matter, might issue if there dependency on different static libs. linker process libraries in order in command line, , each static lib pull symbols required (with information linker has @ time)

use nm . give symbol names.


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -