5

I'm on a mac and I've installed gcc and gmp through homebrew.

To test out my installation I've tried out the simple example from here:

#include <iostream>
#include <gmpxx.h>
using namespace std;
int
main (void)
{
mpz_class a, b, c;

a = 1234;
b = "-5678";
c = a+b;
cout << "sum is " << c << "\n";
cout << "absolute value is " << abs(c) << "\n";

return 0;
}

First of all, if I try g++ test.cpp -lgmpxx -lgmp it complains

test.cpp:9:19: fatal error: gmpxx.h: No such file or directory
 #include <gmpxx.h>
                   ^
compilation terminated.

So I've tried g++ test.cpp -lgmpxx -lgmp -I/usr/local/include/

ld: library not found for -lgmpxx
collect2: error: ld returned 1 exit status

So then I've tried g++ test.cpp -lgmpxx -lgmp -I/usr/local/include/ -L/usr/local/lib/

Undefined symbols for architecture x86_64:
  "operator<<(std::basic_ostream<char, std::char_traits<char> >&, __mpz_struct const*)", referenced from:
      std::basic_ostream<char, std::char_traits<char> >& operator<< <__mpz_struct [1], __mpz_struct [1]>(std::basic_ostream<char, std::char_traits<char> >&, __gmp_expr<__mpz_struct [1], __mpz_struct [1]> const&) in ccPugkZ3.o
      std::basic_ostream<char, std::char_traits<char> >& operator<< <__mpz_struct [1], __gmp_unary_expr<__gmp_expr<__mpz_struct [1], __mpz_struct [1]>, __gmp_abs_function> >(std::basic_ostream<char, std::char_traits<char> >&, __gmp_expr<__mpz_struct [1], __gmp_unary_expr<__gmp_expr<__mpz_struct [1], __mpz_struct [1]>, __gmp_abs_function> > const&) in ccPugkZ3.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

The funny thing is that if I comment out the lines with cout and try g++ test.cpp -lgmpxx -lgmp -I/usr/local/include/ -L/usr/local/lib/ && ./a.out there are no complaints. In particular the line c = a+b; didn't need to be commented out, so I feel like something seems to be happening.

What am I missing? How can I get the sample code to compile?

EDIT:

as per @Will 's suggestion, I've tried g++ test.cpp -lgmpxx -lgmp -I/usr/local/include/ -L/usr/local/lib/ -m32

ld: warning: ld: warning: ignoring file /usr/local/lib//libgmpxx.dylib, file was built for x86_64 which is not the architecture being linked (i386): /usr/local/lib//libgmpxx.dylibignoring file /usr/local/lib//libgmp.dylib, file was built for x86_64 which is not the architecture being linked (i386): /usr/local/lib//libgmp.dylib

Undefined symbols for architecture i386:
  "operator<<(std::basic_ostream<char, std::char_traits<char> >&, __mpz_struct const*)", referenced from:
      std::basic_ostream<char, std::char_traits<char> >& operator<< <__mpz_struct [1], __mpz_struct [1]>(std::basic_ostream<char, std::char_traits<char> >&, __gmp_expr<__mpz_struct [1], __mpz_struct [1]> const&) in ccrPv2wC.o
      std::basic_ostream<char, std::char_traits<char> >& operator<< <__mpz_struct [1], __gmp_unary_expr<__gmp_expr<__mpz_struct [1], __mpz_struct [1]>, __gmp_abs_function> >(std::basic_ostream<char, std::char_traits<char> >&, __gmp_expr<__mpz_struct [1], __gmp_unary_expr<__gmp_expr<__mpz_struct [1], __mpz_struct [1]>, __gmp_abs_function> > const&) in ccrPv2wC.o
  "___gmpz_abs", referenced from:
      __gmp_abs_function::eval(__mpz_struct*, __mpz_struct const*) in ccrPv2wC.o
  "___gmpz_add", referenced from:
      __gmp_binary_plus::eval(__mpz_struct*, __mpz_struct const*, __mpz_struct const*) in ccrPv2wC.o
  "___gmpz_clear", referenced from:
      __gmp_expr<__mpz_struct [1], __mpz_struct [1]>::~__gmp_expr() in ccrPv2wC.o
  "___gmpz_init", referenced from:
      __gmp_expr<__mpz_struct [1], __mpz_struct [1]>::__gmp_expr() in ccrPv2wC.o
      __gmp_expr<__mpz_struct [1], __mpz_struct [1]>::__gmp_expr<__gmp_unary_expr<__gmp_expr<__mpz_struct [1], __mpz_struct [1]>, __gmp_abs_function> >(__gmp_expr<__mpz_struct [1], __gmp_unary_expr<__gmp_expr<__mpz_struct [1], __mpz_struct [1]>, __gmp_abs_function> > const&) in ccrPv2wC.o
  "___gmpz_set_si", referenced from:
      __gmp_expr<__mpz_struct [1], __mpz_struct [1]>::assign_si(long) in ccrPv2wC.o
  "___gmpz_set_str", referenced from:
      __gmp_expr<__mpz_struct [1], __mpz_struct [1]>::operator=(char const*) in ccrPv2wC.o
ld: symbol(s) not found for architecture i386
collect2: error: ld returned 1 exit status

If I am understanding the error message correctly, it seems that the libraries were indeed built for 64-bit, and even the declaration mpz_class a, b, c will fail to compile with -m32.

math4tots
  • 8,540
  • 14
  • 58
  • 95
  • Does adding the switch `-m32` help? I don't have the env to test myself, but it could be that you've installed 32 bit libs? – Will Jul 08 '14 at 20:51
  • @Will Thank you for the suggestion, but unfortunately the switch does not seem to solve the issue. I've posted the error message in an edit to my original post. – math4tots Jul 08 '14 at 21:23
  • put the link path *before* the library name perhaps? `g++ -I/usr/local/include/ -L/usr/local/lib/ -lgmpxx -lgmp test.cpp` (still guessing from phone) – Will Jul 08 '14 at 22:01
  • I would try to compile by myself the same version on some custom, isolated directory, you need to grab source, then use `./configure` with `--enable-cxx` (for C++ support) and e.g. `--prefix=/home/myuser/mygmp` (for both includes and libs, eventually set `CC=gcc` and `CXX=g++`), then `make -j4`, then `make install` see if it works correctly. If so, then probably there is probably some bug within homebrew package. – Grzegorz Szpetkowski Jul 09 '14 at 14:26
  • 1
    Probably GMP was compiled for `clang++ -stdlib=libc++` which is a different incompatible mode. – Marc Glisse Jul 11 '14 at 09:14

1 Answers1

1

From this answer, try running xcode-select --install.

On mac, g++ (clang) fails to search /usr/local/include and /usr/local/lib by default

Community
  • 1
  • 1
leishman
  • 1,017
  • 11
  • 7
  • Don't know about the user, but this does not work for me on OS X 10.11.6. I think it's a bug in Apple's linker, because it does work on 10.10.5. – John Perry Feb 06 '17 at 15:37
  • @JohnPerry Did you every figure out a solution? I'm still on 10.11.6 and I'm getting this error. – John Apr 30 '18 at 21:14
  • @John To be honest, I don't remember, but neither do I think so. For a while I was using `ports` and in order to install GNAT I had to just go ahead & remove gcc altogether. – John Perry May 01 '18 at 01:50