简体   繁体   中英

Problem with compilers when using cmake to build a Xcode project

I have been using CMake for years to build my Xcode projects. Suddenly, maybe after upgrading the OSX (11.6) or maybe after upgrading the Xcode (13.0, commandline tools 13.0), cmake does not work anymore.

First let me give some of the things I have already tried so I am just not referred to an answered question and forgotten:

sudo xcode-select --reset This has no effect.

I have tried resetting the cmake variable manually with cmake.. -DCMAKE_C_COMPILER="/usr/bin/cc" and to other paths. It still gives a "Compiling the C compiler identification source file "CMakeCCompilerId.c" failed." in the CMakeError.log. No c compiler that I can find in my system seems to work including clang in various locations.

I have tried reseting the environmental variables CC, CXX, CLANG, etc to different paths.

This might have something to do with Anaconda which I have installed.

I created a test project to pinpoint the problem. In it is a main.cpp

#include <iostream>

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

and a CMakeLists.txt

cmake_minimum_required(VERSION 3.14)
project(untitled)

set(CMAKE_CXX_STANDARD 11)

add_executable(untitled main.cpp)

The in a build subdirectory I run

>>> cmake .. -G Xcode
-- The C compiler identification is unknown
-- The CXX compiler identification is unknown
CMake Error at CMakeLists.txt:2 (project):
  No CMAKE_C_COMPILER could be found.



CMake Error at CMakeLists.txt:2 (project):
  No CMAKE_CXX_COMPILER could be found.



-- Configuring incomplete, errors occurred!
See also "../Test/build/CMakeFiles/CMakeOutput.log".
See also "../Test/build/CMakeFiles/CMakeError.log".

The beginning of CMakeFiles/CMakeError.log is:

Compiling the C compiler identification source file "CMakeCCompilerId.c" failed.
Compiler:  
Build flags: -march=core2;-mtune=haswell;-mssse3;-ftree-vectorize;-fPIC;-fPIE;-fstack-protector-strong;-O2;-pipe;-isystem;/opt/anaconda3/include
Id flags:  

The output was:
65
Command line invocation:
    /Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild

User defaults from command line:
    IDEPackageSupportUseBuiltinSCM = YES

note: Using new build system
note: Planning
Analyze workspace

.
.
.
   clang-10: error: invalid Darwin version number: macos11.3
    clang-10: error: invalid version number in '-target x86_64-apple-macos11.3'
 
.
.
.

I manage to workout with a similar problem setting some variables inside CMakeLists.txt. Is not a deffinitive solution, however. It needs a followup.

cmake_minimum_required(VERSION 3.14)
project(untitled)
set(CMAKE_C_COMPILER "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc")
set(CMAKE_CXX_COMPILER "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++")
set(CMAKE_CXX_STANDARD 11)

add_executable(untitled main.cpp)

==== EDIT =====

Following this idea I managed to fix this problem (which was one problem on my side too).

There's a problem with the variable CC and CXX created by Xcode. They try to do something like:

xcrun -find cc

The result of that, on my side is:

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc

This is consistent and, if I look for that compiler, it exists. But, it doesn't work within CMake.

I manage to make it work setting those two variables to where they are pointing at:

export CC=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
export CXX=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++

After that, I managed to compile with CMake without setting the variable inside of the CMakeLists.txt file.

You should add those exports to the end of your.bash_profile

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM