Search code examples
gcchyperlinkgmp

Link external library GCC OS X


I have installed GCC 4.7.2 and GMP 5.1.0 and I've written this simple code in main.cpp locate at ~/Desktop:

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

using namespace std;

int main ()
{
    mpz_t a;

    mpz_init(a);

    mpz_set_ui(a, 42);

    cout << "Hello, world!" << endl;
}

I compile it with:

$ g++ main.cpp -o exe

but I get this error message:

Undefined symbols for architecture x86_64:
  "___gmpz_init", referenced from:
      _main in ccC0FXun.o
  "___gmpz_set_ui", referenced from:
      _main in ccC0FXun.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

I think it's because it doesn't find the GMP library, am I right?

So how can I link an external library such as GMP in GCC?


Solution

  • Your program works fine for me here using:

    g++ main.cpp -o exe -lgmp
    

    Check the GCC documentation for a description of the -l flag.