Home > Blockchain >  C compiler not finding gmp linked library
C compiler not finding gmp linked library

Time:10-16

I'm trying to compile a simple C program on an M1 Mac that includes gmp.h. For whatever reason, however, it continuously says that it can't find the library. I checked to see if it exists via locate libgmp.a to which it says it is located in /opt/local/lib/libgmp.a. I also attempted to install it via brew to no avail. I really don't understand what I'm doing wrong here. I also checked the PATH environment variable to make sure this path was included, which it is.

The output of find /opt/local -type f -name "gmp.h" is /opt/local/include/gmp.h, and the output of find /opt/local -type f -name "libgmp*" is

/opt/local/libgmp.a
/opt/local/lib/libgmpxx.4.dylib
/opt/local/lib/libgmp.a
/opt/local/lib/libgmpxx.a
/opt/local/lib/libgmp.10.dylib

I compiled by linking via -L/opt/local/lib -lgmp does not fix the issue.

Command used: gcc main.c -o main.o -L/opt/local/lib -lgmp

CodePudding user response:

To compile a program that relies on a header file:

#include <gmp.h>

located in a non-standard location /usr/local/include/gmp.h you need to tell the compiler where to find it by setting the directory where to find the header file:

gcc -c main.c -I/usr/local/include

Note: gpp -x c -v

To link the program, you need to tell the compiler which library to use (-l) and where to find that library (-L) which in this case /usr/local/lib/gmp.a:

gcc main.o -o main -L/usr/local/lib -lgmp

Note: gcc -print-search-dirs | grep ^libraries

If you are using a dynamic library (.so), you may also need to tell the linker where to find the library at run-time:

export LD_LIBRARY_PATH=/usr/local/lib
./main
  •  Tags:  
  • cgmp
  • Related