0

I'm using gcc(version 11) to compile my .cpp file, and the code is below, which is copied from the official website of 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;
}

I used the compiling command like this:

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

After that, I got some errors like this:

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

I know that Undefined Symbols error often comes up because of the link error, but the detailed error messages looks like that these errors are from ginac itself.
I have tried different orders of these -l parameters, but the errors always exist.
I tried clang instead of gcc to compile this file and it works. So I guess I do install the ginac library correctly. But I'm still confused about these errors when using gcc.
Has anyone met this problem?
I would be very appreciate if anyone could give me some advice!

  • Order of object files and libraries when linking matters. Always link with libraries *last*, after the object (or source) files are listed. – Some programmer dude Apr 13 '22 at 08:16
  • And instead of using `gcc` I suggest you use the `g++` front-end program, as that will automatically link with the C++ library. – Some programmer dude Apr 13 '22 at 08:17
  • Thanks so much for your reply. I tried `g++ hello.cpp -o hello -I /usr/local/include -L /usr/local/lib -lstdc++ -lginac -lcln`, but it doesn't work either. @Some programmer dude – JellyFishDing Apr 13 '22 at 08:26
  • Does the `cln` library depend on the `ginac` library? Then it must come before. Generically, if source, object or library `A` depend on library `L`, then `A` must come before `L` on the command line. – Some programmer dude Apr 13 '22 at 08:28
  • 1
    Actually the `ginac` library depends on the `cln` library, so I think `-lginac -lcln` is right. – JellyFishDing Apr 13 '22 at 09:21

1 Answers1

0

I also had issues using:

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

but it did work with:

g++ -o hello hello.cpp `pkg-config --cflags --libs ginac`
dubc
  • 41
  • 5