Ofey
Ofey

Reputation: 167

gmp strange behaviour that doesn't let me compile new project

I've installed on my ubuntu gmp using this command:

sudo apt-get install libgmp3-dev

and it worked fine. Now I'm trying to create a new project put simply writing

  #include "gmp.h"
  #include <stdio.h>
  #include <time.h>
  #include <stdlib.h>
  #include <string.h>
  #include <unistd.h>




  int main(){

    mpz_t num;
    mpz_init(num);
    printf("%s\n",mpz_get_str (NULL, 10, num));

    mpz_clear(num);


   return 0;

  }

give me

> gcc -lgmp mil.c  /tmp/ccHvV9kT.o: In function `main':
> mil.c:(.text+0x1f): undefined reference to `__gmpz_init'
> mil.c:(.text+0x35): undefined reference to `__gmpz_get_str'
> mil.c:(.text+0x49): undefined reference to `__gmpz_clear' collect2:
> error: ld returned 1 exit status

I just copy-pasted the code of my previous project and I get the same error(in all the function that I created), but compiling my old project I don't get any error. What is my problem???

Upvotes: 0

Views: 189

Answers (1)

Order of arguments to gcc matters a big lot.

Try to use (you want warnings and debug info, so)

 gcc -Wall -Wextra -g mil.c -lgmp -o milprog

Then run ./milprog. You may want to use the gdb debugger on it, with

gdb ./milprog

and you may want (for benchmarking purposes) to ask the compiler to optimize, by adding (before -g) something like -O2 -march=native

Learn to use GNU make (or some other build automation tool, like ninja), see this.

Be sure to use a version control system like git.

BTW, I find more logical and more elegant to include "gmp.h" after (not before, as you did) the inclusion of standard headers (like <stdio.h>).

Upvotes: 2

Related Questions