C ++ gmp和自制软件 [英] C++ gmp and homebrew
问题描述
我在Mac上,并且已经通过homebrew
安装了gcc
和gmp
.
I'm on a mac and I've installed gcc
and gmp
through homebrew
.
要测试安装,我从此处:
#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;
}
首先,如果我尝试g++ test.cpp -lgmpxx -lgmp
它会抱怨
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.
所以我尝试了g++ test.cpp -lgmpxx -lgmp -I/usr/local/include/
ld: library not found for -lgmpxx
collect2: error: ld returned 1 exit status
所以我尝试了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
有趣的是,如果我用cout
注释掉行并尝试g++ test.cpp -lgmpxx -lgmp -I/usr/local/include/ -L/usr/local/lib/ && ./a.out
,则不会有任何抱怨.特别是c = a+b;
行不需要注释掉,因此我感觉好像正在发生某些事情.
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?
按照@Will的建议,我已经尝试过g++ test.cpp -lgmpxx -lgmp -I/usr/local/include/ -L/usr/local/lib/ -m32
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
如果我正确理解了错误消息,则似乎库确实是为64位构建的,甚至声明mpz_class a, b, c
也将无法使用-m32
进行编译.
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
.
推荐答案
从此答案尝试运行xcode-select --install
.