使用M1 MacBook Pro和cmake时的编译错误

7

我刚刚获得了新的M1 MacBook Pro,并正在尝试编译我大学工作所需的代码库。这是我所采取的步骤:

  1. 我将终端设置为始终使用Rosetta。
  2. 使用/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"安装Homebrew。
  3. 使用brew install cmake安装CMake。
  4. 运行cmake <源路径>(此操作正常)。
  5. 运行make(此操作失败)。

这是我得到的错误:

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/stdlib.h:97:15: fatal error: 
      'stdlib.h' file not found
#include_next <stdlib.h>

我已尝试卸载和重新安装Xcode和CommandLineTools,但没有成功。

当使用cmake gui应用程序时,我遇到了不同的错误。 我将其设置为使用CommandLineTools / SDKs / MacOsX11.0.sdk。

在使用此cmake gui设置生成和配置构建文件后运行make时,我收到以下错误:

    -- The C compiler identification is AppleClang 12.0.0.12000032
-- The CXX compiler identification is AppleClang 12.0.0.12000032
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - failed
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc - broken
CMake Error at /Applications/CMake.app/Contents/share/cmake-3.18/Modules/CMakeTestCCompiler.cmake:66 (message):
  The C compiler

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

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: /Users/freyamurphy/nori/build/ext_build/src/tbb_p-build/CMakeFiles/CMakeTmp

如果我运行命令/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc test.c(简单的hello world程序),我会得到以下错误:
test.c:1:10: fatal error: 'stdio.h' file not found
#include "stdio.h"

但是使用cc test.c一切正常。(这将运行/usr /bin/cc)。

我觉得可能是Xcode存在问题。有没有人遇到过类似的问题,可以给我一些建议?非常感谢。


我曾经遇到过这种问题,只需删除构建目录并再次运行Cmake即可解决。我还将源目录移动到其他目录,但我不认为会有任何变化。 - xfajk X
1个回答

10

在安装完Xcode后,您是否重新打开终端?这是需要设置环境变量SDKROOT

关闭并重新运行终端或在终端中打开一个新选项卡。

或在当前终端中运行以下命令:

export SDKROOT=$(xcrun --sdk macosx --show-sdk-path)

同时确保选择了正确的Xcode工具链。

# list available xcode toolchains
xcode-select -p

# select one of listed above
sudo xcode-select -s /Applications/Xcode.app/Contents/Developer

谢谢,我觉得这个方法有效!现在我有一些其他的错误,但它们似乎与另外的东西有关。 - alwaysSpaegie
对我来说,关键是运行您包含的导出命令。由于某种原因,仅仅在终端中打开一个新选项卡并不能解决任何问题。 - Tim Mackey

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接