Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;GMP图书馆ostream运营商<&书信电报;编译但不';t链接?_C++_Linux_Gmp - Fatal编程技术网

C++ C++;GMP图书馆ostream运营商<&书信电报;编译但不';t链接?

C++ C++;GMP图书馆ostream运营商<&书信电报;编译但不';t链接?,c++,linux,gmp,C++,Linux,Gmp,测试.cpp: $ apt-cache show libgmp10 Package: libgmp10 ... Version: 2:5.0.2+dfsg-2ubuntu2 嗯 链接: #include <gmpxx.h> #include <iostream> using namespace std; int main() { mpz_class x = 42; cout << x; } $ g++ -c test.cpp -o t

测试.cpp

$ apt-cache show libgmp10
Package: libgmp10
...
Version: 2:5.0.2+dfsg-2ubuntu2

链接:

#include <gmpxx.h>
#include <iostream>

using namespace std;

int main()
{
    mpz_class x = 42;

    cout << x;
}
$ g++ -c test.cpp -o test.o
$
$g++test.o-lgmp

测试:o:函数中的STD::Orths&运算符< P>你需要链接C++库和C库:

g++-c test.cpp-o test.o-lgmpxx-lgmp
#                         ^^^^^^^

除了我的答案之外,我可以从我的实验中确认两件事:

  • 由于
    g++-M main.cpp-lgmp
    的输出与
    g++-M main.cpp-lgmp
    的输出相同,因此
    -lgmp
    -lgmpxx的包含内容是相同的
  • g++/gcc对这两个标志使用不同的lib,因为
    g++main.cpp-Wl,-t-lgmp
    不同于
    g++main.cpp-Wl,-t-lgmpxx
    ,只有最后一个有效

  • 我没有GMP方面的经验,但由于这些目录是在gcc配置中硬编码的,至少在这个Ubuntu版本中,您需要让gcc输出更加详细,并耐心地解析所有输出,也许您会找到真正的原因。

    如果有疑问,请使用pkg config验证正确的选项(如果库使用pkg config)@user1824407:
    pkg config libgmp10
    不返回任何内容?这比您认为包名与库名不同更常见,有时甚至标志中的名称也不同。我建议
    pkg config--list all | grep-i gmp
    从结果中选择
    rightName
    ,然后
    pkg config--cflags rightName用于包含
    pkg配置--libs rightName
    用于libs@user1824407:
    pkg-config——列出所有| grep-i gmp
    不返回任何内容
    dpkg-L libgmp10
    返回
    pkg-config
    配置文件(.pc扩展名)事实上,看起来我需要提一下both@AndrewTomazosFathomlingCorps:嗯,我明白了。在
    libgmpxx.so
    上尝试
    ldd
    ,看看
    libgmp.so
    是否显示为依赖项。对我来说是这样的。它确实具有依赖项,在玩具示例中它可以工作,但在没有这两个选项的情况下,我在较大的项目中会出现链接错误:
    /usr/include>/gmpxx.h:1984:未定义对uu gmpf_clear的引用
    。Odd@AndrewTomazosFathomlingCorps我明白了,我认为你很好,只要你使用C++接口包的函数,但是如果你直接使用C函数,那么这些依赖关系就必须被明确地满足。我已经更新了这个帖子。g、 例如+,或者创建一个变量。gmpxx.h几乎只包含调用C函数的内联函数(I/O是少数例外之一)。有些系统可能会令人困惑,因为当您链接到libgmpxx.h时,它们会自动链接到它的依赖项(尤其是libgmp.so)所以您没有注意到您确实应该显式地添加-lgmp。
    $ g++ test.o -lgmp
    test.o: In function `std::ostream& operator<<
        <__mpz_struct [1]>(std::ostream&,
             __gmp_expr<__mpz_struct [1],
                  __mpz_struct [1]> const&)':
    
    test.cpp:(.text._ZlsIA1_12__mpz_structERSoS2_RK10__gmp_exprIT_S4_E[_ZlsIA1_12__mpz_structERSoS2_RK10__gmp_exprIT_S4_E]+0x2a):
    
    undefined reference to `operator<<(std::ostream&, __mpz_struct const*)'
    collect2: error: ld returned 1 exit status