g++ 不能静态 link libmongcxx(r3.0.2) 但动态 link 有效
g++ cannot static link libmongcxx(r3.0.2) but dynamic link works
我使用 mongodb 站点的示例代码来说明这里的问题。 OS:ArchLiux,c++是link到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.
代码在 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"];
document << "hello" << "world";
collection.insert_one(document.view());
auto cursor = collection.find({});
for (auto&& doc : cursor) {
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
Link 它没有 -static 工作:
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"
}
现在使用 -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
下面显示了许多错误消息:
/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 to
mongoc_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 to
mongoc_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 to
mongoc_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 to
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):
undefined reference to
mongoc_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 to
mongoc_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 to
mongoc_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 to
mongoc_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 to
mongoc_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 to
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):
undefined reference to
bson_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 to
bson_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 to
bson_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 to
bson_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 to
bson_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 to
bson_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
-static
标志强制链接器只接受静态库而不接受任何共享库。换句话说,为了 运行,它不需要在 运行 时依赖动态库。
很可能 mongocxx
有一些依赖性。
要实现静态链接,系统上必须存在库的存档 (.a) 版本。
另一个可能的问题是链接器命令行中静态库的顺序确实很重要,因此如果依赖于不同的静态库,这也可能是一个问题。链接器将按照它们在命令行中的顺序处理库,并且从每个静态库中它只会提取那些需要的符号(与链接器当时拥有的信息一样多)
使用nm。这将为您提供符号名称。
我使用 mongodb 站点的示例代码来说明这里的问题。 OS:ArchLiux,c++是link到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.
代码在 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"];
document << "hello" << "world";
collection.insert_one(document.view());
auto cursor = collection.find({});
for (auto&& doc : cursor) {
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
Link 它没有 -static 工作:
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"
}
现在使用 -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
下面显示了许多错误消息:
/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 to
mongoc_client_new_from_uri' /home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x10c): undefined reference tomongoc_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 tomongoc_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 tomongoc_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 tomongoc_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 tomongoc_client_get_read_concern' /home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x24b): undefined reference to
mongoc_read_concern_copy' /home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x27c): undefined reference tomongoc_read_concern_destroy' /home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x2a3): undefined reference to
mongoc_read_concern_destroy' /usr/lib/libmongocxx.a(client.cpp.o): In functionmongocxx::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 to
mongoc_client_set_read_prefs' /usr/lib/libmongocxx.a(client.cpp.o): In functionmongocxx::v_noabi::client::read_preference() const': /home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x3c3): undefined reference to
mongoc_client_get_read_prefs' /home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x3cb): undefined reference tomongoc_read_prefs_copy' /home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x3fc): undefined reference to
mongoc_read_prefs_destroy' /home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x423): undefined reference tomongoc_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 tomongoc_client_get_uri' /home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x46b): undefined reference to
mongoc_uri_copy' /home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x49c): undefined reference tomongoc_uri_destroy' /home/dean/work/github/mongo-cxx-driver/src/mongocxx/client.cpp:(.text+0x4c3): undefined reference to
mongoc_uri_destroy' /usr/lib/libmongocxx.a(client.cpp.o): In functionmongocxx::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 to
mongoc_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 to
bson_oid_to_string' /usr/lib/libbsoncxx.a(oid.cpp.o): In functionbsoncxx::v_noabi::oid::get_time_t() const': /home/dean/work/github/mongo-cxx-driver/src/bsoncxx/oid.cpp:(.text+0x1ea): undefined reference to
bson_oid_get_time_t' /usr/lib/libbsoncxx.a(oid.cpp.o): In functionbsoncxx::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 to
bson_oid_compare' /usr/lib/libbsoncxx.a(oid.cpp.o): In functionbsoncxx::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 to
bson_oid_compare' /usr/lib/libbsoncxx.a(oid.cpp.o): In functionbsoncxx::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 to
bson_oid_compare' /usr/lib/libbsoncxx.a(oid.cpp.o): In functionbsoncxx::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 to
bson_oid_compare' /usr/lib/libbsoncxx.a(oid.cpp.o): In functionbsoncxx::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 to
bson_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
-static
标志强制链接器只接受静态库而不接受任何共享库。换句话说,为了 运行,它不需要在 运行 时依赖动态库。
很可能 mongocxx
有一些依赖性。
要实现静态链接,系统上必须存在库的存档 (.a) 版本。
另一个可能的问题是链接器命令行中静态库的顺序确实很重要,因此如果依赖于不同的静态库,这也可能是一个问题。链接器将按照它们在命令行中的顺序处理库,并且从每个静态库中它只会提取那些需要的符号(与链接器当时拥有的信息一样多)
使用nm。这将为您提供符号名称。