【发布时间】:2020-02-06 13:40:33
【问题描述】:
我最近用 Xcode 11.1 安装了 macos catalina 并更新了 cmake、clang 和 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
CMakeLists.txt 如下所示:
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 检测到 gmp 成功:
/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
构建失败:
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
我已经手动编译了:
cc main.c -lgmp
任何人都可以帮助我吗? 非常感谢提前
【问题讨论】:
-
我可以看到
target_link_libraries(Rozzeta ...),但我没有看到任何我预期的target_include_directories(Rozetta ...)。所以我认为这是一个损坏的CMakeLists.txt文件,与 macOS/Xcode/clang 无关。 -
非常感谢您的回答。我添加了
target_include_directories,现在我有另一个问题[ 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)我添加的代码是:set(INCLUDE_DIRS /usr/local/include/ /usr/local/lib/)和target_include_directories(Rozzeta PUBLIC ${INCLUDE_DIRS})。也许我缺少一些基本的东西 - 我是 clang 的新手。 -
/usr/local/lib/不是包含目录,而是链接器目录,因此请将其放入target_link_directories(Rozetta ...)。但是,这使得find_package(GMP REQUIRED)的使用毫无意义,因此您不妨删除它。 -
再次感谢您帮助我。但是,我遇到了同样的错误:
ld: library not found for -lgmp clang: error: linker command failed with exit code 1 (use -v to see invocation) -
我找到了问题的根源。在
link.txt文件中,我删除了-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk,构建成功通过。知道需要做什么才能使link.txt文件不被修改吗?