cmake build failed on macos catalina 10.15

Viewed 8755

I have recently installed macos catalina with Xcode 11.1 and updated cmake, clang and llvm

sudo rm -rf /Library/Developer/CommandLineTools
xcode-select --install

$ cmake --version
cmake version 3.15.4

$ which cmake
/usr/local/bin/cmake

$ clang --version
Apple clang version 11.0.0 (clang-1100.0.33.8)
Target: x86_64-apple-darwin19.0.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

$ which clang
/usr/bin/clang

The CMakeLists.txt looks as bellow:

cmake_minimum_required(VERSION 3.14)

project("ROZZETA" VERSION 0.0.1 LANGUAGES C)

# Allow us to import cmake scripts from  ./cmake
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}")
# Compiler flags
set(CMAKE_C_COMPILER /usr/bin/clang CACHE PATH "")

find_package(GMP REQUIRED)
add_executable(Rozzeta main.c)
target_link_libraries(Rozzeta gmp libgmp libgmp.a)

cmake detected gmp successfully :

/usr/local/bin/cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER=/usr/bin/clang -DCMAKE_CXX_COMPILER=c++ -G "CodeBlocks - Unix Makefiles" /<path to project>
-- The C compiler identification is AppleClang 11.0.0.11000033
-- Check for working C compiler: /usr/bin/clang
-- Check for working C compiler: /usr/bin/clang -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Using toolchain file: .
-- Found GMP: /usr/local/include/gmp.h and /usr/local/lib/libgmp.a
-- Configuring done
-- Generating done

build failed :

cmake --build  .
Scanning dependencies of target Rozzeta
[ 50%] Building C object CMakeFiles/Rozzeta.dir/main.c.o
/Users/gajaka/CLionProjects/Rozzeta/main.c:4:10: fatal error: 'gmp.h' file not found
#include <gmp.h>
         ^~~~~~~
1 error generated.
make[2]: *** [CMakeFiles/Rozzeta.dir/main.c.o] Error 1
make[1]: *** [CMakeFiles/Rozzeta.dir/all] Error 2
make: *** [all] Error 2

I have compiled manually with:

cc main.c -lgmp

Anyone can help me with this ? Many thanks in advance

4 Answers

It was very frustrating for me! I am personally using CLion from jetbrains, while tried to build old codes written in C/C++, it's giving me error because MacOS 10.15, Catalina removed the C headers pkg used to be in Mojave. So, found another way around and tried.

If you have installed XCode 11.1, then open the terminal and run the following command:

xcode-select --install

then,

sudo ln -s /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/* /usr/local/include/

Now, you are good to go. Try to build using cmake.

If you're calling find_package(GMP ...) in your CMake file, why not use the GMP-specific variables it populates? It looks like CMake successfully finds GMP installed on your system (based on your CMake log), so try using the GMP_* variables that should be populated:

cmake_minimum_required(VERSION 3.14)

project("ROZZETA" VERSION 0.0.1 LANGUAGES C)

# Allow us to import cmake scripts from  ./cmake
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}")
# Compiler flags
set(CMAKE_C_COMPILER /usr/bin/clang CACHE PATH "")

find_package(GMP REQUIRED)
add_executable(Rozzeta main.c)

# Use the populated GMP variables here.
target_include_directories(Rozzeta PRIVATE ${GMP_INCLUDE_DIR})
target_link_libraries(Rozzeta PRIVATE ${GMP_LIB})

after

xcode-select --install

I got a clang but no cmake so I did

brew install cmake

which installed cmake for Catalina.

In order to compile programs with cmake and XCode's toolchain in Mac OS 10.15 (Catalina) you must install xcode command line utility tools + make some additional symlinks (@Roy said about it, but forgot to mention other two):

$ xcode-select --install
$ sudo ln -s /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/* /usr/local/include/
$ for i in {Platforms,Toolchains,Tools}; do sudo ln -sn /Applications/Xcode.app/Contents/Developer/$i /Library/Developer/CommandLineTools/$i; done
Related