草庐IT

c++ - g++ 不能静态链接 libmongcxx(r3.0.2) 但动态链接有效

coder 2023-11-04 原文

我使用来自 mongodb 站点的示例代码来说明这里的问题。操作系统:ArchLiux,c++是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

在没有 -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 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

最佳答案

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

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

使用nm .这将为您提供符号名称。

关于c++ - g++ 不能静态链接 libmongcxx(r3.0.2) 但动态链接有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40485621/

有关c++ - g++ 不能静态链接 libmongcxx(r3.0.2) 但动态链接有效的更多相关文章

  1. ruby - 为什么 4.1%2 使用 Ruby 返回 0.0999999999999996?但是 4.2%2==0.2 - 2

    为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返

  2. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  3. ruby - 如何进行排列以有效地定制输出 - 2

    这是一道面试题,我没有答对,但还是很好奇怎么解。你有N个人的大家庭,分别是1,2,3,...,N岁。你想给你的大家庭拍张照片。所有的家庭成员都排成一排。“我是家里的friend,建议家庭成员安排如下:”1岁的家庭成员坐在这一排的最左边。每两个坐在一起的家庭成员的年龄相差不得超过2岁。输入:整数N,1≤N≤55。输出:摄影师可以拍摄的照片数量。示例->输入:4,输出:4符合条件的数组:[1,2,3,4][1,2,4,3][1,3,2,4][1,3,4,2]另一个例子:输入:5输出:6符合条件的数组:[1,2,3,4,5][1,2,3,5,4][1,2,4,3,5][1,2,4,5,3][

  4. ruby-on-rails - Ruby url 到 html 链接转换 - 2

    我正在使用Rails构建一个简单的聊天应用程序。当用户输入url时,我希望将其输出为html链接(即“url”)。我想知道在Ruby中是否有任何库或众所周知的方法可以做到这一点。如果没有,我有一些不错的正则表达式示例代码可以使用... 最佳答案 查看auto_linkRails提供的辅助方法。这会将所有URL和电子邮件地址变成可点击的链接(htmlanchor标记)。这是文档中的代码示例。auto_link("Gotohttp://www.rubyonrails.organdsayhellotodavid@loudthinking.

  5. ruby-on-rails - Prawn - 表格单元格内的链接 - 2

    我正在尝试用Prawn生成PDF。在我的PDF模板中,我有带单元格的表格。在其中一个单元格中,我有一个电子邮件地址:cell_email=pdf.make_cell(:content=>booking.user_email,:border_width=>0)我想让电子邮件链接到“mailto”链接。我知道我可以这样链接:pdf.formatted_text([{:text=>booking.user_email,:link=>"mailto:#{booking.user_email}"}])但是将这两行组合起来(将格式化文本作为内容)不起作用:cell_email=pdf.make_c

  6. ruby - 使用 `+=` 和 `send` 方法 - 2

    如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:

  7. python - 是否可以使用 Ruby 或 Python 禁用 anchor /引用来发出有效的 YAML? - 2

    是否可以在PyYAML或Ruby的Psych引擎中禁用创建anchor和引用(并有效地显式列出冗余数据)?也许我在网上搜索时遗漏了一些东西,但在Psych中似乎没有太多可用的选项,而且我也无法确定PyYAML是否允许这样做.基本原理是我必须序列化一些数据并将其以可读的形式传递给一个不是真正的技术同事进行手动验证。有些数据是多余的,但我需要以最明确的方式列出它们以提高可读性(anchor和引用是提高效率的好概念,但不是人类可读性)。Ruby和Python是我选择的工具,但如果有其他一些相当简单的方法来“展开”YAML文档,它可能就可以了。 最佳答案

  8. ruby - 如何计算 Liquid 中的变量 +1 - 2

    我对如何计算通过{%assignvar=0%}赋值的变量加一完全感到困惑。这应该是最简单的任务。到目前为止,这是我尝试过的:{%assignamount=0%}{%forvariantinproduct.variants%}{%assignamount=amount+1%}{%endfor%}Amount:{{amount}}结果总是0。也许我忽略了一些明显的东西。也许有更好的方法。我想要存档的只是获取运行的迭代次数。 最佳答案 因为{{incrementamount}}将输出您的变量值并且不会影响{%assign%}定义的变量,我

  9. ruby - 在 Ruby 中动态创建数组 - 2

    有没有办法在Ruby中动态创建数组?例如,假设我想遍历用户输入的书籍数组:books=gets.chomp用户输入:"TheGreatGatsby,CrimeandPunishment,Dracula,Fahrenheit451,PrideandPrejudice,SenseandSensibility,Slaughterhouse-Five,TheAdventuresofHuckleberryFinn"我把它变成一个数组:books_array=books.split(",")现在,对于用户输入的每一本书,我想用Ruby创建一个数组。伪代码来做到这一点:x=0books_array.

  10. ruby - 是否可以将 IRB 提示配置为动态更改? - 2

    我想在IRB中浏览文件系统并让提示更改以反射(reflect)当前工作目录,但我不知道如何在每个命令后进行提示更新。最终,我想在日常工作中更多地使用IRB,让bash溜走。我在我的.irbrc中试过这个:require'fileutils'includeFileUtilsIRB.conf[:PROMPT][:CUSTOM]={:PROMPT_N=>"\e[1m:\e[m",:PROMPT_I=>"\e[1m#{pwd}>\e[m",:PROMPT_S=>"FOO",:PROMPT_C=>"\e[1m#{pwd}>\e[m",:RETURN=>""}IRB.conf[:PROMPT_MO

随机推荐