g ++不能静态链接libmongcxx(r3.0.2),但动态链接工作 [英] g++ cannot static link libmongcxx(r3.0.2) but dynamic link works

查看:994
本文介绍了g ++不能静态链接libmongcxx(r3.0.2),但动态链接工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用来自mongodb网站的示例代码来显示这里的问题。 OS:ArchLiux,c ++是g ++的链接。$ c $ -


(C)2016自由软件基金会,这是免费软件;请参阅复制条件的
来源。没有保修;甚至不适用于
适销性或针对特定用途的适用性。

代码位于test.cc文件中

  #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];
文件<< 你好<< 世界;

collection.insert_one(document.view());
auto cursor = collection.find({}); (auto&& doc:cursor)

$ b {
std :: cout<< bsoncxx :: to_json(doc)<<的std :: ENDL;


首先将其编译为目标文件:


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

工作:

lockquote
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

这可以运行并打印出一些消息:

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

现在得到-static选项的错误


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

下面显示了许多错误消息:

blockquote
/usr/lib/libmongocxx.a(client.cpp.o):在函数
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):
未定义引用
mongoc_client_new_from_uri'
/home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x10c):
未定义引用 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):
未定义引用旺oc_client_destroy'
/usr/lib/libmongocxx.a(client.cpp.o):函数
mongocxx :: v_noabi :: client ::〜client()':
/home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x1c3):
未定义引用 mongoc_client_destroy'
/usr/lib/libmongocxx.a(client.cpp.o):函数
mongocxx :: v_noabi :: client :: read_concern(mongocxx :: v_noabi :: read_concern )':
/home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x214):
未定义引用 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):
未定义引用 mongoc_client_get_read_concern'
/home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x24b):
未定义引用
mongoc_read_con cern_copy'
/home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x27c):
未定义引用 mongoc_read_concern_destroy '
/home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x2a3):
未定义引用
mongoc_read_concern_destroy '
/usr/lib/libmongocxx.a(client.cpp.o):函数
mongocxx :: v_noabi :: client :: read_preference(mongocxx :: v_noabi :: read_preference)':
/home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x3a4):
未定义引用
mongoc_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):
未定义引用
mongoc_client_get_read_prefs '
/home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x3cb):
un定义的引用 mongoc_read_prefs_copy'
/home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x3fc):
undefined引用
mongoc_read_prefs_destroy'
/home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x423):
undefined引用 mongoc_read_prefs_destroy'
/usr/lib/libmongocxx.a(client.cpp.o):函数
mongocxx :: v_noabi :: client :: uri()const':
/home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x463):
未定义引用 mongoc_client_get_uri'
/home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x46b):
未定义引用
code> mongoc_uri_copy'
/home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x49c):
未定义引用 mongoc_uri_destroy'
/home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x4c3):
undefi ned引用
mongoc_uri_destroy'
/usr/lib/libmongocxx.a(client.cpp.o):在函数
mongocxx :: v_noabi :: client :: write_concern(mongocxx :: v_noabi :: write_concern)':
/home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x504):
未定义引用
mongoc_client_set_write_concern'
/usr/lib/libmongocxx.a(client.cpp.o):函数
`mongocxx :: v_noabi :: client: :write_concern()const':

....


/usr/lib/libbsoncxx.a(oid.cpp.o):在函数
bsoncxx :: v_noabi :: oid :: to_string [abi:cxx11]( )const':
/home/dean/work/github/mongo-cxx-driver/src/bsoncxx/oid.cpp:(.text+0x139):
未定义引用
bson_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):
未定义引用
bson_oid_get_time_t'
/usr/lib/libbsoncxx.a(oid.cpp.o):在函数
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):
未定义引用
bson_oid_compare'
/usr/lib/libbsoncxx.a(oid.cpp.o):在函数
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):
未定义引用
bson_oid_compare'
/usr/lib/libbsoncxx.a(oid.cpp.o):在函数
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):
未定义引用
bson_oid_compare'
/usr/lib/libbsoncxx.a(oid.cpp.o):在函数
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):
未定义的引用
bson_oid_compare'
/usr/lib/libbsoncxx.a(oid.cpp.o):在函数
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):
未定义引用
bson_oid_compare'
/usr/lib/libbsoncxx.a(oid.cpp .o):/ home / dean / work / github / mongo-cxx-driver / src / bsoncxx / oid.cpp :( .text + 0x42f):
更多未定义的引用'bson_oid_compare'follow collect2:
error:ld returned 1 exit status


解决方案

- static 标志强制链接器仅接受静态li而不是任何共享库。换句话说,它不需要在运行时依赖动态库来运行。
最有可能 mongocxx 有一些依赖关系。
要实现静态链接,需要在系统上存在库的存档(.a)版本。



另一个可能的问题是静态顺序链接器命令行中的库确实很重要,所以如果依赖于不同的静态库,这也可能是一个问题。链接器将按照它们在命令行中的顺序处理这些库,并且从每个静态库中只会拖出那些所需的符号(链接器具有尽可能多的信息)。

使用 nm 。这会给你符号名称。


I use the example code from mongodb site to show the problem here. OS: ArchLiux, c++ is a link to g++

[dean@dell_xps_13 ~]$ c++ --version c++ (GCC) 6.2.1 20160830 Copyright (C) 2016 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

code is 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({});

  for (auto&& doc : cursor) {
    std::cout << bsoncxx::to_json(doc) << std::endl;
  }
}

First compile it to 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 it 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 and print out some messages:

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

Now get error with -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 to 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 to 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 to 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 to 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 to 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 to 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 to 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 to 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 to 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 to 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 to `bson_oid_compare' follow collect2: error: ld returned 1 exit status

解决方案

The -static flag forces the linker to accept only static libraries and not any shared libraries. In other words it does not require a dependency on dynamic libraries at runtime in order to run. Most likely mongocxx has some dependencies. To achieve static linking requires that the archive (.a) versions of your libraries exist on the system.

The another possible issue is that the order of static libraries in the linker command line does matter, so that might also be an issue if there is a dependency on different static libs. The linker will process the libraries in order as they are in the command line, and from each static lib it will only pull those symbols that are required (with as much information as the linker has at that time)

Use nm . That will give you the symbol names.

这篇关于g ++不能静态链接libmongcxx(r3.0.2),但动态链接工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆