Search code examples
c++cgcclinkergmp

GMP(MPIR) linker errors on linux


I wrote a public-private key generator using mpir(on windows) and it works fine.

When I try to compile it on a linux machine using gmp library, it throws a whole bunch of linker errors.

/usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 0 has invali
d symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 1 has invali
d symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 2 has invali
d symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 3 has invali
d symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 4 has invali
d symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 5 has invali
d symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 6 has invali
d symbol index 13
...
/usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib/crt1.o: In function `_start'
:
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status

I'm using g++ -lgmp prime.cpp. I haven't used any non-gmp function. Any idea? I'm not adding code as there's a lot of it.


Solution

  • I'm using g++ -lgmp prime.cpp

    This command line is broken in two ways:

    1. You neglected to provide definiton of main
    2. You specified library before source that references it. Should be:

      g++ main.cpp prime.cpp -lgmp

      The order of libraries and sources/objects on command line matters.

    Update:

    There are several files.. main file depends on them, so before building it.. I was trying to build the other files.

    In that case, correct command is:

    # Compile, but don't link, prime.cpp
    g++ -c prime.cpp