user813869
user813869

Reputation: 31

Using GMP in Xcode 4 -- linking problems

I am trying to use the GMP 5.0.2 libraries in a demo C++ project in Xcode 4.0.2, but I'm having trouble getting the linking to work.

I looked at the comments in the StackOverflow post: Adding Linker Flags in Xcode, found the GMP library and header files in /usr/local/lib and /usr/local/include and dragged them to the project target. This allowed the project to compile, but apparently not to link. The error I'm getting is

    Undefined symbols for architecture x86_64:
  "operator>>(std::istream&, __mpz_struct*)", referenced from:
      _main 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)

Any suggestions for what I should try next? Thanks.

#include <iostream>
#include "gmp.h"
#include <stdio.h>

using namespace std;

int main (int argc, const char * argv[])
{

    mpz_t x;
    mpz_init(x);
    cin >> x;
    cout << "x = " << endl;

    // insert code here...
    std::cout << "Hello, World!\n";
    return 0;
}

Upvotes: 3

Views: 1918

Answers (5)

Senseful
Senseful

Reputation: 91681

If you don't need all of the features of GMP, you should be able to simply drag and drop the mini-gmp.h and mini-gmp.m files into your project and start using the basic features of the library.

Upvotes: -1

cassius
cassius

Reputation: 406

A couple of ideas:

1) As nes says, be sure to add the -lgmp flag to your linker call. 2) Try including "gmpxx.h" and adding -lgmpxx -lgmp to the linker. (The order can be important!) It shouldn't be necessary for straight C code...in theory...but those look more like C++ errors to me. 3) Consider the MPIR library. It is the same as GMP but with a more Windows friendly view of the world. 4) Check the manual - sometimes there are other includes you need for specific functions.

Upvotes: 1

zodiacmcfly
zodiacmcfly

Reputation: 23

you should do like this:

  1. project
  2. build phases
  3. link binary with librarys
  4. click "+" choose your own gmp library,like /opt/local/lib/libgmp.10.dylib (ps:I use macport to complie GMP).

Upvotes: 0

nes1983
nes1983

Reputation: 15756

To run your program with GMP, remember to add the -lgmp flag to the linker, like so: (via Rob Keniger):

Right-click the target in the Xcode Groups and Files list and select Get Info from the contextual menu. In the Build tab, type linker into the search field and then locate the Other Linker Flags item.

Double-click the Other Linker Flags item and add -lgmp.

Upvotes: 2

Tam&#225;s
Tam&#225;s

Reputation: 48061

How was your GMP library compiled? Was it compiled for 32-bit architectures only by any chance? If so, you won't be able to use it in a 64-bit project; you'll have to add -arch i386 to the list of compiler flags to make XCode create a 32-bit executable.

Upvotes: 0

Related Questions