'GMP works when compiling with makefile but not with cmake

I am trying to include <gmp.h> in my project. A very simple Makefile compiles it successfully:

CC=gcc -std=c11 -w
LIBR=-lm -lgmp
FLAGS=-O0

all: test

test: test.o rsa.h
    $(CC) $(FLAGS) -o test test.c rsa.c $(LIBR)

test.o: test.c $(DEPS)
    $(CC) $(FLAGS) -c test.c $(LIBR)
    
rsa.o: $(SUBF)rsa.c rsa.h
    $(CC) $(FLAGS) -c  rsa.c $(LIBR)

clean:
    rm -f *.o test

by adding the -lgmp flag. However, when I try to compile using cmake, I am not able to make it work. I have tried the following :

make_minimum_required(VERSION 3.10)

# set the project name
project(Tutorial)

set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)

find_package(GMP REQUIRED)

# add the executable
add_executable(Tutorial test.c)
target_link_libraries(Tutorial gmp libgmp gmpxx)

while adding the FindGMP.cmake file which contains the following:

# Try to find the GMP libraries:
# GMP_FOUND - System has GMP lib
# GMP_INCLUDE_DIR - The GMP include directory
# GMP_LIBRARIES - Libraries needed to use GMP

if (GMP_INCLUDE_DIR AND GMP_LIBRARIES)
    # Force search at every time, in case configuration changes
    unset(GMP_INCLUDE_DIR CACHE)
    unset(GMP_LIBRARIES CACHE)
endif (GMP_INCLUDE_DIR AND GMP_LIBRARIES)

find_path(GMP_INCLUDE_DIR NAMES gmp.h)
if(STBIN)
    find_library(GMP_LIBRARIES NAMES libgmp.a gmp.lib libgmp-10 libgmp gmp)
else(STBIN)
    find_library(GMP_LIBRARIES NAMES libgmp.so gmp.lib libgmp-10 libgmp gmp)
endif(STBIN)

if(GMP_INCLUDE_DIR AND GMP_LIBRARIES)
   set(GMP_FOUND TRUE)
endif()

if(GMP_FOUND)
    message(STATUS "Configured GMP: -I${GMP_INCLUDE_DIR} -L${GMP_LIBRARIES}")
else(GMP_FOUND)
    message(STATUS "Could NOT find GMP")
endif(GMP_FOUND)

mark_as_advanced(GMP_INCLUDE_DIR GMP_LIBRARIES)

I have also tried different versions of the FindGMP.cmake which I found online but none of them works. cmake outputs:

-- Configured GMP: -I/usr/local/include -L/usr/local/lib/libgmp.dylib

but fails to build with fatal error: 'gmp.h' file not found. I have no idea why I can't manage to compile with cmake and I have tried all the solutions I could find online. Do you have any idea of how to solve this issue?

I am working on a MacOS Catalina 10.15.7



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source