c++ 使用ginac编译.cpp文件时出现“未定义的符号”错误

yhxst69z  于 2022-11-27  发布在  其他
关注(0)|答案(1)|浏览(198)

我使用gcc(版本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时,我仍然对这些错误感到困惑。
有人遇到过这个问题吗?
如果有人能给予我一些建议,我将非常感激!

1rhkuytd

1rhkuytd1#

我在使用以下各项时也遇到问题:

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`

相关问题