【问题标题】:Got an 'Undefined Symbols' error when compiling .cpp file with ginac使用 ginac 编译 .cpp 文件时出现“未定义符号”错误
【发布时间】:2022-11-26 08:56:23
【问题描述】:

我正在使用gcc(version 11)编译我的.cpp文件,代码如下,是从ginac官网复制过来的。

#include <iostream>
#include <ginac/ginac.h>
using namespace std;
using namespace GiNaC;
int main()
{
    symbol x("x"), y("y");
    ex poly;
    for (int i=0; i<3; ++i)
        poly += factorial(i+16)*pow(x,i)*pow(y,2-i);
    cout << poly << endl;
    return 0;
}

我使用了这样的编译命令:

gcc-11 -lstdc++ -lginac -lcln hello.cpp -o hello -I /usr/local/include -L /usr/local/lib

在那之后,我得到了一些这样的错误:

Undefined symbols for architecture x86_64:
  "__ZN5GiNaC12archive_node6add_exERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS_2exE", referenced from:
      __ZNK5GiNaC9containerISt6vectorE7archiveERNS_12archive_nodeE in ccEkVJyo.o
  "__ZN5GiNaC5basic12read_archiveERKNS_12archive_nodeERNS_9containerINSt7__cxx114listEEE", referenced from:
      __ZN5GiNaC9containerISt6vectorE12read_archiveERKNS_12archive_nodeERNS0_INSt7__cxx114listEEE in ccEkVJyo.o
  "__ZN5GiNaC6symbolC1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE", referenced from:
      _main in ccEkVJyo.o
  "__ZN5GiNaC9containerISt6vectorE8reg_infoE", referenced from:
      __ZN5GiNaC9containerISt6vectorE21get_class_info_staticEv in ccEkVJyo.o
  "__ZN5GiNaClsERSoRKNS_2exE", referenced from:
      _main in ccEkVJyo.o
  "__ZNK5GiNaC12archive_node14find_ex_by_locEN9__gnu_cxx17__normal_iteratorIPKNS0_8propertyESt6vectorIS3_SaIS3_EEEERNS_2exERNS_9containerINSt7__cxx114listEEE", referenced from:
      __ZN5GiNaC9containerISt6vectorE12read_archiveERKNS_12archive_nodeERNS0_INSt7__cxx114listEEE in ccEkVJyo.o
  "__ZNK5GiNaC12archive_node19find_property_rangeERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES8_", referenced from:
      __ZN5GiNaC9containerISt6vectorE12read_archiveERKNS_12archive_nodeERNS0_INSt7__cxx114listEEE in ccEkVJyo.o
  "__ZNK5GiNaC5basic10eval_ncmulERKSt6vectorINS_2exESaIS2_EE", referenced from:
      __ZTVN5GiNaC9containerISt6vectorEE in ccEkVJyo.o
  "__ZNK5GiNaC5basic11to_rationalERSt3mapINS_2exES2_NS_10ex_is_lessESaISt4pairIKS2_S2_EEE", referenced from:
      __ZTVN5GiNaC9containerISt6vectorEE in ccEkVJyo.o
  "__ZNK5GiNaC5basic13contract_withEN9__gnu_cxx17__normal_iteratorIPNS_2exESt6vectorIS3_SaIS3_EEEES8_RS7_", referenced from:
      __ZTVN5GiNaC9containerISt6vectorEE in ccEkVJyo.o
  "__ZNK5GiNaC5basic13to_polynomialERSt3mapINS_2exES2_NS_10ex_is_lessESaISt4pairIKS2_S2_EEE", referenced from:
      __ZTVN5GiNaC9containerISt6vectorEE in ccEkVJyo.o
  "__ZNK5GiNaC5basic14subs_one_levelERKSt3mapINS_2exES2_NS_10ex_is_lessESaISt4pairIKS2_S2_EEEj", referenced from:
      __ZNK5GiNaC9containerISt6vectorE4subsERKSt3mapINS_2exES4_NS_10ex_is_lessESaISt4pairIKS4_S4_EEEj in ccEkVJyo.o
  "__ZNK5GiNaC5basic5matchERKNS_2exERSt3mapIS1_S1_NS_10ex_is_lessESaISt4pairIS2_S1_EEE", referenced from:
      __ZTVN5GiNaC9containerISt6vectorEE in ccEkVJyo.o
  "__ZNK5GiNaC5basic6normalERSt3mapINS_2exES2_NS_10ex_is_lessESaISt4pairIKS2_S2_EEES9_RNS_9containerINSt7__cxx114listEEE", referenced from:
      __ZTVN5GiNaC9containerISt6vectorEE in ccEkVJyo.o
  "__ZNK5GiNaC9containerISt6vectorE4infoEj", referenced from:
      __ZTVN5GiNaC9containerISt6vectorEE in ccEkVJyo.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

我知道 Undefined Symbols 错误经常出现是因为链接错误,但详细的错误消息看起来这些错误来自 ginac 本身。
我试过这些-l参数的不同顺序,但错误总是存在。
我尝试用 clang 而不是 gcc 来编译这个文件并且它有效。所以我想我确实正确安装了 ginac 库。但是我在使用gcc时仍然对这些错误感到困惑。
有没有人遇到过这个问题?
如果有人能给我一些建议,我将不胜感激!

【问题讨论】:

  • 链接时目标文件和库的顺序很重要。始终与库链接最后的,在列出对象(或源)文件之后。
  • 而不是使用 gcc 我建议您使用 g++ 前端程序,因为它会自动链接到 C++ 库。
  • 非常感谢你的回复。我试了g++ hello.cpp -o hello -I /usr/local/include -L /usr/local/lib -lstdc++ -lginac -lcln,但也没用。 @一些程序员老兄
  • cln库是否依赖于ginac库?那么它必须先来。通常,如果源、对象或库 A 依赖于库 L,则在命令行中 A 必须位于 L 之前。
  • 实际上ginac库依赖于cln库,所以我认为-lginac -lcln是对的。

标签: c++ gcc clang ginac


【解决方案1】:

我在使用时也遇到了问题:

gcc-11 -lstdc++ -lginac -lcln hello.cpp -o hello -I /usr/local/include -L /usr/local/lib

但它确实适用于:

g++ -o hello hello.cpp `pkg-config --cflags --libs ginac`

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-05
    • 1970-01-01
    • 2022-12-04
    相关资源
    最近更新 更多