Getting started with GMP and Xcode

So I'm pretty new to coding and such like. I'm currently taking a C++ module in university which has a particular bent towards mathematical programming. We are required to use the GMP library, and it just works on the campus PCs, but I'd like to be able to use it at home on my Mac(s) with Xcode. I'd appreciate some help in getting it to work, preferably in simple steps!


So far I've downloaded the .tar.bz2 from gmplib.org, and unzipped (untarred?) it. I've followed the instructions here to install it, although I remain unconvinced that it's actually worked 100%.


In an Xcode project (I'm using the latest version on OS 10.9 btw) I've out the Other Linker Flags to -lgmpxx -lgmp (which is very similar to what we had to do to set up GMP for our user on campus). In main.cpp I can #include <gmpxx.h> and it recognises things like mpz_class etc. but it won't compile and run.


Any help greatly appreciated. But bear in mind I'm not a whizz with Xcode or a command line!


David

iMac (21.5-inch Mid 2011), OS X Mavericks (10.9)

Posted on Dec 2, 2013 9:19 AM

Reply
7 replies

Dec 2, 2013 9:48 AM in response to dw153

dw153 wrote:


it won't compile and run.


Is there some reason why you think it won't compile and run? An error message perhaps?


You will have to add "/usr/local/include" to your header search paths and add "/usr/local/lib" to your library search paths. Then, in your target > link binaries with libraries build phase add gmpxx and gmp.

Dec 2, 2013 10:03 AM in response to etresoft

etresoft wrote:


You will have to add "/usr/local/include" to your header search paths and add "/usr/local/lib" to your library search paths. Then, in your target > link binaries with libraries build phase add gmpxx and gmp.


I've done this, and am now getting this error:


warning: skipping file '/usr/local/include/gmp.h' (unexpected file type 'sourcecode.c.h' in Frameworks & Libraries build phase)


(and the same for gmpxx.h)


I also get this:


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

Undefined symbols for architecture x86_64:

"operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, __mpz_struct const*)", referenced from:

std::__1::basic_ostream<char, std::__1::char_traits<char> >& operator<<<__mpz_struct [1]>(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, __gmp_expr<__mpz_struct [1], __mpz_struct [1]> const&) in main.o

ld: symbol(s) not found for architecture x86_64

clang: error: linker command failed with exit code 1 (use -v to see invocation)


So GMP has not been built for the right system architecture? How can I rebuild using the right architecture?


Thanks for your help 🙂

Dec 2, 2013 12:13 PM in response to dw153

You must have done something more. It sounds like you are trying to compile a header file.


Here is what I did. This is a complete list. If you do anything extra, that would be wrong.


1) Downloaded GMP from http://gmplib.org/#DOWNLOAD (The first try downloaded some silly GNU archive-du-jour so I had to track down the gmp-5.1.3.tar.bz2 file and download that).

2) cd ~/Downloads

3) tar jzxvf gmp-5.1.3.tar.gz2

4) cd gmp-5.1.3

5) ./configure

6) make

7) I run with a standard account so I did "su admin", "sudo bash", "cd ~jdaniel/Downloads/gmp-5.1.3", and "make install". You could probably just do "sudo make install".

8) I created an Xcode project using Application > Command-line tool > C

9) I replaced the main.c file with content from http://gmplib.org/list-archives/gmp-discuss/2008-March/003085.html because I know nothing about GMP.

10) I added "/usr/local/include" to the Header Search Paths on the main project

11) I added "/usr/local/lib" to the Library Search Paths on the main project

12) I added libgmp.a to the target's Build Phases tab under LinkBinary With Libraries

13) I the toolbar I went to gmptest > Edit Scheme and added some command line arguments for the demo code I downloaded.

14) Run

Dec 2, 2013 3:06 PM in response to etresoft

Thanks for the instructions. I have followed them all except:


3) When I put tar jzxvf gmp-5.1.3.tar.gz2 it said something along the lines of cannot call -j and -z, so I instead went with jxvf, which appeared to work.


7) My user has admin privileges so I used sudo make install.


8) I used C++


9) I used my own code.


12) This is in /usr/local/lib right? I also added libgmpxx.a since my lecturer appears to be using gmpxx


13) Skipped entirely.


But I've still got back this message:


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

Undefined symbols for architecture x86_64:

"operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, __mpz_struct const*)", referenced from:

std::__1::basic_ostream<char, std::__1::char_traits<char> >& operator<<<__mpz_struct [1]>(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, __gmp_expr<__mpz_struct [1], __mpz_struct [1]> const&) in main.o

ld: symbol(s) not found for architecture x86_64

clang: error: linker command failed with exit code 1 (use -v to see invocation)

Dec 2, 2013 4:00 PM in response to dw153

dw153 wrote:


3) When I put tar jzxvf gmp-5.1.3.tar.gz2 it said something along the lines of cannot call -j and -z, so I instead went with jxvf, which appeared to work.

That was a typo.


8) I used C++

12) This is in /usr/local/lib right? I also added libgmpxx.a since my lecturer appears to be using gmpxx

Are you sure about #5?


Because my build doesn't even have gmpxx. C++ support is not enabled by default. How did you build it if you used "./configure"? What other options did you use? Or did you not build it from source?


If I enable C++ with "--enable-cxx", then I do get a libgmpxx.a, but it works just as well as it did in C.


You are, like me, running on a 64-bit machine. Both Xcode and the command-line tools are running in 64-bit mode. You shouldn't have any files of a different architecture.

Dec 3, 2013 2:23 AM in response to etresoft

My mistake. Our lecturer provided a zip of gmp501 (which I guess is already compiled since it looks radically different from what you download from gmplib.org). It contains three folders, lib, include and share. I copied the contents of lib into /usr/local/lib and likewise for include. I thought running an install would overwrite the files, so didn't mention it. Clearly, this was not the case. I have now tracked down the offending files and deleted them.


In Xcode I have removed all references to libgmpxx, and I've got rid of the first error message. But I still have the one saying undefined symbols

Dec 3, 2013 6:16 AM in response to dw153

Please review my earlier post. I didn't omit anything that you needed to do. I didn't add anything that you didn't need to do. Delete what you have and start over.


To go off on your own and use your Mac requires self-sufficiency. If your school had intended to support the Mac then why would they give you precompiled code that does nothing but waste your time for two days. This is a two minute job. Any Mac support you get from your school is designed to turn any such two minute Mac job into a three day ordeal so that you will stop using that Mac.


School tasks are designed to weed students out so that the only ones left are those who don't need help. All their actions are designed to weed, not to support. Using a Mac will require you to do more work but it will also force you to learn more about what is going on underneath. This is a crucial test. Do you want to be one of the crowd or do you want to be the best?

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Getting started with GMP and Xcode

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.