7

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

Gajaka
  • 71
  • 1
  • 2
  • I can see `target_link_libraries(Rozzeta ...)` but I don't see any `target_include_directories(Rozetta ...)` which I would have expected. So I think it's a broken `CMakeLists.txt` file and nothing to do with macOS/Xcode/clang. – trojanfoe Oct 09 '19 at 09:10
  • Many thanks for your answer. I have added `target_include_directories` and now I have another problem `[ 50%] Building C object CMakeFiles/Rozzeta.dir/main.c.o [100%] Linking C executable Rozzeta ld: library not found for -lgmp clang: error: linker command failed with exit code 1 (use -v to see invocation)` the code I have added is :`set(INCLUDE_DIRS /usr/local/include/ /usr/local/lib/)` and `target_include_directories(Rozzeta PUBLIC ${INCLUDE_DIRS})`. Perhaps I am missing something basic - I am new in clang. – Gajaka Oct 09 '19 at 13:47
  • `/usr/local/lib/` isn't an include directory, it's a linker directory so put it instead into `target_link_directories(Rozetta ...)`. However this is making the use of `find_package(GMP REQUIRED)` pointless, so you may as well remove that. – trojanfoe Oct 09 '19 at 13:50
  • thanks again for helping me. However, I am getting the same error: `ld: library not found for -lgmp clang: error: linker command failed with exit code 1 (use -v to see invocation)` – Gajaka Oct 09 '19 at 14:07
  • I found the root of the problem. In the `link.txt` file I removed `-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk` and build passed successfully. Any idea what needs to be done in order to have `link.txt` file unmodified ? – Gajaka Oct 09 '19 at 15:06
  • I don't know what `link.txt` is. – trojanfoe Oct 09 '19 at 15:47
  • fixed : 1) added `set(CMAKE_OSX_SYSROOT "/")` to exclude `-isysroot ..MacOSX10.15.sdk` where header files are placed. 2) changed C compiler to `/usr/bin/clang`. – Gajaka Oct 09 '19 at 21:28
  • That sysroot doesn't look right. You can probably leave it off altogether or specify the real one which you can get from `xcrun`. – trojanfoe Oct 10 '19 at 05:59

4 Answers4

5

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.

Roy
  • 1,189
  • 7
  • 13
  • yes I am using CLion as well. Your fix does not work for me . I tried a few days ago. Many thanks for answer. I am happy with my temporary fix at the moment. And yes there is no `macOS_SDK_headers_for_macOS_10.XX.pkg` at all. – Gajaka Oct 14 '19 at 19:45
  • Thanks, `xcode-select --install` fixed my CLion cmake/build issues after upgrading to Catalina. – 101 Oct 15 '19 at 02:53
  • 10
    Mass symlinking all the content from `/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/` to `/usr/local/include/` is a bad idea. This will break several packages that compile with a range of libraries. @Roy you should at least add a disclaimer. – tmo Nov 25 '19 at 13:33
  • Please never do that (bulk symlinking to /usr/local)... You will create a huge mess – melMass May 11 '21 at 21:27
0

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})
Kevin
  • 16,549
  • 8
  • 60
  • 74
-1

after

xcode-select --install

I got a clang but no cmake so I did

brew install cmake

which installed cmake for Catalina.

Chris F Carroll
  • 11,146
  • 3
  • 53
  • 61
-1

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
mortalis
  • 2,060
  • 24
  • 34
  • 1
    It was already commented on @Roy answer that creating all these symlinks is not a good idea, as it breaks several other packages... – Kevin Feb 26 '20 at 16:42