22

I am trying to install an external library into my C++ project using Cmake. I want the Xcode project to be produced with that library. In my terminal i run the following from the build directory:

cmake -G Xcode ..

and that gives me the following errors:

-- The CXX compiler identification is unknown
-- The C compiler identification is unknown
CMake Error at CMakeLists.txt:6 (project):
No CMAKE_CXX_COMPILER could be found.

CMake Error at CMakeLists.txt:6 (project):
  No CMAKE_C_COMPILER could be found.

I am using g++ compiler:

 Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
    Apple LLVM version 7.0.0 (clang-700.1.76)
    Target: x86_64-apple-darwin14.5.0
    Thread model: posix

Edit: CMakeLists.txt file

file(READ "${CMAKE_CURRENT_SOURCE_DIR}/VERSION" PROJECT_VERSION_FULL)
string(REGEX REPLACE "[\n\r]" "" PROJECT_VERSION_FULL "${PROJECT_VERSION_FULL}")
string(REGEX REPLACE "^([0-9]+)\\.[0-9]+\\.[0-9]+$" "\\1" PROJECT_VERSION_MAJOR "${PROJECT_VERSION_FULL}")
string(REGEX REPLACE "^[0-9]+\\.([0-9]+)\\.[0-9]+$" "\\1" PROJECT_VERSION_MINOR "${PROJECT_VERSION_FULL}")
string(REGEX REPLACE "^[0-9]+\\.[0-9]+\\.([0-9]+)$" "\\1" PROJECT_VERSION_PATCH "${PROJECT_VERSION_FULL}")
set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}")
math(EXPR LIBRARY_VERSION_MAJOR "${PROJECT_VERSION_MAJOR}")
set(LIBRARY_VERSION_MINOR "${PROJECT_VERSION_MINOR}")
set(LIBRARY_VERSION_PATCH "${PROJECT_VERSION_PATCH}")
set(LIBRARY_VERSION "${LIBRARY_VERSION_MAJOR}.${LIBRARY_VERSION_MINOR}")
set(LIBRARY_VERSION_FULL "${LIBRARY_VERSION}.${LIBRARY_VERSION_PATCH}")


option(CODE_COVERAGE "Set ON to add code coverage compile options" OFF)
option(GENERATE_DOC "Set ON to genrate doxygen API reference in build/doc directory" OFF)

# C++11 compiler Check
if(NOT CMAKE_CXX_COMPILER_VERSION) # work around for cmake versions smaller than 2.8.10
    execute_process(COMMAND ${CMAKE_CXX_COMPILER} -dumpversion OUTPUT_VARIABLE CMAKE_CXX_COMPILER_VERSION)
endif()
if(CMAKE_CXX_COMPILER MATCHES ".*clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
      set(CMAKE_COMPILER_IS_CLANGXX 1)
endif()
if( (CMAKE_COMPILER_IS_GNUCXX AND ${CMAKE_CXX_COMPILER_VERSION} VERSION_LESS 4.7) OR
    (CMAKE_COMPILER_IS_CLANGXX AND ${CMAKE_CXX_COMPILER_VERSION} VERSION_LESS 3.2))
  message(FATAL_ERROR "Your C++ compiler does not support C++11. Please install g++ 4.7 (or greater) or clang 3.2 (or greater)")
else()
  message(STATUS "Compiler is recent enough to support C++11.")
endif()

if( CMAKE_COMPILER_IS_GNUCXX )
    append_cxx_compiler_flags("-std=c++11 -Wall -Wextra  -DNDEBUG" "GCC" CMAKE_CXX_FLAGS)
    append_cxx_compiler_flags("-O3 -ffast-math -funroll-loops" "GCC" CMAKE_CXX_OPT_FLAGS)
    if ( CODE_COVERAGE )
        append_cxx_compiler_flags("-g -fprofile-arcs -ftest-coverage -lgcov" "GCC" CMAKE_CXX_FLAGS)
    endif()

else()
    if(MSVC)
        append_cxx_compiler_flags("/EHsc" "MSVC" CMAKE_CXX_FLAGS)
        append_cxx_compiler_flags("/Od" "MSVC" CMAKE_CXX_FLAGS_DEBUG)
        append_cxx_compiler_flags("/Ox" "MSVC" CMAKE_CXX_FLAGS_RELEASE)
        set(vars CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
        foreach(var ${vars})
            string(REPLACE "/MD" "-MT" ${var} "${${var}}")
        endforeach(var)

        add_definitions("/DMSVC_COMPILER")
    else()
        append_cxx_compiler_flags("-std=c++11 -DNDEBUG" "CLANG" CMAKE_CXX_FLAGS)
        append_cxx_compiler_flags("-stdlib=libc++" "CLANG" CMAKE_CXX_FLAGS)
        append_cxx_compiler_flags("-O3 -ffast-math -funroll-loops" "CLANG" CMAKE_CXX_OPT_FLAGS)
    endif()
endif()

Also CMakeEdit.log:

Compiling the CXX compiler identification source file "CMakeCXXCompilerId.cpp" failed.
Compiler:  
Build flags: 
Id flags: 

The output was:
1
xcode-select: error: tool 'xcodebuild' requires Xcode, but active developer directory '/Library/Developer/CommandLineTools' is a command line tools instance


Compiling the C compiler identification source file "CMakeCCompilerId.c" failed.
Compiler:  
Build flags: 
Id flags: 

The output was:
1
xcode-select: error: tool 'xcodebuild' requires Xcode, but active developer directory '/Library/Developer/CommandLineTools' is a command line tools instance

EDIT:

This problem happens when xcode-select developer directory was pointing to /Library/Developer/CommandLineTools, when a full regular XCode was required (happens when CLT are installed after XCode).

I have found the solution to be this:

sudo xcode-select -s /Applications/Xcode.app/Contents/Developer

After this being done, when I run cmake -G Xcode .. I get other errors:

-- The CXX compiler identification is AppleClang 7.0.0.7000176
-- The C compiler identification is AppleClang 7.0.0.7000176
CMake Error at /usr/local/Cellar/cmake/3.4.0/share/cmake/Modules/Platform/Darwin.cmake:76 (message):
  CMAKE_OSX_DEPLOYMENT_TARGET is '10.10' but CMAKE_OSX_SYSROOT:

   ""

  is not set to a MacOSX SDK with a recognized version.  Either set
  CMAKE_OSX_SYSROOT to a valid SDK or set CMAKE_OSX_DEPLOYMENT_TARGET to
  empty.
Call Stack (most recent call first):
  /usr/local/Cellar/cmake/3.4.0/share/cmake/Modules/CMakeSystemSpecificInformation.cmake:36 (include)
  CMakeLists.txt:6 (project)

EDIT 2 Looks like the SDK specified by the OS is wrong.

CMAKE_OSX_DEPLOYMENT_TARGET is '10.10' but the matching SDK does not exist
  at:

   "/Applications/DEV/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk"

  Instead using SDK:

   "/Applications/DEV/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk".
Call Stack (most recent call first):
  /usr/local/Cellar/cmake/3.4.0/share/cmake/Modules/CMakeSystemSpecificInitialize.cmake:18 (include)
  CMakeLists.txt:6 (project)


-- The CXX compiler identification is AppleClang 7.0.0.7000176
-- The C compiler identification is AppleClang 7.0.0.7000176
CMake Error at /usr/local/Cellar/cmake/3.4.0/share/cmake/Modules/Platform/Darwin.cmake:76 (message):
  CMAKE_OSX_DEPLOYMENT_TARGET is '10.10' but CMAKE_OSX_SYSROOT:

   ""

  is not set to a MacOSX SDK with a recognized version.  Either set
  CMAKE_OSX_SYSROOT to a valid SDK or set CMAKE_OSX_DEPLOYMENT_TARGET to
  empty.
Call Stack (most recent call first):
  /usr/local/Cellar/cmake/3.4.0/share/cmake/Modules/CMakeSystemSpecificInformation.cmake:36 (include)
  CMakeLists.txt:6 (project)
Whizzil
  • 1,264
  • 6
  • 22
  • 39
  • You should post your `CMakeLists.txt`, and look at line 6 in particular. – l'L'l Nov 30 '15 at 12:52
  • @I'L'I: I made an edit: added CMakeLists.txt – Whizzil Nov 30 '15 at 13:04
  • You're also using homebrew CMake, so your paths might get thrown off (I don't use homebrew). The other errors you are getting should be just a matter of putting the right info in there (target and sdk). – l'L'l Nov 30 '15 at 13:26
  • putting where? It obtains it automatically from the OS. I will make an update. – Whizzil Nov 30 '15 at 13:36
  • I should probably start another topic... – Whizzil Nov 30 '15 at 13:40
  • It could also be an issue with homebrew and the version of Xcode you are using — I've seen that error before. What version of Xcode are you using. There are a couple of things you can add to your CMakeLists.txt also, which I'll add as an answer. – l'L'l Nov 30 '15 at 13:43
  • "I want the Xcode project to be produced with that library." — how do expect to do that without using components of Xcode? CMake doesn't do it all by itself and neither does the OS. – l'L'l Nov 30 '15 at 13:51
  • well thats what `cmake -G Xcode ..` should do. I have seen it working on other machine. It just creates the Xcode project with the library using CMake. – Whizzil Nov 30 '15 at 13:55
  • Are you telling me you don't have have Xcode installed? – l'L'l Nov 30 '15 at 14:01
  • I have Xcode7 installed, I have CMake installed, I have external library installed which uses CMake. – Whizzil Nov 30 '15 at 14:04
  • Is the 10.10 SDK installed as well? Because my Xcode 7 doesn't have it. – l'L'l Nov 30 '15 at 14:12
  • No it isnt, and that was the problem i think. Just 10.11. – Whizzil Nov 30 '15 at 14:14
  • I found the issue with missing compiler IDs to be because I had configured CC and CXX to use GPP in my `.bash_profile`. When I changed to Clang everything worked as expected. – Morten Mar 22 '16 at 16:12

6 Answers6

32

I had the same problem, but I solved it with:

sudo xcode-select --reset

Before doing the above, xcode-select -p reported the path was /Library/Developer/CommandLineTools.

After the reset, the path was /Applications/Xcode.app/Contents/Developer.

4dan
  • 1,033
  • 10
  • 14
11

I had the same output and could solve it by agreeing to the apple license.

sudo xcodebuild -license accept
Arwed Mett
  • 2,614
  • 1
  • 22
  • 35
  • 2
    This gives me `xcode-select: error: tool 'xcodebuild' requires Xcode, but active developer directory '/Library/Developer/CommandLineTools' is a command line tools instance`. EDIT: Until I followed 4dan's answer which fixed it. – Tim MB Aug 24 '19 at 10:57
3

The error you have with the SDK can usually be solved by clearing the CMake build cache and adding the following to your CMakeLists.txt before project():

SET(MACOSX_DEPLOYMENT_TARGET ${DARWIN_MAJOR_SDK_VERSION}.${DARWIN_MINOR_SDK_VERSION})
SET(CMAKE_CXX_FLAGS "--sysroot ${CMAKE_OSX_SYSROOT} ${CMAKE_CXX_FLAGS}")
MESSAGE("Setting MACOSX_DEPLOYMENT_TARGET to '${MACOSX_DEPLOYMENT_TARGET}'.")

If you find that doesn't solve the issue then you should check the version of Xcode is current and has the SDK installed you are specifying. Generally Homebrew and Macports CMake both should have the latest stable build of Xcode installed.

https://github.com/Homebrew/homebrew/issues/23074

l'L'l
  • 44,951
  • 10
  • 95
  • 146
  • Does it matter at which place in a file I add this line? I added it to the start of a file, and the problem persists. Also, I reinstalled Homebrew Cmake 15 minutes ago. – Whizzil Nov 30 '15 at 13:57
  • after `set(LIBRARY_VERSION_FULL "${LIBRARY_VERSION}.${LIBRARY_VERSION_PATCH}")`should be fine. – l'L'l Nov 30 '15 at 13:59
  • Also MESSAGE("Setting MACOSX_DEPLOYMENT_TARGET to '${MACOSX_DEPLOYMENT_TARGET}'.") --> this does not appear in command line when I run the cmake command – Whizzil Nov 30 '15 at 14:06
  • Add it before project(), also CMAKE_OSX_SYSROOT is probably cached. So you're going to continue having this issue until your clean your project. – l'L'l Nov 30 '15 at 14:07
  • exactly the same thing – Whizzil Nov 30 '15 at 14:09
  • It just needed the Cache file to be removed, now it works. Thank you. – Whizzil Nov 30 '15 at 14:14
3

I had the same issue and as mentioned in one of the comments it appears to be due to the fact that I installed the command-line tools first.

I solved it by opening the Xcode app, going to Preferences -> Locations, and selecting the Xcode installation from the dropdown for Command Line Tools. It was initially blank.

Rotsiser Mho
  • 551
  • 2
  • 5
  • 19
3

After upgrading to the latest version of CMake, it worked again.

cmake 3.25.0

AppleClang 14.0.0.14000029

Jinesi Yelizati
  • 271
  • 1
  • 7
  • after I upgrade XCode to 14.2, my cmake 3.23.2 broken. after I uninstall cmake and install again to cmake 3.26.3, the error gone. – andrewchan2022 Apr 11 '23 at 08:09
  • Same here, had CMake 3.23.1 and downloaded CMake from [Kitware v3.26.4](https://cmake.org/download/), I'm using Xcode 14.1beta, after the "upgrade" (replaced existing CMake) the error went away – Meir Gabay Jun 29 '23 at 12:01
1

I solved a similar problem by

sudo xcodebuild -license

After agreeing the license, C compiler and CXX compiler can be identified.

================================

As a reference, my error message is:

-- The C compiler identification is unknown
-- The CXX compiler identification is unknown
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- broken
CMake Error at /usr/local/Cellar/cmake/3.13.3/share/cmake/Modules/CMakeTestCCompiler.cmake:52 (message):
  The C compiler "/usr/bin/cc" is not able to compile a simple test program.
  It fails with the following output:
    Change Dir: /Users/MyName/pyscf/pyscf/lib/build/CMakeFiles/CMakeTmp
    Run Build Command:"/usr/bin/make" "cmTC_114dd/fast"
    Agreeing to the Xcode/iOS license requires admin privileges, please run “sudo xcodebuild -license” and then retry this command.
  CMake will not be able to correctly generate this project.
  Call Stack (most recent call first):
  CMakeLists.txt:16 (project)
-- Configuring incomplete, errors occurred!
Fileland
  • 351
  • 1
  • 3
  • 9