C++ gmp and homebrew

2019-05-03 07:22发布

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.

标签: c++ homebrew gmp
1条回答
smile是对你的礼貌
2楼-- · 2019-05-03 07:39
登录 后发表回答